maintenance.py 742 B

1234567891011121314151617181920212223242526
  1. from datetime import timedelta
  2. from django.conf import settings
  3. from django.utils.timezone import now
  4. from .models import Issue
  5. def cleanup_old_issues():
  6. days = settings.GLITCHTIP_MAX_EVENT_LIFE_DAYS
  7. # Delete ~1k empty issues at a time until less than 1k remain then delete the rest. Avoids memory overload.
  8. queryset = Issue.objects.filter(
  9. issueevent=None, last_seen__lt=now() - timedelta(days=days)
  10. ).order_by("id")
  11. while True:
  12. try:
  13. empty_issue_delimiter = queryset.values_list("id", flat=True)[
  14. 1000:1001
  15. ].get()
  16. queryset.filter(id__lte=empty_issue_delimiter).delete()
  17. except Issue.DoesNotExist:
  18. break
  19. queryset.delete()