feat: add graceful SIGTERM handling for clean shutdown

Install a SIGTERM signal handler that stops the bot loop and closes
the IRC connection, allowing cProfile stats to flush before exit.
This commit is contained in:
user
2026-02-15 17:06:37 +01:00
parent 29e77f97b2
commit 23ba7dc474

View File

@@ -49,13 +49,27 @@ def build_parser() -> argparse.ArgumentParser:
def _run(bot: Bot) -> None:
"""Run the bot event loop."""
"""Run the bot event loop with graceful SIGTERM handling."""
import signal
async def _start_with_signal():
loop = asyncio.get_running_loop()
loop.add_signal_handler(signal.SIGTERM, _shutdown, bot)
await bot.start()
try:
asyncio.run(bot.start())
asyncio.run(_start_with_signal())
except KeyboardInterrupt:
logging.getLogger("derp").info("interrupted, shutting down")
def _shutdown(bot: Bot) -> None:
"""Signal handler: stop the bot loop so cProfile can flush."""
logging.getLogger("derp").info("SIGTERM received, shutting down")
bot._running = False
asyncio.get_running_loop().create_task(bot.conn.close())
def main(argv: list[str] | None = None) -> int:
"""Main entry point."""
parser = build_parser()