Files
ai-rpg/backend/app/workers/main.py

38 lines
1.2 KiB
Python
Raw Normal View History

2026-06-19 11:30:38 +03:00
"""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).
"""
2026-06-19 11:28:04 +03:00
from __future__ import annotations
import asyncio
2026-06-19 11:30:38 +03:00
from app.db_wait import wait_for_db_or_exit
2026-06-19 11:28:04 +03:00
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")
2026-06-19 11:30:38 +03:00
# 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")
2026-06-19 11:28:04 +03:00
# Run all background loops concurrently
await asyncio.gather(
trigger_loop(),
# Future: rag indexer, summary compactor, etc.
)
if __name__ == "__main__":
asyncio.run(main())