fc3d: cleanup_old_download_events daily task (terminal-only, configurable retention)
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -11,7 +11,7 @@ from sqlalchemy.orm import sessionmaker
|
||||
|
||||
from ..celery_app import celery
|
||||
from ..config import get_config
|
||||
from ..models import ImageRecord, ImportTask
|
||||
from ..models import DownloadEvent, ImageRecord, ImportSettings, ImportTask
|
||||
from ..utils.phash import compute_phash
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
@@ -196,3 +196,29 @@ def verify_integrity() -> int:
|
||||
last_id = rows[-1].id
|
||||
log.info("verify_integrity verdicts: %s (total %d)", counts, total)
|
||||
return total
|
||||
|
||||
|
||||
@celery.task(name="backend.app.tasks.maintenance.cleanup_old_download_events")
|
||||
def cleanup_old_download_events() -> int:
|
||||
"""FC-3d: delete terminal DownloadEvent rows older than the configured
|
||||
retention window. Never touches pending/running rows.
|
||||
|
||||
Why terminal-only: pending/running rows represent in-flight work whose
|
||||
owning task may still be alive; deleting them would orphan the task.
|
||||
Retention days comes from ImportSettings.download_event_retention_days
|
||||
so the operator can tune without a code change.
|
||||
"""
|
||||
SessionLocal = _sync_session_factory()
|
||||
with SessionLocal() as session:
|
||||
settings = session.execute(
|
||||
select(ImportSettings).where(ImportSettings.id == 1)
|
||||
).scalar_one()
|
||||
retention_days = settings.download_event_retention_days
|
||||
cutoff = datetime.now(UTC) - timedelta(days=retention_days)
|
||||
result = session.execute(
|
||||
delete(DownloadEvent)
|
||||
.where(DownloadEvent.status.in_(["ok", "error", "skipped"]))
|
||||
.where(DownloadEvent.started_at < cutoff)
|
||||
)
|
||||
session.commit()
|
||||
return result.rowcount or 0
|
||||
|
||||
Reference in New Issue
Block a user