mirror of
https://github.com/inventree/InvenTree.git
synced 2026-11-26 17:48:54 +00:00
[bug] Remove duplicate task (#13380)
- Removes duplicate copy of "delete_old_notifications" - Updated regression test - Delete legacy tasks
This commit is contained in:
1 parent
292defb03e
commit
a4a26dfb70
3 files changed
+42
-26
No files matched your search
@@ -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',
|
||||
|
||||
@@ -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():
|
||||
|
||||
@@ -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):
|
||||
|
||||
Reference in new issue
Block a user