diff --git a/packages/plugins/workflow/src/server/Plugin.ts b/packages/plugins/workflow/src/server/Plugin.ts index a315e1b59..c9f867047 100644 --- a/packages/plugins/workflow/src/server/Plugin.ts +++ b/packages/plugins/workflow/src/server/Plugin.ts @@ -100,6 +100,9 @@ export default class WorkflowPlugin extends Plugin { workflows.forEach((workflow: WorkflowModel) => { this.toggle(workflow); }); + + // check for not started executions + await this.dispatch(); }); this.app.on('beforeStop', async () => { diff --git a/packages/plugins/workflow/src/server/__tests__/Plugin.test.ts b/packages/plugins/workflow/src/server/__tests__/Plugin.test.ts index 2606fab0a..efce0dbb1 100644 --- a/packages/plugins/workflow/src/server/__tests__/Plugin.test.ts +++ b/packages/plugins/workflow/src/server/__tests__/Plugin.test.ts @@ -1,6 +1,7 @@ import { MockServer } from '@nocobase/test'; import Database from '@nocobase/database'; import { getApp, sleep } from '.'; +import { EXECUTION_STATUS } from '../constants'; @@ -181,4 +182,70 @@ describe('workflow > Plugin', () => { expect(c2).toBe(1); }); }); + + describe('dispatcher', () => { + it('multiple triggers in same event', async () => { + const w1 = await WorkflowModel.create({ + enabled: true, + type: 'collection', + config: { + mode: 1, + collection: 'posts' + } + }); + + const w2 = await WorkflowModel.create({ + enabled: true, + type: 'collection', + config: { + mode: 1, + collection: 'posts' + } + }); + + const p1 = await PostRepo.create({ values: { title: 't1' } }); + + await sleep(500); + + const [e1] = await w1.getExecutions(); + expect(e1.status).toBe(EXECUTION_STATUS.RESOLVED); + + const [e2] = await w2.getExecutions(); + expect(e2.status).toBe(EXECUTION_STATUS.RESOLVED); + }); + + it('when server starts, process all created executions', async () => { + const w1 = await WorkflowModel.create({ + enabled: true, + type: 'collection', + config: { + mode: 1, + collection: 'posts' + } + }); + + await app.stop(); + + await db.reconnect(); + + const p1 = await PostRepo.create({ values: { title: 't1' } }); + + const ExecutionModel = db.getCollection('executions').model; + const e1 = await ExecutionModel.create({ + workflowId: w1.id, + key: w1.key, + useTransaction: w1.useTransaction, + context: { + data: p1.get() + } + }); + + await app.start(); + + await sleep(500); + + const [e2] = await w1.getExecutions(); + expect(e2.status).toBe(EXECUTION_STATUS.RESOLVED); + }); + }); }); diff --git a/packages/plugins/workflow/src/server/collections/executions.ts b/packages/plugins/workflow/src/server/collections/executions.ts index 95d9b9527..1f6a7eca5 100644 --- a/packages/plugins/workflow/src/server/collections/executions.ts +++ b/packages/plugins/workflow/src/server/collections/executions.ts @@ -16,6 +16,7 @@ export default { name: 'useTransaction', defaultValue: false }, + // @deprecated { type: 'uuid', name: 'transaction',