mirror of
				https://github.com/inventree/InvenTree.git
				synced 2025-11-04 07:05:41 +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:
		@@ -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)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -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')
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user