tasks.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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 events.models import Event
  6. from glitchtip.debounced_celery_task import debounced_task, debounced_wrap
  7. from .models import Issue
  8. @shared_task
  9. def cleanup_old_events():
  10. """Delete older events and associated data"""
  11. days = settings.GLITCHTIP_MAX_EVENT_LIFE_DAYS
  12. qs = Event.objects.filter(created__lt=now() - timedelta(days=days))
  13. # Fast bulk delete - see https://code.djangoproject.com/ticket/9519
  14. qs._raw_delete(qs.db)
  15. # Delete ~1k empty issues at a time until less than 1k remain then delete the rest. Avoids memory overload.
  16. queryset = Issue.objects.filter(event=None).order_by("id")
  17. while True:
  18. try:
  19. empty_issue_delimiter = queryset.values_list("id", flat=True)[
  20. 1000:1001
  21. ].get()
  22. queryset.filter(id__lte=empty_issue_delimiter).delete()
  23. except Issue.DoesNotExist:
  24. break
  25. queryset.delete()
  26. @shared_task
  27. def update_search_index_all_issues():
  28. """Very slow, force reindex of all issues"""
  29. for issue_pk in Issue.objects.all().values_list("pk", flat=True):
  30. Issue.update_index(issue_pk)
  31. @debounced_task(lambda x, *a, **k: x)
  32. @shared_task
  33. @debounced_wrap
  34. def update_search_index_issue(issue_id: int):
  35. """
  36. Debounced task to update one issue's search index/tags.
  37. Useful for mitigating excessive DB updates on rapidly recurring issues.
  38. Usage: update_search_index_issue(args=[issue_id], countdown=10)
  39. """
  40. Issue.update_index(issue_id)