From 6fb58d01a3ad5c2c7a5c11482ee6099dff01a356 Mon Sep 17 00:00:00 2001 From: Junyi Date: Sun, 4 Dec 2022 17:38:37 -0800 Subject: [PATCH] test(plugin-workflow): add cache and test for sqlite (#1194) --- packages/plugins/workflow/src/server/Plugin.ts | 15 ++++++++++----- .../workflow/src/server/__tests__/Plugin.test.ts | 2 +- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/packages/plugins/workflow/src/server/Plugin.ts b/packages/plugins/workflow/src/server/Plugin.ts index a6b66b47f..b5f6bf4e6 100644 --- a/packages/plugins/workflow/src/server/Plugin.ts +++ b/packages/plugins/workflow/src/server/Plugin.ts @@ -22,7 +22,7 @@ export default class WorkflowPlugin extends Plugin { triggers: Registry = new Registry(); calculators = calculators; extensions = extensions; - executing: ExecutionModel = null; + executing: ExecutionModel | null = null; pending: Pending[] = []; events: [WorkflowModel, any, { context?: any }][] = []; @@ -133,7 +133,7 @@ export default class WorkflowPlugin extends Plugin { } } - public trigger(workflow: WorkflowModel, context: Object, options: { context?: any } = {}): Promise { + public trigger(workflow: WorkflowModel, context: Object, options: { context?: any } = {}): void { // `null` means not to trigger if (context == null) { return; @@ -166,7 +166,7 @@ export default class WorkflowPlugin extends Plugin { } } - await this.db.sequelize.transaction(async transaction => { + const execution = await this.db.sequelize.transaction(async transaction => { const execution = await workflow.createExecution({ context, key: workflow.key, @@ -200,6 +200,11 @@ export default class WorkflowPlugin extends Plugin { return execution; }); + // NOTE: cache first execution for most cases + if (!this.executing && !this.pending.length) { + this.pending.push([execution]); + } + this.events.shift(); if (this.events.length) { @@ -223,10 +228,10 @@ export default class WorkflowPlugin extends Plugin { return; } - let next: Pending; + let next: Pending | null = null; // resuming has high priority if (this.pending.length) { - next = this.pending.shift(); + next = this.pending.shift() as Pending; } else { const execution = await this.db.getRepository('executions').findOne({ filter: { diff --git a/packages/plugins/workflow/src/server/__tests__/Plugin.test.ts b/packages/plugins/workflow/src/server/__tests__/Plugin.test.ts index 9956b0af8..710316beb 100644 --- a/packages/plugins/workflow/src/server/__tests__/Plugin.test.ts +++ b/packages/plugins/workflow/src/server/__tests__/Plugin.test.ts @@ -219,7 +219,7 @@ describe('workflow > Plugin', () => { }); describe('dispatcher', () => { - it.skip('multiple triggers in same event', async () => { + it('multiple triggers in same event', async () => { const w1 = await WorkflowModel.create({ enabled: true, type: 'collection',