test(plugin-workflow): add cache and test for sqlite (#1194)

This commit is contained in:
Junyi 2022-12-04 17:38:37 -08:00 committed by GitHub
parent c19ca1f30b
commit 6fb58d01a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 6 deletions

View File

@ -22,7 +22,7 @@ export default class WorkflowPlugin extends Plugin {
triggers: Registry<Trigger> = new Registry(); triggers: Registry<Trigger> = new Registry();
calculators = calculators; calculators = calculators;
extensions = extensions; extensions = extensions;
executing: ExecutionModel = null; executing: ExecutionModel | null = null;
pending: Pending[] = []; pending: Pending[] = [];
events: [WorkflowModel, any, { context?: any }][] = []; 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<void> { public trigger(workflow: WorkflowModel, context: Object, options: { context?: any } = {}): void {
// `null` means not to trigger // `null` means not to trigger
if (context == null) { if (context == null) {
return; 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({ const execution = await workflow.createExecution({
context, context,
key: workflow.key, key: workflow.key,
@ -200,6 +200,11 @@ export default class WorkflowPlugin extends Plugin {
return execution; return execution;
}); });
// NOTE: cache first execution for most cases
if (!this.executing && !this.pending.length) {
this.pending.push([execution]);
}
this.events.shift(); this.events.shift();
if (this.events.length) { if (this.events.length) {
@ -223,10 +228,10 @@ export default class WorkflowPlugin extends Plugin {
return; return;
} }
let next: Pending; let next: Pending | null = null;
// resuming has high priority // resuming has high priority
if (this.pending.length) { if (this.pending.length) {
next = this.pending.shift(); next = this.pending.shift() as Pending;
} else { } else {
const execution = await this.db.getRepository('executions').findOne({ const execution = await this.db.getRepository('executions').findOne({
filter: { filter: {

View File

@ -219,7 +219,7 @@ describe('workflow > Plugin', () => {
}); });
describe('dispatcher', () => { describe('dispatcher', () => {
it.skip('multiple triggers in same event', async () => { it('multiple triggers in same event', async () => {
const w1 = await WorkflowModel.create({ const w1 = await WorkflowModel.create({
enabled: true, enabled: true,
type: 'collection', type: 'collection',