codehooks-js 1.3.20 → 1.3.22

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "codehooks-js",
3
- "version": "1.3.20",
3
+ "version": "1.3.22",
4
4
  "type": "module",
5
5
  "description": "Codehooks.io official library - provides express.JS like syntax",
6
6
  "main": "index.js",
@@ -23,7 +23,8 @@ class Workflow extends EventEmitter {
23
23
  #description = null; // workflow description
24
24
  #defaultStepOptions = { // default step options
25
25
  timeout: 30000, // timeout for a step
26
- maxRetries: 3 // maximum number of retries for a step
26
+ maxRetries: 3, // maximum number of retries for a step
27
+ workers: 1 // number of workers for a step
27
28
  };
28
29
 
29
30
  /**
@@ -154,6 +155,36 @@ class Workflow extends EventEmitter {
154
155
  return this.#steps;
155
156
  }
156
157
 
158
+ /**
159
+ * Get the number of workers for a specific step
160
+ * @param {string} stepName - Name of the step
161
+ * @returns {number} The number of workers for the step
162
+ */
163
+ getWorkersForStep(stepName) {
164
+ const stepConfig = this.#steps[stepName];
165
+ return stepConfig?.workers ?? this.#defaultStepOptions.workers ?? 1;
166
+ }
167
+
168
+ /**
169
+ * Set the default number of workers for steps
170
+ * @param {number} workers - Number of workers
171
+ * @throws {Error} If workers is not a positive number
172
+ */
173
+ setDefaultWorkers(workers) {
174
+ if (typeof workers !== 'number' || workers <= 0) {
175
+ throw new Error('Workers must be a positive number');
176
+ }
177
+ this.#defaultStepOptions.workers = workers;
178
+ }
179
+
180
+ /**
181
+ * Get the default number of workers
182
+ * @returns {number} The default number of workers
183
+ */
184
+ getDefaultWorkers() {
185
+ return this.#defaultStepOptions.workers;
186
+ }
187
+
157
188
  /**
158
189
  * Configure the steps engine
159
190
  * @param {Object} config - Configuration object
@@ -161,6 +192,7 @@ class Workflow extends EventEmitter {
161
192
  * @param {string} config.queuePrefix - Queue prefix
162
193
  * @param {number} config.timeout - Timeout in milliseconds
163
194
  * @param {number} config.maxStepCount - Maximum step count
195
+ * @param {number} config.workers - Default number of workers for steps
164
196
  * @param {Object} config.steps - Steps configuration
165
197
  */
166
198
  configure(config) {
@@ -176,6 +208,9 @@ class Workflow extends EventEmitter {
176
208
  if (config.maxStepCount) {
177
209
  this.setMaxStepCount(config.maxStepCount);
178
210
  }
211
+ if (config.workers) {
212
+ this.setDefaultWorkers(config.workers);
213
+ }
179
214
  if (config.steps) {
180
215
  this.setStepsConfig(config.steps);
181
216
  }
@@ -370,7 +405,7 @@ class Workflow extends EventEmitter {
370
405
  state: mergedState,
371
406
  options: options,
372
407
  instanceId: instanceId
373
- });
408
+ }, { workers: this.getWorkersForStep(step) });
374
409
  }
375
410
  } else {
376
411
  console.debug('enqueue step', nextStep, instanceId);
@@ -380,7 +415,7 @@ class Workflow extends EventEmitter {
380
415
  state: mergedState,
381
416
  options: options,
382
417
  instanceId: instanceId
383
- });
418
+ }, { workers: this.getWorkersForStep(nextStep) });
384
419
  }
385
420
  this.emit('stepEnqueued', { workflowName: stepsName, step: nextStep, state: newState, instanceId });
386
421
  resolve();
@@ -431,12 +466,19 @@ class Workflow extends EventEmitter {
431
466
  async registerWithApp(app, name, description, definition) {
432
467
  this.emit('workflowCreated', { name, description });
433
468
 
469
+ // Log current configuration state at registration time
470
+ console.debug('Workflow registration config:', {
471
+ defaultWorkers: this.#defaultStepOptions.workers,
472
+ stepsConfig: this.#steps
473
+ });
474
+
434
475
  // Validate each step in the definition
435
476
  for (const [stepName, step] of Object.entries(definition)) {
436
477
  try {
437
478
  // Skip null, undefined, or invalid step names
438
479
  if (stepName !== undefined && stepName !== null && stepName !== 'null' && stepName.trim() !== '') {
439
- console.debug('registering queue for step', `${this.#queuePrefix}_${name}_${stepName}`);
480
+ const workerCount = this.getWorkersForStep(stepName);
481
+ console.debug('registering queue for step', `${this.#queuePrefix}_${name}_${stepName}`, { workers: workerCount, stepConfig: this.#steps[stepName] });
440
482
  app.worker(`${this.#queuePrefix}_${name}_${stepName}`, async (req, res) => {
441
483
  try {
442
484
  const { stepsName, goto, state, instanceId, options } = req.body.payload;
@@ -457,7 +499,7 @@ class Workflow extends EventEmitter {
457
499
  //console.debug('Worker res.end', stepName);
458
500
  res.end();
459
501
  }
460
- });
502
+ }, { workers: workerCount });
461
503
  }
462
504
  } catch (error) {
463
505
  console.error(`Invalid step definition '${stepName}': ${error.message}`);
@@ -508,7 +550,7 @@ class Workflow extends EventEmitter {
508
550
  state: newState,
509
551
  instanceId: newState._id,
510
552
  options: {}
511
- });
553
+ }, { workers: this.getWorkersForStep(firstStepName) });
512
554
  resolve(newState);
513
555
  } catch (error) {
514
556
  console.error('Error starting workflow:', error.message);
@@ -589,7 +631,7 @@ class Workflow extends EventEmitter {
589
631
  state: state,
590
632
  options: {},
591
633
  instanceId: instanceId
592
- });
634
+ }, { workers: this.getWorkersForStep(state.nextStep) });
593
635
 
594
636
  resolve({ instanceId });
595
637
  });