2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-04-30 20:46:47 +00:00

fix(backend): Make task registering more robust (#9586)

* Make task registering more robust
See https://github.com/inventree/InvenTree/issues/9579

* add unit test
This commit is contained in:
Matthias Mair 2025-04-29 13:15:26 +02:00 committed by GitHub
parent 13b543e128
commit 9f0354b315
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 22 additions and 4 deletions

View File

@ -180,7 +180,15 @@ class ScheduleMixin:
obj['func'] = 'plugin.registry.call_plugin_function' obj['func'] = 'plugin.registry.call_plugin_function'
obj['args'] = f"'{slug}', '{func_name}'" 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! # Scheduled task already exists - update it!
logger.info("Updating scheduled task '%s'", task_name) logger.info("Updating scheduled task '%s'", task_name)
instance = Schedule.objects.get(name=task_name) instance = Schedule.objects.get(name=task_name)

View File

@ -2,10 +2,10 @@
from django.test import TestCase from django.test import TestCase
from plugin import InvenTreePlugin, registry from plugin import InvenTreePlugin
from plugin.helpers import MixinImplementationError from plugin.helpers import MixinImplementationError
from plugin.mixins import ScheduleMixin from plugin.mixins import ScheduleMixin
from plugin.registry import call_plugin_function from plugin.registry import call_plugin_function, registry
class ExampleScheduledTaskPluginTests(TestCase): class ExampleScheduledTaskPluginTests(TestCase):
@ -42,9 +42,19 @@ class ExampleScheduledTaskPluginTests(TestCase):
# test updating the schedule # test updating the schedule
hello_schedule = Schedule.objects.get(name='plugin.schedule.hello') hello_schedule = Schedule.objects.get(name='plugin.schedule.hello')
self.assertEqual(hello_schedule.minutes, 45) 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 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() 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 # Check that the schedule was updated
hello_schedule = Schedule.objects.get(name='plugin.schedule.hello') hello_schedule = Schedule.objects.get(name='plugin.schedule.hello')