tasks.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. from datetime import timedelta
  2. from celery import shared_task
  3. from django.conf import settings
  4. from django.utils.timezone import now
  5. from organizations_ext.models import Organization
  6. from .assemble import assemble_artifacts
  7. from .models import FileBlob
  8. @shared_task
  9. def assemble_artifacts_task(org_id, version, checksum, chunks, **kwargs):
  10. """
  11. Creates release files from an uploaded artifact bundle.
  12. """
  13. organization = Organization.objects.get(pk=org_id)
  14. assemble_artifacts(organization, version, checksum, chunks)
  15. @shared_task
  16. def cleanup_old_files():
  17. """
  18. Delete files in both the database and media storage
  19. Deletion only occurs when the creation date of the following are older than max file life days
  20. - FileBlob
  21. - Any related File objects
  22. - Any related events attached to the project, release, release file
  23. This operation has minor risk of deleting a file that is still desired,
  24. if it hasn't been used for a long time and have no recent event data
  25. """
  26. days_ago = now() - timedelta(days=settings.GLITCHTIP_MAX_FILE_LIFE_DAYS)
  27. for file_blob in (
  28. FileBlob.objects.filter(created__lt=days_ago)
  29. .exclude(file__created__gte=days_ago)
  30. .exclude(file__releasefile__release__projects__issue__created__gte=days_ago)
  31. ):
  32. file_blob.blob.delete() # Delete actual file
  33. file_blob.delete()