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.tasks
from plugin.events import trigger_event
from part import models as PartModels
from stock import models as StockModels
from users import models as UserModels
@ -585,6 +587,13 @@ class Build(MPTTModel, ReferenceIndexingMixin):
# which point to thie Build Order
self.allocated_stock.all().delete()
# Register an event
trigger_event(
'build.completed',
build_id=self.pk,
user_id=user.pk,
)
@transaction.atomic
def cancelBuild(self, user):
""" Mark the Build as CANCELLED
@ -604,6 +613,12 @@ class Build(MPTTModel, ReferenceIndexingMixin):
self.status = BuildStatus.CANCELLED
self.save()
trigger_event(
'build.cancelled',
build_id=self.pk,
user_id=user.pk,
)
@transaction.atomic
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
InvenTree.tasks.offload_task('build.tasks.check_build_stock', instance)
trigger_event(
'build.created',
build_id=instance.pk,
)
class BuildOrderAttachment(InvenTreeAttachment):
"""

View File

@ -30,10 +30,29 @@ def trigger_event(event, *args, **kwargs):
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
if settings.PLUGIN_TESTING or InvenTreeSetting.get_setting('ENABLE_PLUGINS_EVENTS'):
with transaction.atomic():
for slug, plugin in plugin_registry.plugins.items():
if plugin.mixin_enabled('events'):
@ -44,6 +63,7 @@ def trigger_event(event, *args, **kwargs):
logger.debug(f"Registering callback for plugin '{slug}'")
# Offload a separate task for each plugin
offload_task(
'plugin.events.process_event',
slug,

View File

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