From 8943dabc4dbc0b0e02cf079f4531de8a4e1905c7 Mon Sep 17 00:00:00 2001 From: Junyi Date: Wed, 4 Jan 2023 18:26:44 +0800 Subject: [PATCH] fix(plugin-workflow): fix events in prepare (#1325) --- packages/plugins/workflow/src/server/Plugin.ts | 8 +++++--- .../workflow/src/server/__tests__/Plugin.test.ts | 11 +++++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/packages/plugins/workflow/src/server/Plugin.ts b/packages/plugins/workflow/src/server/Plugin.ts index 55d4b0fac..11c4ec46a 100644 --- a/packages/plugins/workflow/src/server/Plugin.ts +++ b/packages/plugins/workflow/src/server/Plugin.ts @@ -158,7 +158,11 @@ export default class WorkflowPlugin extends Plugin { } private prepare = async () => { - const [workflow, context, options] = this.events[0]; + const event = this.events.shift(); + if (!event) { + return; + } + const [workflow, context, options] = event; if (options.context?.executionId) { // NOTE: no transaction here for read-uncommitted execution @@ -213,8 +217,6 @@ export default class WorkflowPlugin extends Plugin { this.pending.push([execution]); } - this.events.shift(); - if (this.events.length) { await this.prepare(); } else { diff --git a/packages/plugins/workflow/src/server/__tests__/Plugin.test.ts b/packages/plugins/workflow/src/server/__tests__/Plugin.test.ts index 710316beb..f814a0e78 100644 --- a/packages/plugins/workflow/src/server/__tests__/Plugin.test.ts +++ b/packages/plugins/workflow/src/server/__tests__/Plugin.test.ts @@ -215,6 +215,17 @@ describe('workflow > Plugin', () => { const [execution] = await workflow.getExecutions(); expect(execution.status).toBe(EXECUTION_STATUS.RESOLVED); + + // NOTE: second trigger to ensure no skipped event + const p3 = await PostRepo.create({ values: { title: 't2' } }); + + await sleep(500); + + const posts2 = await PostRepo.find(); + expect(posts2.length).toBe(4); + + const [execution2] = await workflow.getExecutions({ order: [['createdAt', 'DESC']] }); + expect(execution2.status).toBe(EXECUTION_STATUS.RESOLVED); }); });