tasks.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. from datetime import timedelta
  2. from django.utils.timezone import now
  3. from django.conf import settings
  4. from celery import shared_task
  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. # Do not optimize Issue with raw_delete as it has FK references to it.
  16. Issue.objects.filter(event=None).delete()
  17. @shared_task
  18. def update_search_index_all_issues():
  19. """ Very slow, force reindex of all issues """
  20. for issue_pk in Issue.objects.all().values_list("pk", flat=True):
  21. Issue.update_index(issue_pk)
  22. @debounced_task(lambda x, *a, **k: x)
  23. @shared_task
  24. @debounced_wrap
  25. def update_search_index_issue(issue_id: int, skip_tags=False):
  26. """
  27. Debounced task to update one issue's search index/tags.
  28. Useful for mitigating excessive DB updates on rapidly recurring issues.
  29. Usage: update_search_index_issue(args=[issue_id], countdown=10)
  30. """
  31. Issue.update_index(issue_id, skip_tags)