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:
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user