feat(plugin-workflow): add checker for intervally dispatching (#4119)

This commit is contained in:
Junyi 2024-04-21 14:23:13 +08:00 committed by GitHub
parent e25d15518e
commit 5bcaa9d11f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -45,6 +45,7 @@ export default class PluginWorkflowServer extends Plugin {
private loggerCache: LRUCache<string, Logger>; private loggerCache: LRUCache<string, Logger>;
private meter = null; private meter = null;
private checker: NodeJS.Timeout = null;
private onBeforeSave = async (instance: WorkflowModel, options) => { private onBeforeSave = async (instance: WorkflowModel, options) => {
const Model = <typeof WorkflowModel>instance.constructor; const Model = <typeof WorkflowModel>instance.constructor;
@ -245,6 +246,10 @@ export default class PluginWorkflowServer extends Plugin {
workflows.forEach((workflow: WorkflowModel) => { workflows.forEach((workflow: WorkflowModel) => {
this.toggle(workflow); this.toggle(workflow);
}); });
this.checker = setInterval(() => {
this.dispatch();
}, 300_000);
}); });
this.app.on('afterStart', () => { this.app.on('afterStart', () => {
@ -271,6 +276,10 @@ export default class PluginWorkflowServer extends Plugin {
if (this.executing) { if (this.executing) {
await this.executing; await this.executing;
} }
if (this.checker) {
clearInterval(this.checker);
}
}); });
} }
@ -466,6 +475,7 @@ export default class PluginWorkflowServer extends Plugin {
this.executing = (async () => { this.executing = (async () => {
let next: Pending | null = null; let next: Pending | null = null;
try {
// resuming has high priority // resuming has high priority
if (this.pending.length) { if (this.pending.length) {
next = this.pending.shift() as Pending; next = this.pending.shift() as Pending;
@ -480,7 +490,7 @@ export default class PluginWorkflowServer extends Plugin {
}, },
}, },
appends: ['workflow'], appends: ['workflow'],
sort: 'createdAt', sort: 'id',
})) as ExecutionModel; })) as ExecutionModel;
if (execution) { if (execution) {
this.getLogger(execution.workflowId).info(`execution (${execution.id}) fetched from db`); this.getLogger(execution.workflowId).info(`execution (${execution.id}) fetched from db`);
@ -490,12 +500,13 @@ export default class PluginWorkflowServer extends Plugin {
if (next) { if (next) {
await this.process(...next); await this.process(...next);
} }
} finally {
this.executing = null; this.executing = null;
if (next) { if (next) {
this.dispatch(); this.dispatch();
} }
}
})(); })();
} }