in atr/worker.py [0:0]
def main() -> None:
"""Main entry point."""
import atr.config as config
conf = config.get()
if os.path.isdir(conf.STATE_DIR):
os.chdir(conf.STATE_DIR)
_setup_logging()
_LOGGER.info(f"Starting worker process with pid {os.getpid()}")
tasks: list[asyncio.Task] = []
async def _handle_signal(signum: int) -> None:
_LOGGER.info(f"Received signal {signum}, shutting down...")
await db.shutdown_database()
for t in tasks:
t.cancel()
_LOGGER.debug("Cancelled all running tasks")
asyncio.get_event_loop().stop()
_LOGGER.debug("Stopped event loop")
for s in (signal.SIGTERM, signal.SIGINT):
signal.signal(s, lambda signum, frame: asyncio.create_task(_handle_signal(signum)))
_worker_resources_limit_set()
async def _start() -> None:
await asyncio.create_task(db.init_database_for_worker())
tasks.append(asyncio.create_task(_worker_loop_run()))
await asyncio.gather(*tasks)
asyncio.run(_start())
# If the worker decides to stop running (see #230 in _worker_loop_run()), shutdown the database gracefully
asyncio.run(db.shutdown_database())
_LOGGER.info("Exiting worker process")