not fixed: unable to gracefully shutdown matrix server issue

This commit is contained in:
草师傅 2025-08-24 22:14:58 +08:00
parent 91984f45fa
commit 0f71c0e74d
2 changed files with 20 additions and 3 deletions

16
main.py
View file

@ -1,5 +1,6 @@
import asyncio
import logging
import signal
import sys
import config
@ -20,7 +21,20 @@ async def main():
# Initialize and start Matrix bot if configured
tasks.append(matrix_bot.main())
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:
logging.error("No bot is configured to start. Please check your configuration.")