videogen/apps/api/app/services/job_idempotency.py
caelum0x d56639a594
Some checks failed
build-images / web (push) Has been cancelled
vision2026-ci / Python SDK resource tests (push) Has been cancelled
build-images / api (push) Has been cancelled
build-images / worker-gpu (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (comfy-workflow-schema) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (effects-core) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (eventbus-types) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (growth-analytics) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (provider-adapter-kit) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (publishing-connectors) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (render-core) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (shot-template-schema) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (trends-schema) (push) Has been cancelled
vision2026-ci / Desktop engine crates (test + clippy + fmt) (push) Has been cancelled
codeql / Analyze (javascript-typescript) (push) Has been cancelled
codeql / Analyze (python) (push) Has been cancelled
perf / API hot-path micro-smoke (budgeted) (push) Has been cancelled
perf / Desktop engine crates (release build check) (push) Has been cancelled
vision2026-ci / API phase modules (ruff + pytest) (push) Has been cancelled
vision2026-ci / Alembic upgrade + drift check (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (caption-engine) (push) Has been cancelled
Menivor — open source release (MIT), free to use and fork
Paid, hosted product at menivor.com, now retired as a company project.
Fresh squashed history of the cleaned working tree (secret-scanned,
no .env, no customer data, no outreach logs). Built on
MoneyPrinterTurbo (MIT, harry0703) — see NOTICE.
2026-12-04 02:10:26 +03:00

43 lines
1.5 KiB
Python

"""Lifecycle management for durable ``POST /jobs`` idempotency records.
The request rows must survive transient client retries, yet need no indefinite
retention: the authoritative job and immutable credit ledger retain operational
and financial history. This module is deliberately small so Celery beat can
run it without importing the API controller.
"""
from __future__ import annotations
from datetime import UTC, datetime, timedelta
from app.config import config
from app.models.db.base import get_session
from app.models.db.repositories import JobRequestRepository
_DEFAULT_RETENTION_HOURS = 24
_DEFAULT_SWEEP_LIMIT = 1_000
def _retention_hours() -> int:
"""Return a safe, bounded replay window from application configuration."""
try:
configured = int(
config.app.get(
"reelforge_idempotency_retention_hours", _DEFAULT_RETENTION_HOURS
)
)
except (TypeError, ValueError):
return _DEFAULT_RETENTION_HOURS
return max(1, min(configured, 24 * 31))
def sweep_expired_job_requests(
*, now: datetime | None = None, limit: int = _DEFAULT_SWEEP_LIMIT
) -> dict[str, int]:
"""Delete at most ``limit`` idempotency rows older than the retry window."""
current = now or datetime.now(UTC)
cutoff = current - timedelta(hours=_retention_hours())
with get_session() as session:
deleted = JobRequestRepository(session).delete_created_before(cutoff, limit)
return {"deleted": deleted}