*** Wartungsfenster jeden ersten Mittwoch vormittag im Monat ***

Skip to content
Snippets Groups Projects
Commit 7ad96bae authored by Moser, Maximilian's avatar Moser, Maximilian
Browse files

Add background task for cleaning up dead files

* sometimes the cleanup operations for failed file uploads seem to leave
  over some `FileInstance` entries in the database that have a null URI
* these file instances cause the periodic health checks to fail and
  trigger warning emails
parent a7e057c7
No related branches found
No related tags found
1 merge request!83Add logic for assembling config items from their parts
......@@ -10,9 +10,12 @@
from typing import Optional
from celery import shared_task
from celery.schedules import crontab
from flask import current_app, url_for
from invenio_access.permissions import system_identity
from invenio_accounts.proxies import current_datastore
from invenio_db import db
from invenio_files_rest.models import FileInstance
from invenio_notifications.tasks import broadcast_notification
from invenio_rdm_records.proxies import current_rdm_records_service as records_service
......@@ -78,3 +81,27 @@ def send_publication_notification(recid: str, user_id: Optional[str] = None):
html_message=html_message,
)
broadcast_notification(notification.dumps())
@shared_task
def remove_dead_files():
"""Remove dead file instances (that don't have a URI) from the database.
These files seem to be leftovers from failed uploads that don't get cleaned up
properly.
"""
dead_file_instances = FileInstance.query.filter(FileInstance.uri.is_(None)).all()
for fi in dead_file_instances:
db.session.delete(fi)
for o in fi.objects:
db.session.delete(o)
db.session.commit()
CELERY_BEAT_SCHEDULE = {
"clean-dead-files": {
"task": "invenio_config_tuw.tasks.remove_dead_files",
"schedule": crontab(minute=1, hour=2),
},
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment