@nocobase/plugin-workflow
Version:
A powerful BPM tool that provides foundational support for business automation, with the capability to extend unlimited triggers and nodes.
486 lines (484 loc) • 17 kB
JavaScript
/**
* This file is part of the NocoBase (R) project.
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
* Authors: NocoBase Team.
*
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
* For more information, please refer to: https://www.nocobase.com/agreement.
*/
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var Plugin_exports = {};
__export(Plugin_exports, {
WORKER_JOB_WORKFLOW_PROCESS: () => WORKER_JOB_WORKFLOW_PROCESS,
default: () => PluginWorkflowServer
});
module.exports = __toCommonJS(Plugin_exports);
var import_path = __toESM(require("path"));
var import_nodejs_snowflake = require("nodejs-snowflake");
var import_lru_cache = __toESM(require("lru-cache"));
var import_database = require("@nocobase/database");
var import_server = require("@nocobase/server");
var import_utils = require("@nocobase/utils");
var import_Dispatcher = __toESM(require("./Dispatcher"));
var import_Processor = __toESM(require("./Processor"));
var import_actions = __toESM(require("./actions"));
var import_functions = __toESM(require("./functions"));
var import_CollectionTrigger = __toESM(require("./triggers/CollectionTrigger"));
var import_ScheduleTrigger = __toESM(require("./triggers/ScheduleTrigger"));
var import_CalculationInstruction = __toESM(require("./instructions/CalculationInstruction"));
var import_ConditionInstruction = __toESM(require("./instructions/ConditionInstruction"));
var import_EndInstruction = __toESM(require("./instructions/EndInstruction"));
var import_CreateInstruction = __toESM(require("./instructions/CreateInstruction"));
var import_DestroyInstruction = __toESM(require("./instructions/DestroyInstruction"));
var import_QueryInstruction = __toESM(require("./instructions/QueryInstruction"));
var import_UpdateInstruction = __toESM(require("./instructions/UpdateInstruction"));
var import_WorkflowRepository = __toESM(require("./repositories/WorkflowRepository"));
const WORKER_JOB_WORKFLOW_PROCESS = "workflow:process";
class PluginWorkflowServer extends import_server.Plugin {
instructions = new import_utils.Registry();
triggers = new import_utils.Registry();
functions = new import_utils.Registry();
enabledCache = /* @__PURE__ */ new Map();
snowflake;
dispatcher = new import_Dispatcher.default(this);
get channelPendingExecution() {
return `${this.name}.pendingExecution`;
}
loggerCache;
meter = null;
checker = null;
onBeforeSave = async (instance, { transaction, cycling }) => {
if (cycling) {
return;
}
const Model = instance.constructor;
if (!instance.key) {
instance.set("key", (0, import_utils.uid)());
}
if (instance.enabled) {
instance.set("current", true);
}
const previous = await Model.findOne({
where: {
key: instance.key,
current: true,
id: {
[import_database.Op.ne]: instance.id
}
},
transaction
});
if (!previous) {
instance.set("current", true);
} else if (instance.current) {
await previous.update(
{ enabled: false, current: null },
{
transaction,
cycling: true
}
);
this.toggle(previous, false, { transaction });
}
};
onAfterCreate = async (model, { transaction }) => {
const WorkflowStatsModel = this.db.getModel("workflowStats");
let stats = await WorkflowStatsModel.findOne({
where: { key: model.key },
transaction
});
if (!stats) {
stats = await model.createStats({ executed: 0 }, { transaction });
}
model.stats = stats;
model.versionStats = await model.createVersionStats({ id: model.id }, { transaction });
if (model.enabled) {
this.toggle(model, true, { transaction });
}
};
onAfterUpdate = async (model, { transaction }) => {
model.stats = await model.getStats({ transaction });
model.versionStats = await model.getVersionStats({ transaction });
this.toggle(model, model.enabled, { transaction });
};
onAfterDestroy = async (model, { transaction }) => {
this.toggle(model, false, { transaction });
const TaskRepo = this.db.getRepository("workflowTasks");
await TaskRepo.destroy({
filter: {
workflowId: model.id
},
transaction
});
};
// [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
onAfterStart = async () => {
const collection = this.db.getCollection("workflows");
const workflows = await collection.repository.find({
appends: ["versionStats"]
});
for (const workflow of workflows) {
if (workflow.current) {
workflow.stats = await workflow.getStats();
if (!workflow.stats) {
workflow.stats = await workflow.createStats({ executed: 0 });
}
}
if (!workflow.versionStats) {
workflow.versionStats = await workflow.createVersionStats({ executed: 0 });
}
if (workflow.enabled) {
this.toggle(workflow, true, { silent: true });
}
}
this.checker = setInterval(() => {
this.getLogger("dispatcher").debug(`(cycling) check for queueing executions`);
this.dispatcher.dispatch();
}, 3e5);
this.app.on("workflow:dispatch", () => {
this.app.logger.info("workflow:dispatch");
this.dispatcher.dispatch();
});
this.dispatcher.setReady(true);
this.getLogger("dispatcher").info("(starting) check for queueing executions");
this.dispatcher.dispatch();
};
onBeforeStop = async () => {
if (this.checker) {
clearInterval(this.checker);
}
await this.dispatcher.beforeStop();
this.app.logger.info(`stopping workflow plugin before app (${this.app.name}) shutdown...`);
for (const workflow of this.enabledCache.values()) {
this.toggle(workflow, false, { silent: true });
}
this.app.eventQueue.unsubscribe(this.channelPendingExecution);
this.loggerCache.clear();
};
async handleSyncMessage(message) {
if (message.type === "statusChange") {
if (message.enabled) {
let workflow = this.enabledCache.get(message.workflowId);
if (workflow) {
await workflow.reload();
} else {
workflow = await this.db.getRepository("workflows").findOne({
filterByTk: message.workflowId
});
}
if (workflow) {
this.toggle(workflow, true, { silent: true });
}
} else {
const workflow = this.enabledCache.get(message.workflowId);
if (workflow) {
this.toggle(workflow, false, { silent: true });
}
}
}
}
serving() {
return this.app.serving(WORKER_JOB_WORKFLOW_PROCESS);
}
/**
* @experimental
*/
getLogger(workflowId = "dispatcher") {
const now = /* @__PURE__ */ new Date();
const date = `${now.getFullYear()}-${`0${now.getMonth() + 1}`.slice(-2)}-${`0${now.getDate()}`.slice(-2)}`;
const key = `${date}-${workflowId}`;
if (this.loggerCache.has(key)) {
return this.loggerCache.get(key);
}
const logger = this.createLogger({
dirname: import_path.default.join("workflows", String(workflowId)),
filename: "%DATE%.log"
});
this.loggerCache.set(key, logger);
return logger;
}
/**
* @experimental
* @param {WorkflowModel} workflow
* @returns {boolean}
*/
isWorkflowSync(workflow) {
const trigger = this.triggers.get(workflow.type);
if (!trigger) {
throw new Error(`invalid trigger type ${workflow.type} of workflow ${workflow.id}`);
}
return trigger.sync ?? workflow.sync;
}
registerTrigger(type, trigger) {
if (typeof trigger === "function") {
this.triggers.register(type, new trigger(this));
} else if (trigger) {
this.triggers.register(type, trigger);
} else {
throw new Error("invalid trigger type to register");
}
}
registerInstruction(type, instruction) {
if (typeof instruction === "function") {
this.instructions.register(type, new instruction(this));
} else if (instruction) {
this.instructions.register(type, instruction);
} else {
throw new Error("invalid instruction type to register");
}
}
initTriggers(more = {}) {
this.registerTrigger("collection", import_CollectionTrigger.default);
this.registerTrigger("schedule", import_ScheduleTrigger.default);
for (const [name, trigger] of Object.entries(more)) {
this.registerTrigger(name, trigger);
}
}
initInstructions(more = {}) {
this.registerInstruction("calculation", import_CalculationInstruction.default);
this.registerInstruction("condition", import_ConditionInstruction.default);
this.registerInstruction("end", import_EndInstruction.default);
this.registerInstruction("create", import_CreateInstruction.default);
this.registerInstruction("destroy", import_DestroyInstruction.default);
this.registerInstruction("query", import_QueryInstruction.default);
this.registerInstruction("update", import_UpdateInstruction.default);
for (const [name, instruction] of Object.entries({ ...more })) {
this.registerInstruction(name, instruction);
}
}
async beforeLoad() {
this.db.registerRepositories({
WorkflowRepository: import_WorkflowRepository.default
});
const PluginRepo = this.db.getRepository("applicationPlugins");
const pluginRecord = await PluginRepo.findOne({
filter: { name: this.name }
});
this.snowflake = new import_nodejs_snowflake.Snowflake({
custom_epoch: pluginRecord == null ? void 0 : pluginRecord.createdAt.getTime()
});
}
/**
* @internal
*/
async load() {
const { db, options } = this;
(0, import_actions.default)(this);
this.initTriggers(options.triggers);
this.initInstructions(options.instructions);
(0, import_functions.default)(this, options.functions);
this.functions.register("instanceId", () => this.app.instanceId);
this.functions.register("epoch", () => 1605024e3);
this.functions.register("genSnowflakeId", () => this.app.snowflakeIdGenerator.generate());
this.loggerCache = new import_lru_cache.default({
max: 20,
updateAgeOnGet: true,
dispose(logger) {
const cachedLogger = logger;
if (!cachedLogger) {
return;
}
cachedLogger.silent = true;
if (typeof cachedLogger.close === "function") {
cachedLogger.close();
}
}
});
this.meter = this.app.telemetry.metric.getMeter();
const counter = this.meter.createObservableGauge("workflow.events.counter");
counter.addCallback((result) => {
result.observe(this.dispatcher.getEventsCount());
});
this.app.acl.registerSnippet({
name: `pm.${this.name}.workflows`,
actions: [
"workflows:*",
"workflows.nodes:*",
"executions:list",
"executions:get",
"executions:cancel",
"executions:destroy",
"flow_nodes:update",
"flow_nodes:destroy",
"flow_nodes:test",
"jobs:get",
"workflowCategories:*"
]
});
this.app.acl.registerSnippet({
name: "ui.workflows",
actions: ["workflows:list"]
});
this.app.acl.allow("userWorkflowTasks", "listMine", "loggedIn");
this.app.acl.allow("*", ["trigger"], "loggedIn");
db.on("workflows.beforeSave", this.onBeforeSave);
db.on("workflows.afterCreate", this.onAfterCreate);
db.on("workflows.afterUpdate", this.onAfterUpdate);
db.on("workflows.afterDestroy", this.onAfterDestroy);
this.app.on("afterStart", this.onAfterStart);
this.app.on("beforeStop", this.onBeforeStop);
this.app.eventQueue.subscribe(this.channelPendingExecution, {
idle: () => this.serving() && this.dispatcher.idle,
process: this.dispatcher.onQueueExecution
});
}
toggle(workflow, enable, { silent, transaction } = {}) {
const type = workflow.get("type");
const trigger = this.triggers.get(type);
if (!trigger) {
this.getLogger(workflow.id).error(`trigger type ${workflow.type} of workflow ${workflow.id} is not implemented`, {
workflowId: workflow.id
});
return;
}
const next = enable ?? workflow.get("enabled");
if (next) {
const prev = workflow.previous();
if (prev.config) {
trigger.off({ ...workflow.get(), ...prev });
this.getLogger(workflow.id).info(`toggle OFF workflow ${workflow.id} based on configuration before updated`, {
workflowId: workflow.id
});
}
trigger.on(workflow);
this.getLogger(workflow.id).info(`toggle ON workflow ${workflow.id}`, {
workflowId: workflow.id
});
this.enabledCache.set(workflow.id, workflow);
} else {
trigger.off(workflow);
this.getLogger(workflow.id).info(`toggle OFF workflow ${workflow.id}`, {
workflowId: workflow.id
});
this.enabledCache.delete(workflow.id);
}
if (!silent) {
this.sendSyncMessage(
{
type: "statusChange",
workflowId: workflow.id,
enabled: next
},
{ transaction }
);
}
}
trigger(workflow, context, options = {}) {
return this.dispatcher.trigger(workflow, context, options);
}
async run(pending) {
return this.dispatcher.run(pending);
}
async resume(job) {
return this.dispatcher.resume(job);
}
/**
* Start a deferred execution
* @experimental
*/
async start(execution) {
return this.dispatcher.start(execution);
}
createProcessor(execution, options = {}) {
return new import_Processor.default(execution, { ...options, plugin: this });
}
async execute(workflow, values, options = {}) {
const trigger = this.triggers.get(workflow.type);
if (!trigger) {
throw new Error(`trigger type "${workflow.type}" of workflow ${workflow.id} is not registered`);
}
if (!trigger.execute) {
throw new Error(`"execute" method of trigger ${workflow.type} is not implemented`);
}
return trigger.execute(workflow, values, options);
}
/**
* @experimental
* @param {string} dataSourceName
* @param {Transaction} transaction
* @param {boolean} create
* @returns {Trasaction}
*/
useDataSourceTransaction(dataSourceName = "main", transaction, create = false) {
const { db } = this.app.dataSourceManager.dataSources.get(dataSourceName).collectionManager;
if (!db) {
return;
}
if (db.sequelize === (transaction == null ? void 0 : transaction.sequelize)) {
return transaction;
}
if (create) {
return db.sequelize.transaction();
}
}
/**
* @experimental
*/
async updateTasksStats(userId, type, stats = { pending: 0, all: 0 }, { transaction }) {
const { db } = this.app;
const repository = db.getRepository("userWorkflowTasks");
let record = await repository.findOne({
filter: {
userId,
type
},
transaction
});
if (record) {
await record.update(
{
stats
},
{ transaction }
);
} else {
record = await repository.create({
values: {
userId,
type,
stats
},
transaction
});
}
if (userId) {
this.app.emit("ws:sendToUser", {
userId,
message: { type: "workflow:tasks:updated", payload: record.get() }
});
}
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
WORKER_JOB_WORKFLOW_PROCESS
});