Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

fix(trigger): sync plugin relationships on publish (#41756)

bucketbase26b5ee943a6334
diff --git a/api/events/event_handlers/sync_plugin_trigger_when_app_created.py b/api/events/event_handlers/sync_plugin_trigger_when_app_created.py
index 68be37dfdbc..cc6e851ad87 100644
--- a/api/events/event_handlers/sync_plugin_trigger_when_app_created.py
+++ b/api/events/event_handlers/sync_plugin_trigger_when_app_created.py
@@ -1,6 +1,6 @@
import logging
-from events.app_event import app_draft_workflow_was_synced
+from events.app_event import app_draft_workflow_was_synced, app_published_workflow_was_updated
from models.model import App, AppMode
from models.workflow import Workflow
from services.trigger.trigger_service import TriggerService
@@ -9,14 +9,26 @@ logger = logging.getLogger(__name__)
@app_draft_workflow_was_synced.connect
-def handle(sender, synced_draft_workflow: Workflow, **kwargs):
+@app_published_workflow_was_updated.connect
+def handle(
+ sender,
+ synced_draft_workflow: Workflow | None = None,
+ published_workflow: Workflow | None = None,
+ **kwargs,
+):
"""
- While creating a workflow or updating a workflow, we may need to sync
- its plugin trigger relationships in DB.
+ Sync plugin trigger relationships when a draft changes or is published.
+
+ The published workflow must be reconciled as well because production trigger
+ dispatch relies on these relationships, while debug dispatch does not.
"""
app: App = sender
if app.mode != AppMode.WORKFLOW.value:
# only handle workflow app, chatflow is not supported yet
return
- TriggerService.sync_plugin_trigger_relationships(app, synced_draft_workflow)
+ workflow = published_workflow if published_workflow is not None else synced_draft_workflow
+ if workflow is None:
+ return
+
+ TriggerService.sync_plugin_trigger_relationships(app, workflow)
diff --git a/api/tests/unit_tests/events/event_handlers/test_sync_plugin_trigger_when_app_created.py b/api/tests/unit_tests/events/event_handlers/test_sync_plugin_trigger_when_app_created.py
new file mode 100644
index 00000000000..a9d631cf7dd
--- /dev/null
+++ b/api/tests/unit_tests/events/event_handlers/test_sync_plugin_trigger_when_app_created.py
@@ -0,0 +1,31 @@
+from types import SimpleNamespace
+from typing import cast
+from unittest.mock import patch
+
+from events.event_handlers.sync_plugin_trigger_when_app_created import handle
+from models.model import AppMode
+from models.workflow import Workflow
+
+
+def test_syncs_plugin_trigger_relationships_from_published_workflow() -> None:
+ app = SimpleNamespace(mode=AppMode.WORKFLOW.value)
+ published_workflow = cast(Workflow, object())
+
+ with patch(
+ "events.event_handlers.sync_plugin_trigger_when_app_created.TriggerService.sync_plugin_trigger_relationships"
+ ) as sync_relationships:
+ handle(app, published_workflow=published_workflow)
+
+ sync_relationships.assert_called_once_with(app, published_workflow)
+
+
+def test_keeps_draft_workflow_relationship_sync() -> None:
+ app = SimpleNamespace(mode=AppMode.WORKFLOW.value)
+ draft_workflow = cast(Workflow, object())
+
+ with patch(
+ "events.event_handlers.sync_plugin_trigger_when_app_created.TriggerService.sync_plugin_trigger_relationships"
+ ) as sync_relationships:
+ handle(app, synced_draft_workflow=draft_workflow)
+
+ sync_relationships.assert_called_once_with(app, draft_workflow)