tasks.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. while True:
  28. file_blobs = (
  29. FileBlob.objects.filter(created__lt=days_ago)
  30. .exclude(file__created__gte=days_ago)
  31. .exclude(file__releasefile__release__projects__issue__created__gte=days_ago)
  32. ).only("id", "blob")[:1000]
  33. ids = []
  34. for file_blob in file_blobs:
  35. ids.append(file_blob.id)
  36. file_blob.blob.delete() # Delete actual file
  37. if ids:
  38. FileBlob.objects.filter(id__in=ids).delete()
  39. else:
  40. break