ui polish and importer logging tuning

This commit is contained in:
Bryan Van Deusen
2026-01-28 09:36:59 -05:00
parent 0bf1961175
commit d0fcde38e8
12 changed files with 110 additions and 133 deletions
+58
View File
@@ -302,6 +302,64 @@ def recover_interrupted_tasks():
return {'recovered': recovered}
@celery.task(name='app.tasks.scan.cleanup_old_tasks')
def cleanup_old_tasks(days: int = 7):
"""
Clean up old failed and skipped import task records.
Called periodically by Celery Beat to prevent database bloat.
Deletes ImportTask records with status 'failed' or 'skipped'
that are older than the specified number of days.
Also cleans up orphaned ImportBatch records that have no tasks.
Args:
days: Number of days to retain failed/skipped tasks (default: 7)
Returns:
dict with counts of deleted records
"""
from datetime import timedelta
cutoff = datetime.utcnow() - timedelta(days=days)
log.info(f"Cleaning up failed/skipped tasks older than {days} days (before {cutoff})")
# Delete old failed tasks
failed_deleted = ImportTask.query.filter(
ImportTask.status == 'failed',
ImportTask.completed_at < cutoff
).delete(synchronize_session=False)
# Delete old skipped tasks
skipped_deleted = ImportTask.query.filter(
ImportTask.status == 'skipped',
ImportTask.completed_at < cutoff
).delete(synchronize_session=False)
db.session.commit()
# Clean up orphaned batches (batches with no tasks)
orphaned_batches = ImportBatch.query.filter(
~ImportBatch.tasks.any()
).all()
batches_deleted = len(orphaned_batches)
for batch in orphaned_batches:
db.session.delete(batch)
db.session.commit()
if failed_deleted or skipped_deleted or batches_deleted:
log.info(f"Cleanup complete: {failed_deleted} failed tasks, "
f"{skipped_deleted} skipped tasks, {batches_deleted} orphaned batches deleted")
return {
'failed_deleted': failed_deleted,
'skipped_deleted': skipped_deleted,
'batches_deleted': batches_deleted
}
@celery.task(name='app.tasks.scan.update_batch_stats')
def update_batch_stats(batch_id: int):
"""