2022-02-23 18:39:36 +08:00
|
|
|
import { Database, Model } from '@nocobase/database';
|
2022-04-29 22:21:58 +08:00
|
|
|
import { HasManyCountAssociationsMixin, HasManyCreateAssociationMixin, HasManyGetAssociationsMixin } from 'sequelize';
|
2022-02-27 22:58:41 +08:00
|
|
|
|
|
|
|
import triggers from '../triggers';
|
2022-01-09 22:22:26 +08:00
|
|
|
import { EXECUTION_STATUS } from '../constants';
|
2022-01-28 00:25:26 +08:00
|
|
|
import ExecutionModel from './Execution';
|
|
|
|
import FlowNodeModel from './FlowNode';
|
|
|
|
|
|
|
|
export default class WorkflowModel extends Model {
|
|
|
|
declare static database: Database;
|
|
|
|
|
|
|
|
declare id: number;
|
|
|
|
declare title: string;
|
|
|
|
declare enabled: boolean;
|
|
|
|
declare description?: string;
|
|
|
|
declare type: string;
|
|
|
|
declare config: any;
|
2022-04-14 00:05:13 +08:00
|
|
|
declare useTransaction: boolean;
|
2022-05-12 12:19:25 +08:00
|
|
|
declare executed: boolean;
|
2022-01-28 00:25:26 +08:00
|
|
|
|
|
|
|
declare createdAt: Date;
|
|
|
|
declare updatedAt: Date;
|
|
|
|
|
|
|
|
declare nodes: FlowNodeModel[];
|
|
|
|
declare getNodes: HasManyGetAssociationsMixin<FlowNodeModel>;
|
|
|
|
declare createNode: HasManyCreateAssociationMixin<FlowNodeModel>;
|
|
|
|
|
|
|
|
declare executions: ExecutionModel[];
|
2022-04-29 22:21:58 +08:00
|
|
|
declare countExecutions: HasManyCountAssociationsMixin;
|
2022-01-28 00:25:26 +08:00
|
|
|
declare getExecutions: HasManyGetAssociationsMixin<ExecutionModel>;
|
|
|
|
declare createExecution: HasManyCreateAssociationMixin<ExecutionModel>;
|
2022-01-09 22:22:26 +08:00
|
|
|
|
|
|
|
static async mount() {
|
2022-01-28 00:25:26 +08:00
|
|
|
const collection = this.database.getCollection('workflows');
|
|
|
|
const workflows = await collection.repository.find({
|
2022-02-23 18:39:36 +08:00
|
|
|
filter: { enabled: true },
|
2022-01-09 22:22:26 +08:00
|
|
|
});
|
|
|
|
|
2022-01-28 21:02:32 +08:00
|
|
|
workflows.forEach((workflow: WorkflowModel) => {
|
|
|
|
workflow.toggle();
|
2022-01-09 22:22:26 +08:00
|
|
|
});
|
|
|
|
|
2022-01-28 21:02:32 +08:00
|
|
|
this.addHook('afterCreate', (model: WorkflowModel) => model.toggle());
|
|
|
|
this.addHook('afterUpdate', (model: WorkflowModel) => model.toggle());
|
|
|
|
this.addHook('afterDestroy', (model: WorkflowModel) => model.toggle(false));
|
2022-01-09 22:22:26 +08:00
|
|
|
}
|
|
|
|
|
2022-01-28 21:02:32 +08:00
|
|
|
getHookId() {
|
|
|
|
return `workflow-${this.get('id')}`;
|
2022-01-09 22:22:26 +08:00
|
|
|
}
|
|
|
|
|
2022-04-14 00:05:13 +08:00
|
|
|
getTransaction(options) {
|
|
|
|
if (!this.useTransaction) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
return options.transaction && !options.transaction.finished
|
|
|
|
? options.transaction
|
|
|
|
: (<typeof WorkflowModel>this.constructor).database.sequelize.transaction();
|
|
|
|
}
|
|
|
|
|
2022-01-28 21:02:32 +08:00
|
|
|
async toggle(enable?: boolean) {
|
|
|
|
const type = this.get('type');
|
2022-02-27 22:58:41 +08:00
|
|
|
const { on, off } = triggers.get(type);
|
2022-01-28 21:02:32 +08:00
|
|
|
if (typeof enable !== 'undefined' ? enable : this.get('enabled')) {
|
2022-04-14 00:05:13 +08:00
|
|
|
on.call(this, this.trigger.bind(this));
|
2022-01-28 21:02:32 +08:00
|
|
|
} else {
|
|
|
|
off.call(this);
|
|
|
|
}
|
2022-01-09 22:22:26 +08:00
|
|
|
}
|
|
|
|
|
2022-04-14 00:05:13 +08:00
|
|
|
async trigger(context: Object, options) {
|
2022-01-09 22:22:26 +08:00
|
|
|
// `null` means not to trigger
|
|
|
|
if (context === null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-04-14 00:05:13 +08:00
|
|
|
const transaction = await this.getTransaction(options);
|
|
|
|
|
2022-04-29 22:21:58 +08:00
|
|
|
if (this.useTransaction) {
|
|
|
|
const existed = await this.countExecutions({
|
|
|
|
where: {
|
|
|
|
transaction: transaction.id
|
|
|
|
},
|
|
|
|
transaction
|
|
|
|
});
|
|
|
|
|
|
|
|
if (existed) {
|
|
|
|
console.warn(`workflow ${this.id} has already been triggered in same execution (${transaction.id}), and newly triggering will be skipped.`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-09 22:22:26 +08:00
|
|
|
const execution = await this.createExecution({
|
|
|
|
context,
|
2022-02-23 18:39:36 +08:00
|
|
|
status: EXECUTION_STATUS.STARTED,
|
2022-04-29 22:21:58 +08:00
|
|
|
useTransaction: this.useTransaction,
|
|
|
|
transaction: transaction.id
|
2022-04-14 00:05:13 +08:00
|
|
|
}, { transaction });
|
2022-01-28 00:25:26 +08:00
|
|
|
|
2022-01-26 02:27:23 +08:00
|
|
|
execution.workflow = this;
|
|
|
|
|
2022-04-14 00:05:13 +08:00
|
|
|
await execution.start({ transaction });
|
|
|
|
|
2022-05-12 12:19:25 +08:00
|
|
|
if (!this.executed) {
|
|
|
|
await this.update({ executed: true }, { transaction });
|
|
|
|
}
|
|
|
|
|
2022-04-14 00:05:13 +08:00
|
|
|
if (transaction && (!options.transaction || options.transaction.finished)) {
|
|
|
|
await transaction.commit();
|
|
|
|
}
|
|
|
|
|
2022-01-09 22:22:26 +08:00
|
|
|
return execution;
|
|
|
|
}
|
|
|
|
}
|