diff --git a/src/backend/InvenTree/InvenTree/apps.py b/src/backend/InvenTree/InvenTree/apps.py index 5aa8afa77b..93f22aeed4 100644 --- a/src/backend/InvenTree/InvenTree/apps.py +++ b/src/backend/InvenTree/InvenTree/apps.py @@ -120,6 +120,7 @@ class InvenTreeConfig(AppConfig): def remove_obsolete_tasks(self): """Delete any obsolete scheduled tasks in the database.""" obsolete = [ + 'common.tasks.delete_old_notifications', 'data_exporter.tasks.cleanup_old_export_outputs', 'InvenTree.tasks.delete_expired_sessions', 'label.tasks.cleanup_old_label_outputs', diff --git a/src/backend/InvenTree/common/tasks.py b/src/backend/InvenTree/common/tasks.py index 2a81c089f0..003d6b694a 100644 --- a/src/backend/InvenTree/common/tasks.py +++ b/src/backend/InvenTree/common/tasks.py @@ -6,7 +6,6 @@ from django.conf import settings from django.core.exceptions import AppRegistryNotReady from django.core.files.storage import default_storage from django.db.utils import IntegrityError, OperationalError -from django.utils import timezone import feedparser import requests @@ -33,27 +32,6 @@ def cleanup_old_data_outputs(): output.delete() -@tracer.start_as_current_span('delete_old_notifications') -@scheduled_task(ScheduledTask.DAILY) -def delete_old_notifications(): - """Remove old notifications from the database. - - Anything older than ~3 months is removed - """ - try: - from common.models import NotificationEntry - except AppRegistryNotReady: # pragma: no cover - logger.info( - "Could not perform 'delete_old_notifications' - App registry not ready" - ) - return - - before = timezone.now() - timedelta(days=90) - - # Delete notification records before the specified date - NotificationEntry.objects.filter(updated__lte=before).delete() - - @tracer.start_as_current_span('update_news_feed') @scheduled_task(ScheduledTask.DAILY) def update_news_feed(): diff --git a/src/backend/InvenTree/common/test_tasks.py b/src/backend/InvenTree/common/test_tasks.py index 9335c39928..ca1d09603d 100644 --- a/src/backend/InvenTree/common/test_tasks.py +++ b/src/backend/InvenTree/common/test_tasks.py @@ -1,13 +1,16 @@ """Tests for tasks in app common.""" +from datetime import timedelta from unittest.mock import patch from django.conf import settings from django.test import TestCase +from django.utils import timezone import requests from common.models import NewsFeedEntry, NotificationEntry +from common.settings import set_global_setting from InvenTree.tasks import offload_task from . import tasks as common_tasks @@ -16,11 +19,45 @@ from . import tasks as common_tasks class TaskTest(TestCase): """Tests for common tasks.""" - def test_delete(self): - """Test that the task `delete_old_notifications` runs through without errors.""" - # check empty run + def test_delete_old_notifications(self): + """Test that old notification entries are deleted according to the setting. + + Previously, a second task in common.tasks deleted entries after a + hard-coded 90 days, silently capping INVENTREE_DELETE_NOTIFICATIONS_DAYS. + """ + from InvenTree.tasks import delete_old_notifications + + # The duplicate task has been removed + self.assertFalse(hasattr(common_tasks, 'delete_old_notifications')) + + # Empty run self.assertEqual(NotificationEntry.objects.all().count(), 0) - offload_task(common_tasks.delete_old_notifications) + offload_task(delete_old_notifications) + + now = timezone.now() + + for key, age in [('recent', 10), ('medium', 100), ('old', 200)]: + NotificationEntry.objects.create(key=key, uid=1) + NotificationEntry.objects.filter(key=key).update( + updated=now - timedelta(days=age) + ) + + # Retention period longer than 90 days is respected + set_global_setting('INVENTREE_DELETE_NOTIFICATIONS_DAYS', 150) + offload_task(delete_old_notifications) + + self.assertEqual( + set(NotificationEntry.objects.values_list('key', flat=True)), + {'recent', 'medium'}, + ) + + # Shorter retention period + set_global_setting('INVENTREE_DELETE_NOTIFICATIONS_DAYS', 30) + offload_task(delete_old_notifications) + + self.assertEqual( + list(NotificationEntry.objects.values_list('key', flat=True)), ['recent'] + ) class NewsFeedTests(TestCase):