2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-04-29 20:16:44 +00:00

Iterate through plugins in a separate background task

This commit is contained in:
Oliver 2022-01-09 22:58:59 +11:00
parent 3731d688c9
commit d765be8c73
4 changed files with 55 additions and 15 deletions

View File

@ -36,6 +36,8 @@ import InvenTree.fields
import InvenTree.helpers import InvenTree.helpers
import InvenTree.tasks import InvenTree.tasks
from plugin.events import trigger_event
from part import models as PartModels from part import models as PartModels
from stock import models as StockModels from stock import models as StockModels
from users import models as UserModels from users import models as UserModels
@ -585,6 +587,13 @@ class Build(MPTTModel, ReferenceIndexingMixin):
# which point to thie Build Order # which point to thie Build Order
self.allocated_stock.all().delete() self.allocated_stock.all().delete()
# Register an event
trigger_event(
'build.completed',
build_id=self.pk,
user_id=user.pk,
)
@transaction.atomic @transaction.atomic
def cancelBuild(self, user): def cancelBuild(self, user):
""" Mark the Build as CANCELLED """ Mark the Build as CANCELLED
@ -604,6 +613,12 @@ class Build(MPTTModel, ReferenceIndexingMixin):
self.status = BuildStatus.CANCELLED self.status = BuildStatus.CANCELLED
self.save() self.save()
trigger_event(
'build.cancelled',
build_id=self.pk,
user_id=user.pk,
)
@transaction.atomic @transaction.atomic
def unallocateStock(self, bom_item=None, output=None): def unallocateStock(self, bom_item=None, output=None):
""" """
@ -1042,6 +1057,11 @@ def after_save_build(sender, instance: Build, created: bool, **kwargs):
# Run checks on required parts # Run checks on required parts
InvenTree.tasks.offload_task('build.tasks.check_build_stock', instance) InvenTree.tasks.offload_task('build.tasks.check_build_stock', instance)
trigger_event(
'build.created',
build_id=instance.pk,
)
class BuildOrderAttachment(InvenTreeAttachment): class BuildOrderAttachment(InvenTreeAttachment):
""" """

View File

@ -191,7 +191,7 @@ class EventMixin:
def process_event(self, event, *args, **kwargs): def process_event(self, event, *args, **kwargs):
# Default implementation does not do anything # Default implementation does not do anything
raise NotImplementedError raise NotImplementedError
class MixinMeta: class MixinMeta:
MIXIN_NAME = 'Events' MIXIN_NAME = 'Events'

View File

@ -30,27 +30,47 @@ def trigger_event(event, *args, **kwargs):
logger.debug(f"Event triggered: '{event}'") logger.debug(f"Event triggered: '{event}'")
# Offload a separate task for each plugin offload_task(
'plugin.event.register_event',
event,
*args,
**kwargs
)
def register_event(event, *args, **kwargs):
"""
Register the event with any interested plugins.
Note: This function is processed by the background worker,
as it performs multiple database access operations.
"""
logger.debug(f"Registering triggered event: '{event}'")
# Determine if there are any plugins which are interested in responding # Determine if there are any plugins which are interested in responding
if settings.PLUGIN_TESTING or InvenTreeSetting.get_setting('ENABLE_PLUGINS_EVENTS'): if settings.PLUGIN_TESTING or InvenTreeSetting.get_setting('ENABLE_PLUGINS_EVENTS'):
for slug, plugin in plugin_registry.plugins.items(): with transaction.atomic():
if plugin.mixin_enabled('events'): for slug, plugin in plugin_registry.plugins.items():
config = plugin.plugin_config() if plugin.mixin_enabled('events'):
if config and config.active: config = plugin.plugin_config()
logger.debug(f"Registering callback for plugin '{slug}'") if config and config.active:
offload_task( logger.debug(f"Registering callback for plugin '{slug}'")
'plugin.events.process_event',
slug, # Offload a separate task for each plugin
event, offload_task(
*args, 'plugin.events.process_event',
**kwargs slug,
) event,
*args,
**kwargs
)
def process_event(plugin_slug, event, *args, **kwargs): def process_event(plugin_slug, event, *args, **kwargs):

View File

@ -178,7 +178,7 @@ class IntegrationPluginBase(MixinBase, plugin.InvenTreePlugin):
fnc_name = self._mixins.get(key) fnc_name = self._mixins.get(key)
# Allow for simple case where the mixin is "always" ready # Allow for simple case where the mixin is "always" ready
if fnc_name == True: if fnc_name is True:
return True return True
return getattr(self, fnc_name, True) return getattr(self, fnc_name, True)