From c4acdebcaa3e31518e2131e8bfb520e75d7afd27 Mon Sep 17 00:00:00 2001 From: Junyi Date: Tue, 6 Aug 2024 08:29:37 +0800 Subject: [PATCH] fix(plugin-workflow): fix collection trigger in async mode after transaction committed (#4994) --- .../plugin-workflow/src/server/Processor.ts | 2 +- .../__tests__/triggers/collection.test.ts | 44 ++++++++++++++++++- .../src/server/triggers/CollectionTrigger.ts | 8 +++- 3 files changed, 51 insertions(+), 3 deletions(-) diff --git a/packages/plugins/@nocobase/plugin-workflow/src/server/Processor.ts b/packages/plugins/@nocobase/plugin-workflow/src/server/Processor.ts index 6005477d3a..a09a0d6d78 100644 --- a/packages/plugins/@nocobase/plugin-workflow/src/server/Processor.ts +++ b/packages/plugins/@nocobase/plugin-workflow/src/server/Processor.ts @@ -161,7 +161,7 @@ export default class Processor { // for uncaught error, set to error this.logger.error( `execution (${this.execution.id}) run instruction [${node.type}] for node (${node.id}) failed: `, - { error: err }, + err, ); job = { result: diff --git a/packages/plugins/@nocobase/plugin-workflow/src/server/__tests__/triggers/collection.test.ts b/packages/plugins/@nocobase/plugin-workflow/src/server/__tests__/triggers/collection.test.ts index 992e0caf8c..d9ce532026 100644 --- a/packages/plugins/@nocobase/plugin-workflow/src/server/__tests__/triggers/collection.test.ts +++ b/packages/plugins/@nocobase/plugin-workflow/src/server/__tests__/triggers/collection.test.ts @@ -7,7 +7,7 @@ * For more information, please refer to: https://www.nocobase.com/agreement. */ -import { BelongsToRepository, MockDatabase } from '@nocobase/database'; +import { BelongsToRepository, MockDatabase, Op } from '@nocobase/database'; import { getApp, sleep } from '@nocobase/plugin-workflow-test'; import { MockServer } from '@nocobase/test'; @@ -652,6 +652,48 @@ describe('workflow > triggers > collection', () => { }); }); + describe('transaction', () => { + it('should trigger after transaction committed', async () => { + const workflow = await WorkflowModel.create({ + enabled: true, + type: 'collection', + config: { + mode: 1, + collection: 'posts', + }, + }); + + await workflow.createNode({ + type: 'destroy', + config: { + collection: 'posts', + params: { + filter: { + id: { + $not: null, + }, + }, + }, + }, + }); + + await db.sequelize.transaction(async (transaction) => { + const p1 = await PostRepo.create({ values: { title: 't1' }, transaction }); + await sleep(50); + const p2 = await PostRepo.create({ values: { title: 't2' }, transaction }); + await sleep(50); + }); + + await sleep(500); + + const executions = await workflow.getExecutions(); + expect(executions.length).toBe(2); + + const posts = await PostRepo.find(); + expect(posts.length).toBe(0); + }); + }); + describe('cycling trigger', () => { it('trigger should not be triggered more than once in same execution', async () => { const workflow = await WorkflowModel.create({ diff --git a/packages/plugins/@nocobase/plugin-workflow/src/server/triggers/CollectionTrigger.ts b/packages/plugins/@nocobase/plugin-workflow/src/server/triggers/CollectionTrigger.ts index 0d1f08c4ad..385ddf7591 100644 --- a/packages/plugins/@nocobase/plugin-workflow/src/server/triggers/CollectionTrigger.ts +++ b/packages/plugins/@nocobase/plugin-workflow/src/server/triggers/CollectionTrigger.ts @@ -113,7 +113,13 @@ async function handler(this: CollectionTrigger, workflow: WorkflowModel, data: M }, ); } else { - this.workflow.trigger(workflow, { data: json, stack: context?.stack }); + if (transaction) { + transaction.afterCommit(() => { + this.workflow.trigger(workflow, { data: json, stack: context?.stack }); + }); + } else { + this.workflow.trigger(workflow, { data: json, stack: context?.stack }); + } } }