codehooks-js 1.3.20 → 1.3.21

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.21",
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();
@@ -508,7 +543,7 @@ class Workflow extends EventEmitter {
508
543
  state: newState,
509
544
  instanceId: newState._id,
510
545
  options: {}
511
- });
546
+ }, { workers: this.getWorkersForStep(firstStepName) });
512
547
  resolve(newState);
513
548
  } catch (error) {
514
549
  console.error('Error starting workflow:', error.message);
@@ -589,7 +624,7 @@ class Workflow extends EventEmitter {
589
624
  state: state,
590
625
  options: {},
591
626
  instanceId: instanceId
592
- });
627
+ }, { workers: this.getWorkersForStep(state.nextStep) });
593
628
 
594
629
  resolve({ instanceId });
595
630
  });