@nocobase/plugin-workflow 1.9.0-beta.15 → 1.9.0-beta.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -36,6 +36,7 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
36
36
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
37
37
  var Plugin_exports = {};
38
38
  __export(Plugin_exports, {
39
+ WORKER_JOB_WORKFLOW_PROCESS: () => WORKER_JOB_WORKFLOW_PROCESS,
39
40
  default: () => PluginWorkflowServer
40
41
  });
41
42
  module.exports = __toCommonJS(Plugin_exports);
@@ -59,6 +60,7 @@ var import_DestroyInstruction = __toESM(require("./instructions/DestroyInstructi
59
60
  var import_QueryInstruction = __toESM(require("./instructions/QueryInstruction"));
60
61
  var import_UpdateInstruction = __toESM(require("./instructions/UpdateInstruction"));
61
62
  var import_WorkflowRepository = __toESM(require("./repositories/WorkflowRepository"));
63
+ const WORKER_JOB_WORKFLOW_PROCESS = "workflow:process";
62
64
  class PluginWorkflowServer extends import_server.Plugin {
63
65
  instructions = new import_utils.Registry();
64
66
  triggers = new import_utils.Registry();
@@ -66,6 +68,9 @@ class PluginWorkflowServer extends import_server.Plugin {
66
68
  enabledCache = /* @__PURE__ */ new Map();
67
69
  snowflake;
68
70
  dispatcher = new import_Dispatcher.default(this);
71
+ get channelPendingExecution() {
72
+ return `${this.name}.pendingExecution`;
73
+ }
69
74
  loggerCache;
70
75
  meter = null;
71
76
  checker = null;
@@ -170,6 +175,7 @@ class PluginWorkflowServer extends import_server.Plugin {
170
175
  };
171
176
  onBeforeStop = async () => {
172
177
  this.dispatcher.setReady(false);
178
+ this.app.eventQueue.unsubscribe(this.channelPendingExecution);
173
179
  this.app.logger.info(`stopping workflow plugin before app (${this.app.name}) shutdown...`);
174
180
  for (const workflow of this.enabledCache.values()) {
175
181
  this.toggle(workflow, false, { silent: true });
@@ -202,6 +208,9 @@ class PluginWorkflowServer extends import_server.Plugin {
202
208
  }
203
209
  }
204
210
  }
211
+ serving() {
212
+ return this.app.serving(WORKER_JOB_WORKFLOW_PROCESS);
213
+ }
205
214
  /**
206
215
  * @experimental
207
216
  */
@@ -279,10 +288,6 @@ class PluginWorkflowServer extends import_server.Plugin {
279
288
  this.snowflake = new import_nodejs_snowflake.Snowflake({
280
289
  custom_epoch: pluginRecord == null ? void 0 : pluginRecord.createdAt.getTime()
281
290
  });
282
- this.app.backgroundJobManager.subscribe(`${this.name}.pendingExecution`, {
283
- idle: () => this.app.serving(import_Dispatcher.WORKER_JOB_WORKFLOW_PROCESS) && this.dispatcher.idle,
284
- process: this.dispatcher.onQueueExecution
285
- });
286
291
  }
287
292
  /**
288
293
  * @internal
@@ -340,12 +345,18 @@ class PluginWorkflowServer extends import_server.Plugin {
340
345
  db.on("workflows.afterDestroy", this.onAfterDestroy);
341
346
  this.app.on("afterStart", this.onAfterStart);
342
347
  this.app.on("beforeStop", this.onBeforeStop);
348
+ this.app.eventQueue.subscribe(this.channelPendingExecution, {
349
+ idle: () => this.serving() && this.dispatcher.idle,
350
+ process: this.dispatcher.onQueueExecution
351
+ });
343
352
  }
344
353
  toggle(workflow, enable, { silent, transaction } = {}) {
345
354
  const type = workflow.get("type");
346
355
  const trigger = this.triggers.get(type);
347
356
  if (!trigger) {
348
- this.getLogger(workflow.id).error(`trigger type ${workflow.type} of workflow ${workflow.id} is not implemented`);
357
+ this.getLogger(workflow.id).error(`trigger type ${workflow.type} of workflow ${workflow.id} is not implemented`, {
358
+ workflowId: workflow.id
359
+ });
349
360
  return;
350
361
  }
351
362
  const next = enable ?? workflow.get("enabled");
@@ -353,14 +364,20 @@ class PluginWorkflowServer extends import_server.Plugin {
353
364
  const prev = workflow.previous();
354
365
  if (prev.config) {
355
366
  trigger.off({ ...workflow.get(), ...prev });
356
- this.getLogger(workflow.id).info(`toggle OFF workflow ${workflow.id} based on configuration before updated`);
367
+ this.getLogger(workflow.id).info(`toggle OFF workflow ${workflow.id} based on configuration before updated`, {
368
+ workflowId: workflow.id
369
+ });
357
370
  }
358
371
  trigger.on(workflow);
359
- this.getLogger(workflow.id).info(`toggle ON workflow ${workflow.id}`);
372
+ this.getLogger(workflow.id).info(`toggle ON workflow ${workflow.id}`, {
373
+ workflowId: workflow.id
374
+ });
360
375
  this.enabledCache.set(workflow.id, workflow);
361
376
  } else {
362
377
  trigger.off(workflow);
363
- this.getLogger(workflow.id).info(`toggle OFF workflow ${workflow.id}`);
378
+ this.getLogger(workflow.id).info(`toggle OFF workflow ${workflow.id}`, {
379
+ workflowId: workflow.id
380
+ });
364
381
  this.enabledCache.delete(workflow.id);
365
382
  }
366
383
  if (!silent) {
@@ -460,3 +477,7 @@ class PluginWorkflowServer extends import_server.Plugin {
460
477
  }
461
478
  }
462
479
  }
480
+ // Annotate the CommonJS export names for ESM import in node:
481
+ 0 && (module.exports = {
482
+ WORKER_JOB_WORKFLOW_PROCESS
483
+ });
@@ -144,7 +144,9 @@ class Processor {
144
144
  async start() {
145
145
  const { execution } = this;
146
146
  if (execution.status) {
147
- this.logger.warn(`execution was ended with status ${execution.status} before, could not be started again`);
147
+ this.logger.warn(`execution was ended with status ${execution.status} before, could not be started again`, {
148
+ workflowId: execution.workflowId
149
+ });
148
150
  return;
149
151
  }
150
152
  await this.prepare();
@@ -158,7 +160,9 @@ class Processor {
158
160
  async resume(job) {
159
161
  const { execution } = this;
160
162
  if (execution.status) {
161
- this.logger.warn(`execution was ended with status ${execution.status} before, could not be resumed`);
163
+ this.logger.warn(`execution was ended with status ${execution.status} before, could not be resumed`, {
164
+ workflowId: execution.workflowId
165
+ });
162
166
  return;
163
167
  }
164
168
  await this.prepare();
@@ -168,7 +172,7 @@ class Processor {
168
172
  async exec(instruction, node, prevJob) {
169
173
  let job;
170
174
  try {
171
- this.logger.debug(`config of node`, { data: node.config });
175
+ this.logger.debug(`config of node`, { data: node.config, workflowId: node.workflowId });
172
176
  job = await instruction(node, prevJob, this);
173
177
  if (job === null) {
174
178
  return this.exit();
@@ -179,7 +183,7 @@ class Processor {
179
183
  } catch (err) {
180
184
  this.logger.error(
181
185
  `execution (${this.execution.id}) run instruction [${node.type}] for node (${node.id}) failed: `,
182
- err
186
+ { error: err, workflowId: node.workflowId }
183
187
  );
184
188
  job = {
185
189
  result: err instanceof Error ? {
@@ -199,7 +203,10 @@ class Processor {
199
203
  }
200
204
  const savedJob = this.saveJob(job);
201
205
  this.logger.info(
202
- `execution (${this.execution.id}) run instruction [${node.type}] for node (${node.id}) finished as status: ${savedJob.status}`
206
+ `execution (${this.execution.id}) run instruction [${node.type}] for node (${node.id}) finished as status: ${savedJob.status}`,
207
+ {
208
+ workflowId: node.workflowId
209
+ }
203
210
  );
204
211
  this.logger.debug(`result of node`, { data: savedJob.result });
205
212
  if (savedJob.status === import_constants.JOB_STATUS.RESOLVED && node.downstream) {
@@ -217,7 +224,9 @@ class Processor {
217
224
  if (typeof instruction.run !== "function") {
218
225
  return Promise.reject(new Error("`run` should be implemented for customized execution of the node"));
219
226
  }
220
- this.logger.info(`execution (${this.execution.id}) run instruction [${node.type}] for node (${node.id})`);
227
+ this.logger.info(`execution (${this.execution.id}) run instruction [${node.type}] for node (${node.id})`, {
228
+ workflowId: node.workflowId
229
+ });
221
230
  return this.exec(instruction.run.bind(instruction), node, input);
222
231
  }
223
232
  // parent node should take over the control
@@ -225,7 +234,9 @@ class Processor {
225
234
  this.logger.debug(`branch ended at node (${node.id})`);
226
235
  const parentNode = this.findBranchParentNode(node);
227
236
  if (parentNode) {
228
- this.logger.debug(`not on main, recall to parent entry node (${node.id})})`);
237
+ this.logger.debug(`not on main, recall to parent entry node (${node.id})})`, {
238
+ workflowId: node.workflowId
239
+ });
229
240
  await this.recall(parentNode, job);
230
241
  return null;
231
242
  }
@@ -242,7 +253,9 @@ class Processor {
242
253
  new Error(`"resume" method should be implemented for [${node.type}] instruction of node (#${node.id})`)
243
254
  );
244
255
  }
245
- this.logger.info(`execution (${this.execution.id}) resume instruction [${node.type}] for node (${node.id})`);
256
+ this.logger.info(`execution (${this.execution.id}) resume instruction [${node.type}] for node (${node.id})`, {
257
+ workflowId: node.workflowId
258
+ });
246
259
  return this.exec(instruction.resume.bind(instruction), node, job);
247
260
  }
248
261
  async exit(s) {
@@ -295,7 +308,9 @@ class Processor {
295
308
  if (this.mainTransaction && this.mainTransaction !== this.transaction) {
296
309
  await this.mainTransaction.commit();
297
310
  }
298
- this.logger.info(`execution (${this.execution.id}) exiting with status ${this.execution.status}`);
311
+ this.logger.info(`execution (${this.execution.id}) exiting with status ${this.execution.status}`, {
312
+ workflowId: this.execution.workflowId
313
+ });
299
314
  return null;
300
315
  }
301
316
  /**
@@ -326,7 +341,9 @@ class Processor {
326
341
  this.lastSavedJob = job;
327
342
  this.jobsMapByNodeKey[job.nodeKey] = job;
328
343
  this.jobResultsMapByNodeKey[job.nodeKey] = job.result;
329
- this.logger.debug(`job added to save list: ${JSON.stringify(job)}`);
344
+ this.logger.debug(`job added to save list: ${JSON.stringify(job)}`, {
345
+ workflowId: this.execution.workflowId
346
+ });
330
347
  return job;
331
348
  }
332
349
  /**
@@ -158,7 +158,7 @@ async function destroy(context, next) {
158
158
  const repository = import_actions.utils.getRepositoryFromParams(context);
159
159
  const { filterByTk, keepBranch } = context.action.params;
160
160
  const keepBranchIndex = keepBranch == null || keepBranch === "" ? null : Number.parseInt(keepBranch, 10);
161
- const fields = ["id", "upstreamId", "downstreamId", "branchIndex"];
161
+ const fields = ["id", "upstreamId", "downstreamId", "branchIndex", "key"];
162
162
  const instance = await repository.findOne({
163
163
  filterByTk,
164
164
  fields: [...fields, "workflowId"],
@@ -147,14 +147,20 @@ class CollectionTrigger extends import__.default {
147
147
  };
148
148
  }
149
149
  on(workflow) {
150
- var _a, _b;
150
+ var _a;
151
151
  const { collection, mode } = workflow.config;
152
152
  if (!collection) {
153
153
  return;
154
154
  }
155
155
  const [dataSourceName, collectionName] = (0, import_data_source_manager.parseCollectionName)(collection);
156
- const { db } = ((_b = (_a = this.workflow.app.dataSourceManager) == null ? void 0 : _a.dataSources.get(dataSourceName)) == null ? void 0 : _b.collectionManager) ?? {};
156
+ const dataSource = (_a = this.workflow.app.dataSourceManager) == null ? void 0 : _a.dataSources.get(dataSourceName);
157
+ if (!dataSource) {
158
+ this.workflow.getLogger().warn(`[CollectionTrigger] data source not exists: ${dataSourceName}`);
159
+ return;
160
+ }
161
+ const { db } = dataSource.collectionManager;
157
162
  if (!db || !db.getCollection(collectionName)) {
163
+ this.workflow.getLogger().warn(`[CollectionTrigger] collection not exists: ${dataSourceName}`);
158
164
  return;
159
165
  }
160
166
  for (const [key, type] of MODE_BITMAP_EVENTS.entries()) {
@@ -182,8 +188,14 @@ class CollectionTrigger extends import__.default {
182
188
  return;
183
189
  }
184
190
  const [dataSourceName, collectionName] = (0, import_data_source_manager.parseCollectionName)(collection);
185
- const { db } = ((_a = this.workflow.app.dataSourceManager.dataSources.get(dataSourceName)) == null ? void 0 : _a.collectionManager) ?? {};
191
+ const dataSource = (_a = this.workflow.app.dataSourceManager) == null ? void 0 : _a.dataSources.get(dataSourceName);
192
+ if (!dataSource) {
193
+ this.workflow.getLogger().warn(`[CollectionTrigger] data source not exists: ${dataSourceName}`);
194
+ return;
195
+ }
196
+ const { db } = dataSource.collectionManager;
186
197
  if (!db || !db.getCollection(collectionName)) {
198
+ this.workflow.getLogger().warn(`[CollectionTrigger] collection not exists: ${dataSourceName}`);
187
199
  return;
188
200
  }
189
201
  for (const [key, type] of MODE_BITMAP_EVENTS.entries()) {
@@ -379,6 +379,10 @@ class DateFieldScheduleTrigger {
379
379
  };
380
380
  this.events.set(name, listener);
381
381
  const dataSource = this.workflow.app.dataSourceManager.dataSources.get(dataSourceName);
382
+ if (!dataSource) {
383
+ this.workflow.getLogger().warn(`[Schedule on date field] data source not exists: ${dataSourceName}`);
384
+ return;
385
+ }
382
386
  const { db } = dataSource.collectionManager;
383
387
  db.on(event, listener);
384
388
  }
@@ -396,6 +400,10 @@ class DateFieldScheduleTrigger {
396
400
  const listener = this.events.get(name);
397
401
  if (listener) {
398
402
  const dataSource = this.workflow.app.dataSourceManager.dataSources.get(dataSourceName);
403
+ if (!dataSource) {
404
+ this.workflow.getLogger().warn(`[Schedule on date field] data source not exists: ${dataSourceName}`);
405
+ return;
406
+ }
399
407
  const { db } = dataSource.collectionManager;
400
408
  db.off(event, listener);
401
409
  this.events.delete(name);
package/package.json CHANGED
@@ -2,13 +2,16 @@
2
2
  "name": "@nocobase/plugin-workflow",
3
3
  "displayName": "Workflow",
4
4
  "displayName.zh-CN": "工作流",
5
+ "displayName.ru-RU": "Рабочий процесс",
5
6
  "description": "A powerful BPM tool that provides foundational support for business automation, with the capability to extend unlimited triggers and nodes.",
6
7
  "description.zh-CN": "一个强大的 BPM 工具,为业务自动化提供基础支持,并且可任意扩展更多的触发器和节点。",
7
- "version": "1.9.0-beta.15",
8
+ "description.ru-RU": "Мощный инструмент BPM, обеспечивающий базовую поддержку автоматизации бизнес-процессов с возможностью неограниченного расширения триггеров и узлов.",
9
+ "version": "1.9.0-beta.17",
8
10
  "license": "AGPL-3.0",
9
11
  "main": "./dist/server/index.js",
10
12
  "homepage": "https://docs.nocobase.com/handbook/workflow",
11
13
  "homepage.zh-CN": "https://docs-cn.nocobase.com/handbook/workflow",
14
+ "homepage.ru-RU": "https://docs-ru.nocobase.com/handbook/workflow",
12
15
  "devDependencies": {
13
16
  "@ant-design/icons": "5.x",
14
17
  "@formily/antd-v5": "1.x",
@@ -45,7 +48,7 @@
45
48
  "@nocobase/test": "1.x",
46
49
  "@nocobase/utils": "1.x"
47
50
  },
48
- "gitHead": "f7285eff753fd268350cfd65cd079a35b8957f0e",
51
+ "gitHead": "4f95b676235fa3f7583493412279d8132a20c4d0",
49
52
  "keywords": [
50
53
  "Workflow"
51
54
  ]