tasks.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. from datetime import timedelta
  2. from celery import shared_task
  3. from django.conf import settings
  4. from django.db import connection
  5. from django.utils.timezone import now
  6. from events.models import Event
  7. from glitchtip.debounced_celery_task import debounced_task, debounced_wrap
  8. from .models import Issue
  9. @shared_task
  10. def cleanup_old_events():
  11. """Delete older events and associated data"""
  12. days = settings.GLITCHTIP_MAX_EVENT_LIFE_DAYS
  13. qs = Event.objects.filter(created__lt=now() - timedelta(days=days))
  14. # Fast bulk delete - see https://code.djangoproject.com/ticket/9519
  15. qs._raw_delete(qs.db)
  16. # Delete ~1k empty issues at a time until less than 1k remain then delete the rest. Avoids memory overload.
  17. queryset = Issue.objects.filter(event=None).order_by("id")
  18. while True:
  19. try:
  20. empty_issue_delimiter = queryset.values_list("id", flat=True)[
  21. 1000:1001
  22. ].get()
  23. queryset.filter(id__lte=empty_issue_delimiter).delete()
  24. except Issue.DoesNotExist:
  25. break
  26. queryset.delete()
  27. @shared_task
  28. def update_search_index_all_issues():
  29. """Very slow, force reindex of all issues"""
  30. for issue_pk in Issue.objects.all().values_list("pk", flat=True):
  31. Issue.update_index(issue_pk)
  32. @debounced_task(lambda x, *a, **k: x)
  33. @shared_task
  34. @debounced_wrap
  35. def update_search_index_issue(issue_id: int):
  36. """
  37. Debounced task to update one issue's search index/tags.
  38. Useful for mitigating excessive DB updates on rapidly recurring issues.
  39. Usage: update_search_index_issue(args=[issue_id], countdown=10)
  40. """
  41. Issue.update_index(issue_id)
  42. @shared_task
  43. def reindex_issues_model():
  44. """
  45. The GIN index on the issues table grows indefinitely, it needs reindexed regularly
  46. """
  47. with connection.cursor() as cursor:
  48. cursor.execute("REINDEX TABLE CONCURRENTLY issues_issue")