Browse Source

Added cleanup data script

David Burke 4 years ago
parent
commit
c54b10ac17
4 changed files with 49 additions and 1 deletions
  1. 1 0
      alerts/tasks.py
  2. 12 1
      glitchtip/settings.py
  3. 13 0
      issues/tasks.py
  4. 23 0
      issues/tests/test_tasks.py

+ 1 - 0
alerts/tasks.py

@@ -8,6 +8,7 @@ from .models import Notification
 
 @shared_task
 def process_alerts():
+    """ Inspect alerts and determine if new notifications need sent """
     now = timezone.now()
     for project in Project.objects.all():
         for alert in project.projectalert_set.all():

+ 12 - 1
glitchtip/settings.py

@@ -15,6 +15,7 @@ import sys
 import environ
 import sentry_sdk
 from sentry_sdk.integrations.django import DjangoIntegration
+from celery.schedules import crontab
 
 env = environ.Env(
     ALLOWED_HOSTS=(list, ["*"]),
@@ -60,6 +61,9 @@ if POD_IP:
 # Used in email and DSN generation. Set to full domain such as https://glitchtip.example.com
 GLITCHTIP_DOMAIN = env.url("GLITCHTIP_DOMAIN", default="http://localhost:8000")
 
+# Events and associated data older than this will be deleted from the database
+GLITCHTIP_MAX_EVENT_LIFE_DAYS = env.int("GLITCHTIP_MAX_EVENT_LIFE_DAYS", default=90)
+
 # For development purposes only, prints out inbound event store json
 EVENT_STORE_DEBUG = env.bool("EVENT_STORE_DEBUG", False)
 
@@ -215,7 +219,14 @@ CELERY_BROKER_TRANSPORT_OPTIONS = {
 CELERY_RESULT_BACKEND = "django-db"
 CELERY_CACHE_BACKEND = "django-cache"
 CELERY_BEAT_SCHEDULE = {
-    "send-alert-notifications": {"task": "alerts.tasks.process_alerts", "schedule": 60,}
+    "send-alert-notifications": {
+        "task": "alerts.tasks.process_alerts",
+        "schedule": 60,
+    },
+    "cleanup-old-events": {
+        "task": "issues.tasks.cleanup_old_events",
+        "schedule": crontab(hour=7),
+    },
 }
 CACHES = {"default": {"BACKEND": "redis_cache.RedisCache", "LOCATION": REDIS_URL}}
 

+ 13 - 0
issues/tasks.py

@@ -0,0 +1,13 @@
+from datetime import timedelta
+from django.utils.timezone import now
+from django.conf import settings
+from celery import shared_task
+from .models import Event, Issue
+
+
+@shared_task
+def cleanup_old_events():
+    """ Delete older events and associated data  """
+    days = settings.GLITCHTIP_MAX_EVENT_LIFE_DAYS
+    Event.objects.filter(created__lt=now() - timedelta(days=days)).delete()
+    Issue.objects.filter(event=None).delete()

+ 23 - 0
issues/tests/test_tasks.py

@@ -0,0 +1,23 @@
+from datetime import timedelta
+from django.conf import settings
+from django.test import TestCase
+from django.utils.timezone import now
+from model_bakery import baker
+from freezegun import freeze_time
+from issues.models import Event, Issue
+from ..tasks import cleanup_old_events
+
+
+class TasksTestCase(TestCase):
+    def test_cleanup_old_events(self):
+        baker.make("issues.Event")
+        cleanup_old_events()
+        self.assertEqual(Event.objects.count(), 1)
+        self.assertEqual(Issue.objects.count(), 1)
+
+        with freeze_time(
+            now() + timedelta(days=settings.GLITCHTIP_MAX_EVENT_LIFE_DAYS)
+        ):
+            cleanup_old_events()
+            self.assertEqual(Event.objects.count(), 0)
+            self.assertEqual(Issue.objects.count(), 0)