2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-04-30 12:36:45 +00:00

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 <code@mjmair.com>
This commit is contained in:
github-actions[bot] 2025-04-29 12:36:55 +01:00 committed by GitHub
parent 7090950066
commit af8a2f14a5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 22 additions and 4 deletions

View File

@ -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)

View File

@ -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')