This commit is contained in:
Mikan
2026-06-19 16:31:45 +03:00
parent d0d1f003ae
commit 5a78def096
21 changed files with 1250 additions and 548 deletions

View File

@@ -1,9 +1,12 @@
"""Worker entrypoint: runs trigger checker + future background jobs.
"""Worker entrypoint.
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).
Historically this ran a trigger-polling loop. Triggers now fire in-process
inside the orchestrator when in-game time changes (see app.core.triggers),
so the worker has nothing to do at the moment. We keep the container running
as a placeholder for future background jobs (RAG re-indexer, summary
compactor, etc.).
If you add a background job, register it in `asyncio.gather(...)` below.
"""
from __future__ import annotations
@@ -11,7 +14,6 @@ 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")
@@ -20,17 +22,19 @@ 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.
# Make sure the DB is reachable and tables exist before doing anything.
# (Future background jobs may need this.)
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.
)
log.info("worker_ready_no_jobs_registered")
# Nothing to do for now — sleep forever. Future background jobs go here:
# await asyncio.gather(
# some_future_loop(),
# another_future_loop(),
# )
while True:
await asyncio.sleep(3600)
if __name__ == "__main__":