From af8a2f14a5ecb8dd5f379b6e51d0ec11d6123c33 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Tue, 29 Apr 2025 12:36:55 +0100 Subject: [PATCH] fix(backend): Make task registering more robust (#9586) (#9605) * Make task registering more robust See https://github.com/inventree/InvenTree/issues/9579 * add unit test (cherry picked from commit 9f0354b3151e9c09a41eab579f1fa0d82f1b2bc8) Co-authored-by: Matthias Mair --- .../plugin/base/integration/ScheduleMixin.py | 10 +++++++++- .../samples/integration/test_scheduled_task.py | 16 +++++++++++++--- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/backend/InvenTree/plugin/base/integration/ScheduleMixin.py b/src/backend/InvenTree/plugin/base/integration/ScheduleMixin.py index c61e907e9c..f075d2bd56 100644 --- a/src/backend/InvenTree/plugin/base/integration/ScheduleMixin.py +++ b/src/backend/InvenTree/plugin/base/integration/ScheduleMixin.py @@ -179,7 +179,15 @@ class ScheduleMixin: obj['func'] = 'plugin.registry.call_plugin_function' obj['args'] = f"'{slug}', '{func_name}'" - if Schedule.objects.filter(name=task_name).exists(): + tasks = Schedule.objects.filter(name=task_name) + if len(tasks) > 1: + logger.info( + "Found multiple tasks; Adding a new scheduled task '%s'", + task_name, + ) + tasks.delete() + Schedule.objects.create(**obj) + elif len(tasks) == 1: # Scheduled task already exists - update it! logger.info("Updating scheduled task '%s'", task_name) instance = Schedule.objects.get(name=task_name) diff --git a/src/backend/InvenTree/plugin/samples/integration/test_scheduled_task.py b/src/backend/InvenTree/plugin/samples/integration/test_scheduled_task.py index 2b9d254456..2d5bc9c12c 100644 --- a/src/backend/InvenTree/plugin/samples/integration/test_scheduled_task.py +++ b/src/backend/InvenTree/plugin/samples/integration/test_scheduled_task.py @@ -2,10 +2,10 @@ from django.test import TestCase -from plugin import InvenTreePlugin, registry +from plugin import InvenTreePlugin from plugin.helpers import MixinImplementationError from plugin.mixins import ScheduleMixin -from plugin.registry import call_plugin_function +from plugin.registry import call_plugin_function, registry class ExampleScheduledTaskPluginTests(TestCase): @@ -42,9 +42,19 @@ class ExampleScheduledTaskPluginTests(TestCase): # test updating the schedule hello_schedule = Schedule.objects.get(name='plugin.schedule.hello') self.assertEqual(hello_schedule.minutes, 45) - # change the schedule and reregister + # change the schedule and reregister -> the interval should be preserved plg.scheduled_tasks['hello']['minutes'] = 15 + # add a doubly scheduled task - this should be removed + Schedule.objects.create(name='plugin.schedule.hello') + self.assertEqual( + Schedule.objects.filter(name='plugin.schedule.hello').count(), 2 + ) + plg.register_tasks() + # The duplicate task should be removed + self.assertEqual( + Schedule.objects.filter(name='plugin.schedule.hello').count(), 1 + ) # Check that the schedule was updated hello_schedule = Schedule.objects.get(name='plugin.schedule.hello')