Skip to content

Commit

Permalink
Add task to clean up orphaned uploads (#3793)
Browse files Browse the repository at this point in the history
  • Loading branch information
DeD1rk committed Sep 10, 2024
1 parent 94c95bb commit 8deaf1a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
28 changes: 27 additions & 1 deletion website/photos/tasks.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import logging
import os

from django.db import transaction
from django.dispatch import Signal
from django.utils import timezone

from celery import shared_task
from django_drf_filepond.models import TemporaryUpload
from django_drf_filepond.models import TemporaryUpload, TemporaryUploadChunked
from django_drf_filepond.models import storage as filepond_storage

from mailinglists.services import get_member_email_addresses
from members.models.member import Member
Expand Down Expand Up @@ -65,3 +68,26 @@ def process_album_upload(
finally:
archive.delete()
upload.delete()


@shared_task
def clean_broken_uploads():
# Cancel and remove completed uploads that are older than 12 hours.
for upload in TemporaryUpload.objects.filter(
uploaded__lte=timezone.now() - timezone.timedelta(hours=12)
):
logger.info(f"Removing old upload {upload.upload_id}")
upload.file.delete()
upload.delete()

# Cancel and remove uploads that have not received new chunks in the last hour.
for tuc in TemporaryUploadChunked.objects.filter(
last_upload_time__lt=timezone.now() - timezone.timedelta(hours=1),
).exclude(upload_id__in=TemporaryUpload.objects.values("upload_id")):
logger.info(f"Removing incomplete chunked upload {tuc.upload_id}")
# Store the chunk and check if we've now completed the upload.
upload_file = os.path.join(
tuc.upload_dir, f"{tuc.file_id}_{tuc.last_chunk + 1}"
)
filepond_storage.delete(upload_file)
tuc.delete()
4 changes: 4 additions & 0 deletions website/thaliawebsite/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,10 @@ def show_toolbar(request):
"task": "promotion.tasks.promo_update_overview_daily",
"schedule": crontab(minute=0, hour=8),
},
"cleanupfileponduploads": {
"task": "photos.tasks.clean_broken_uploads",
"schedule": crontab(minute=45),
},
}

###############################################################################
Expand Down

0 comments on commit 8deaf1a

Please sign in to comment.