not fixed: unable to gracefully shutdown matrix server issue
This commit is contained in:
parent
91984f45fa
commit
0f71c0e74d
2 changed files with 20 additions and 3 deletions
|
@ -2,6 +2,7 @@ import asyncio
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
|
import sys
|
||||||
|
|
||||||
from nio import AsyncClient, MatrixRoom, RoomMessageText
|
from nio import AsyncClient, MatrixRoom, RoomMessageText
|
||||||
from nio.events.room_events import RoomMessageText
|
from nio.events.room_events import RoomMessageText
|
||||||
|
@ -95,7 +96,6 @@ class MatrixAdapter:
|
||||||
async def stop(self):
|
async def stop(self):
|
||||||
"""Stop the bot."""
|
"""Stop the bot."""
|
||||||
logger.info("Stopping Matrix bot...")
|
logger.info("Stopping Matrix bot...")
|
||||||
await self.client.logout()
|
|
||||||
await self.client.close()
|
await self.client.close()
|
||||||
|
|
||||||
|
|
||||||
|
@ -144,10 +144,13 @@ async def main():
|
||||||
await bot.start()
|
await bot.start()
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
logger.info("Bot interrupted by user")
|
logger.info("Bot interrupted by user")
|
||||||
|
# TODO: fix unable to gracefully shutdown issue
|
||||||
|
await bot.stop()
|
||||||
|
sys.exit(0)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Bot error: {e}")
|
logger.error(f"Bot error: {e}")
|
||||||
finally:
|
|
||||||
await bot.stop()
|
await bot.stop()
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
16
main.py
16
main.py
|
@ -1,5 +1,6 @@
|
||||||
import asyncio
|
import asyncio
|
||||||
import logging
|
import logging
|
||||||
|
import signal
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
import config
|
import config
|
||||||
|
@ -20,7 +21,20 @@ async def main():
|
||||||
# Initialize and start Matrix bot if configured
|
# Initialize and start Matrix bot if configured
|
||||||
tasks.append(matrix_bot.main())
|
tasks.append(matrix_bot.main())
|
||||||
if tasks:
|
if tasks:
|
||||||
await asyncio.gather(*tasks, return_exceptions=True)
|
# Setup signal handler for graceful shutdown
|
||||||
|
def signal_handler(signum, frame):
|
||||||
|
logging.info("Received shutdown signal, cancelling all tasks...")
|
||||||
|
for task in tasks:
|
||||||
|
task.cancel()
|
||||||
|
|
||||||
|
# Register signal handlers
|
||||||
|
for sig in (signal.SIGTERM, signal.SIGINT):
|
||||||
|
asyncio.get_event_loop().add_signal_handler(sig, signal_handler, sig, None)
|
||||||
|
|
||||||
|
try:
|
||||||
|
await asyncio.gather(*tasks, return_exceptions=True)
|
||||||
|
except asyncio.CancelledError:
|
||||||
|
logging.info("All tasks cancelled successfully")
|
||||||
else:
|
else:
|
||||||
logging.error("No bot is configured to start. Please check your configuration.")
|
logging.error("No bot is configured to start. Please check your configuration.")
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue