Spaces:
Runtime error

BattleFiven's picture
deploy: round 6 save_intermediate light (sha 05316bb)
ed86a97
Raw History Blame Contribute Delete
1.97 kB
"""Background retry loop for OpenProject syncs that failed.
Wired in the FastAPI lifespan (when OPENPROJECT_RETRY_LOOP_ENABLED=true).
Picks up to N tickets in 'failed' state with attempts < max and re-tries
them, throttled by the configured interval.
"""
from __future__ import annotations
import asyncio
from src.db import BugTicketRepository
from src.db.database import get_db
from src.integrations.openproject.config import OpenProjectConfig
from src.integrations.openproject.sync import sync_bug_to_openproject
from src.utils.logging import get_logger
logger = get_logger(__name__)
_BATCH_SIZE = 10
async def run_retry_loop() -> None:
"""Forever loop. Cancellable via task.cancel() from lifespan shutdown."""
cfg = OpenProjectConfig.from_env()
if cfg is None or not cfg.retry_loop_enabled:
logger.info("openproject_retry_loop_disabled")
return
interval = max(60, cfg.retry_interval_seconds)
logger.info(
"openproject_retry_loop_started",
extra={"interval_seconds": interval, "max_attempts": cfg.max_sync_attempts},
)
while True:
try:
await asyncio.sleep(interval)
await asyncio.to_thread(_retry_one_batch, cfg)
except asyncio.CancelledError:
logger.info("openproject_retry_loop_stopped")
raise
except Exception:
logger.exception("openproject_retry_loop_iteration_failed")
def _retry_one_batch(cfg: OpenProjectConfig) -> None:
db = get_db()
with db.get_session() as session:
repo = BugTicketRepository(session)
candidates = repo.list_failed_for_retry(
max_attempts=cfg.max_sync_attempts, limit=_BATCH_SIZE
)
for ticket in candidates:
try:
sync_bug_to_openproject(ticket.id, config=cfg)
except Exception:
logger.exception(
"openproject_retry_loop_ticket_failed", extra={"bug_id": ticket.id}
)