fix(plugin-workflow): dispatch when server start (#1183)

This commit is contained in:
Junyi 2022-12-01 23:58:13 -08:00 committed by GitHub
parent c64e32945e
commit eee4cabd64
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 71 additions and 0 deletions

View File

@ -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 () => {

View File

@ -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);
});
});
});

View File

@ -16,6 +16,7 @@ export default {
name: 'useTransaction',
defaultValue: false
},
// @deprecated
{
type: 'uuid',
name: 'transaction',