ui polish and importer logging tuning
This commit is contained in:
@@ -212,6 +212,11 @@ def import_media_file(self, task_id: int):
|
||||
# Add artist tag
|
||||
_add_artist_tag(record, artist)
|
||||
|
||||
# Add archive tag if this file came from an archive
|
||||
archive_tag_id = context.get('archive_tag_id')
|
||||
if archive_tag_id:
|
||||
_add_archive_tag(record, archive_tag_id)
|
||||
|
||||
db.session.add(record)
|
||||
db.session.commit()
|
||||
|
||||
@@ -515,6 +520,12 @@ def _supersede_existing(task: ImportTask, old_record: ImageRecord, src_path: str
|
||||
old_record, dest_path, thumb_path, file_hash, phash, metadata, filename
|
||||
)
|
||||
|
||||
# Add archive tag if this file came from an archive
|
||||
context = task.context or {}
|
||||
archive_tag_id = context.get('archive_tag_id')
|
||||
if archive_tag_id:
|
||||
_add_archive_tag(record, archive_tag_id)
|
||||
|
||||
db.session.commit()
|
||||
|
||||
# Complete task
|
||||
|
||||
@@ -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):
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user