tasks.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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 apps.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. def cleanup_old_files():
  16. """
  17. Delete files in both the database and media storage
  18. Deletion only occurs when the creation date of the following are older than max file life days
  19. - FileBlob
  20. - Any related File objects
  21. - Any related events attached to the project, release, release file
  22. This operation has minor risk of deleting a file that is still desired,
  23. if it hasn't been used for a long time and have no recent event data
  24. """
  25. days_ago = now() - timedelta(days=settings.GLITCHTIP_MAX_FILE_LIFE_DAYS)
  26. while True:
  27. file_blobs = (
  28. FileBlob.objects.filter(created__lt=days_ago)
  29. .exclude(file__created__gte=days_ago)
  30. .exclude(
  31. file__releasefile__release__projects__issues__last_seen__gte=days_ago
  32. )
  33. ).only("id", "blob")[:1000]
  34. ids = []
  35. for file_blob in file_blobs:
  36. ids.append(file_blob.id)
  37. file_blob.blob.delete() # Delete actual file
  38. if ids:
  39. FileBlob.objects.filter(id__in=ids).delete()
  40. else:
  41. break