From 7ba26ebfbbaece93ac078de5d42a19c13777defa Mon Sep 17 00:00:00 2001 From: Oliver Date: Tue, 4 Jul 2023 23:26:19 +1000 Subject: [PATCH] Task improvements (#5159) * Ignore triggered events if plugin events are not enabled * Ensure that plugin tasks are handled by the background worker * Allow shell access * Don't force async if testing * Enable plugin events as part of CI * fix --- InvenTree/plugin/base/event/events.py | 17 ++++++++++++++++- .../plugin/samples/event/test_event_sample.py | 3 +++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/InvenTree/plugin/base/event/events.py b/InvenTree/plugin/base/event/events.py index f2b28a9c3e..96a91fcec0 100644 --- a/InvenTree/plugin/base/event/events.py +++ b/InvenTree/plugin/base/event/events.py @@ -20,17 +20,27 @@ def trigger_event(event, *args, **kwargs): This event will be stored in the database, and the worker will respond to it later on. """ + from common.models import InvenTreeSetting + if not settings.PLUGINS_ENABLED: # Do nothing if plugins are not enabled return # pragma: no cover + if not InvenTreeSetting.get_setting('ENABLE_PLUGINS_EVENTS', False): + # Do nothing if plugin events are not enabled + return + # Make sure the database can be accessed and is not being tested rn - if not canAppAccessDatabase() and not settings.PLUGIN_TESTING_EVENTS: + if not canAppAccessDatabase(allow_shell=True) and not settings.PLUGIN_TESTING_EVENTS: logger.debug(f"Ignoring triggered event '{event}' - database not ready") return logger.debug(f"Event triggered: '{event}'") + # By default, force the event to be processed asynchronously + if 'force_async' not in kwargs and not settings.PLUGIN_TESTING_EVENTS: + kwargs['force_async'] = True + offload_task( register_event, event, @@ -63,6 +73,11 @@ def register_event(event, *args, **kwargs): logger.debug(f"Registering callback for plugin '{slug}'") + # This task *must* be processed by the background worker, + # unless we are running CI tests + if 'force_async' not in kwargs and not settings.PLUGIN_TESTING_EVENTS: + kwargs['force_async'] = True + # Offload a separate task for each plugin offload_task( process_event, diff --git a/InvenTree/plugin/samples/event/test_event_sample.py b/InvenTree/plugin/samples/event/test_event_sample.py index 9085117716..e466aaa0eb 100644 --- a/InvenTree/plugin/samples/event/test_event_sample.py +++ b/InvenTree/plugin/samples/event/test_event_sample.py @@ -3,6 +3,7 @@ from django.conf import settings from django.test import TestCase +from common.models import InvenTreeSetting from plugin import InvenTreePlugin, registry from plugin.base.event.events import trigger_event from plugin.helpers import MixinNotImplementedError @@ -21,6 +22,8 @@ class EventPluginSampleTests(TestCase): config.active = True config.save() + InvenTreeSetting.set_setting('ENABLE_PLUGINS_EVENTS', True, change_user=None) + # Enable event testing settings.PLUGIN_TESTING_EVENTS = True # Check that an event is issued