2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-05-02 05:26:45 +00:00

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
This commit is contained in:
Oliver 2023-07-04 23:26:19 +10:00 committed by GitHub
parent 4c3dc6ddbd
commit 7ba26ebfbb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 1 deletions

View File

@ -20,17 +20,27 @@ def trigger_event(event, *args, **kwargs):
This event will be stored in the database, This event will be stored in the database,
and the worker will respond to it later on. and the worker will respond to it later on.
""" """
from common.models import InvenTreeSetting
if not settings.PLUGINS_ENABLED: if not settings.PLUGINS_ENABLED:
# Do nothing if plugins are not enabled # Do nothing if plugins are not enabled
return # pragma: no cover 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 # 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") logger.debug(f"Ignoring triggered event '{event}' - database not ready")
return return
logger.debug(f"Event triggered: '{event}'") 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( offload_task(
register_event, register_event,
event, event,
@ -63,6 +73,11 @@ def register_event(event, *args, **kwargs):
logger.debug(f"Registering callback for plugin '{slug}'") 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 a separate task for each plugin
offload_task( offload_task(
process_event, process_event,

View File

@ -3,6 +3,7 @@
from django.conf import settings from django.conf import settings
from django.test import TestCase from django.test import TestCase
from common.models import InvenTreeSetting
from plugin import InvenTreePlugin, registry from plugin import InvenTreePlugin, registry
from plugin.base.event.events import trigger_event from plugin.base.event.events import trigger_event
from plugin.helpers import MixinNotImplementedError from plugin.helpers import MixinNotImplementedError
@ -21,6 +22,8 @@ class EventPluginSampleTests(TestCase):
config.active = True config.active = True
config.save() config.save()
InvenTreeSetting.set_setting('ENABLE_PLUGINS_EVENTS', True, change_user=None)
# Enable event testing # Enable event testing
settings.PLUGIN_TESTING_EVENTS = True settings.PLUGIN_TESTING_EVENTS = True
# Check that an event is issued # Check that an event is issued