diff --git a/ingest/radieo/__main__.py b/ingest/radieo/__main__.py index 7923a9c..160f4ff 100644 --- a/ingest/radieo/__main__.py +++ b/ingest/radieo/__main__.py @@ -1,6 +1,8 @@ """Entry point: wire providers/fetchers into the queue and serve GET /next.""" import logging +import signal +import threading from . import config from .api import IngestServer @@ -159,6 +161,17 @@ def main() -> None: config.CACHE_DIR, config.STATE_DB, ) + + # Shut down cleanly on `docker compose down` (SIGTERM) instead of being + # killed after the grace period. shutdown() must run off the serving + # thread, so hand it to a helper thread. + def _handle_signal(signum, _frame): + log.info("received signal %d, shutting down", signum) + threading.Thread(target=server.shutdown, daemon=True).start() + + signal.signal(signal.SIGTERM, _handle_signal) + signal.signal(signal.SIGINT, _handle_signal) + try: server.serve_forever() except KeyboardInterrupt: