"""Worker entrypoint: runs trigger checker + future background jobs. Waits for the database (and required tables) to be ready before starting the background loops. This prevents the worker from crashing when the backend hasn't finished running `init_db()` yet (typical docker-compose race condition where both services start in parallel). """ from __future__ import annotations import asyncio from app.db_wait import wait_for_db_or_exit from app.logging_setup import get_logger, setup_logging from app.workers.trigger_runner import main_loop as trigger_loop log = get_logger("worker") async def main(): setup_logging() log.info("worker_starting") # Wait until DB is reachable and the `settings` table exists. # The backend's lifespan runs `init_db()` which creates tables; if the # worker comes up first, this loop will retry until that completes. await wait_for_db_or_exit(max_retries=60, delay=2.0) log.info("worker_db_ready_starting_loops") # Run all background loops concurrently await asyncio.gather( trigger_loop(), # Future: rag indexer, summary compactor, etc. ) if __name__ == "__main__": asyncio.run(main())