2022-01-09 22:22:26 +08:00
|
|
|
import path from 'path';
|
|
|
|
|
2022-02-12 22:35:48 +08:00
|
|
|
import { Plugin } from '@nocobase/server';
|
|
|
|
|
2022-01-28 00:25:26 +08:00
|
|
|
import WorkflowModel from './models/Workflow';
|
|
|
|
import ExecutionModel from './models/Execution';
|
2022-03-27 15:51:48 +08:00
|
|
|
import actions from './actions';
|
2022-02-27 22:58:41 +08:00
|
|
|
|
2022-02-12 22:35:48 +08:00
|
|
|
export default class WorkflowPlugin extends Plugin {
|
2022-01-09 22:22:26 +08:00
|
|
|
async load(options = {}) {
|
|
|
|
const { db } = this.app;
|
|
|
|
|
2022-01-28 00:25:26 +08:00
|
|
|
db.registerModels({
|
2022-01-09 22:22:26 +08:00
|
|
|
WorkflowModel,
|
|
|
|
ExecutionModel,
|
|
|
|
});
|
|
|
|
|
2022-01-28 00:25:26 +08:00
|
|
|
await db.import({
|
2022-01-09 22:22:26 +08:00
|
|
|
directory: path.resolve(__dirname, 'collections'),
|
|
|
|
});
|
|
|
|
|
2022-03-27 15:51:48 +08:00
|
|
|
actions(this.app);
|
|
|
|
|
2022-01-09 22:22:26 +08:00
|
|
|
// [Life Cycle]:
|
|
|
|
// * load all workflows in db
|
|
|
|
// * add all hooks for enabled workflows
|
|
|
|
// * add hooks for create/update[enabled]/delete workflow to add/remove specific hooks
|
|
|
|
this.app.on('beforeStart', async () => {
|
2022-01-28 00:25:26 +08:00
|
|
|
const { model } = db.getCollection('workflows');
|
2022-02-12 22:35:48 +08:00
|
|
|
await (model as typeof WorkflowModel).mount();
|
2022-03-28 22:01:10 +08:00
|
|
|
});
|
2022-01-09 22:22:26 +08:00
|
|
|
|
|
|
|
// [Life Cycle]: initialize all necessary seed data
|
2022-03-28 22:01:10 +08:00
|
|
|
this.app.on('db.init', async () => {});
|
2022-01-09 22:22:26 +08:00
|
|
|
|
|
|
|
// const [Automation, AutomationJob] = database.getModels(['automations', 'automations_jobs']);
|
|
|
|
|
|
|
|
// Automation.addHook('afterCreate', async (model: AutomationModel) => {
|
|
|
|
// model.get('enabled') && await model.loadJobs();
|
|
|
|
// });
|
|
|
|
|
|
|
|
// Automation.addHook('afterUpdate', async (model: AutomationModel) => {
|
|
|
|
// if (!model.changed('enabled' as any)) {
|
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
// model.get('enabled') ? await model.loadJobs() : await model.cancelJobs();
|
|
|
|
// });
|
|
|
|
|
|
|
|
// Automation.addHook('beforeDestroy', async (model: AutomationModel) => {
|
|
|
|
// await model.cancelJobs();
|
|
|
|
// });
|
|
|
|
|
|
|
|
// AutomationJob.addHook('afterCreate', async (model: AutomationJobModel) => {
|
|
|
|
// await model.bootstrap();
|
|
|
|
// });
|
|
|
|
|
|
|
|
// AutomationJob.addHook('beforeDestroy', async (model: AutomationJobModel) => {
|
|
|
|
// await model.cancel();
|
|
|
|
// });
|
|
|
|
}
|
2022-03-28 22:01:10 +08:00
|
|
|
|
|
|
|
getName(): string {
|
|
|
|
return this.getPackageName(__dirname);
|
|
|
|
}
|
2022-01-09 22:22:26 +08:00
|
|
|
}
|