codehooks-js 1.3.21 → 1.3.23

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.
Files changed (2) hide show
  1. package/package.json +3 -2
  2. package/workflow/engine.mjs +50 -11
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "codehooks-js",
3
- "version": "1.3.21",
3
+ "version": "1.3.23",
4
4
  "type": "module",
5
5
  "description": "Codehooks.io official library - provides express.JS like syntax",
6
6
  "main": "index.js",
@@ -22,7 +22,8 @@
22
22
  "tsconfig.json"
23
23
  ],
24
24
  "scripts": {
25
- "test": "echo \"Warning: no tests specified\""
25
+ "test": "echo \"Warning: no tests specified\"",
26
+ "generate-monaco-types": "node scripts/generate-monaco-types.cjs"
26
27
  },
27
28
  "author": "Codehooks",
28
29
  "license": "ISC",
@@ -165,6 +165,16 @@ class Workflow extends EventEmitter {
165
165
  return stepConfig?.workers ?? this.#defaultStepOptions.workers ?? 1;
166
166
  }
167
167
 
168
+ /**
169
+ * Get the timeout for a specific step
170
+ * @param {string} stepName - Name of the step
171
+ * @returns {number} The timeout in milliseconds for the step
172
+ */
173
+ getTimeoutForStep(stepName) {
174
+ const stepConfig = this.#steps[stepName];
175
+ return stepConfig?.timeout ?? this.#defaultStepOptions.timeout ?? this.#timeout;
176
+ }
177
+
168
178
  /**
169
179
  * Set the default number of workers for steps
170
180
  * @param {number} workers - Number of workers
@@ -185,6 +195,26 @@ class Workflow extends EventEmitter {
185
195
  return this.#defaultStepOptions.workers;
186
196
  }
187
197
 
198
+ /**
199
+ * Set the default timeout for steps
200
+ * @param {number} timeout - Timeout in milliseconds
201
+ * @throws {Error} If timeout is not a positive number
202
+ */
203
+ setDefaultTimeout(timeout) {
204
+ if (typeof timeout !== 'number' || timeout <= 0) {
205
+ throw new Error('Timeout must be a positive number');
206
+ }
207
+ this.#defaultStepOptions.timeout = timeout;
208
+ }
209
+
210
+ /**
211
+ * Get the default timeout for steps
212
+ * @returns {number} The default timeout in milliseconds
213
+ */
214
+ getDefaultTimeout() {
215
+ return this.#defaultStepOptions.timeout;
216
+ }
217
+
188
218
  /**
189
219
  * Configure the steps engine
190
220
  * @param {Object} config - Configuration object
@@ -204,6 +234,7 @@ class Workflow extends EventEmitter {
204
234
  }
205
235
  if (config.timeout) {
206
236
  this.setTimeout(config.timeout);
237
+ this.setDefaultTimeout(config.timeout);
207
238
  }
208
239
  if (config.maxStepCount) {
209
240
  this.setMaxStepCount(config.maxStepCount);
@@ -405,7 +436,7 @@ class Workflow extends EventEmitter {
405
436
  state: mergedState,
406
437
  options: options,
407
438
  instanceId: instanceId
408
- }, { workers: this.getWorkersForStep(step) });
439
+ }, { workers: this.getWorkersForStep(step), timeout: this.getTimeoutForStep(step) });
409
440
  }
410
441
  } else {
411
442
  console.debug('enqueue step', nextStep, instanceId);
@@ -415,7 +446,7 @@ class Workflow extends EventEmitter {
415
446
  state: mergedState,
416
447
  options: options,
417
448
  instanceId: instanceId
418
- }, { workers: this.getWorkersForStep(nextStep) });
449
+ }, { workers: this.getWorkersForStep(nextStep), timeout: this.getTimeoutForStep(nextStep) });
419
450
  }
420
451
  this.emit('stepEnqueued', { workflowName: stepsName, step: nextStep, state: newState, instanceId });
421
452
  resolve();
@@ -466,12 +497,21 @@ class Workflow extends EventEmitter {
466
497
  async registerWithApp(app, name, description, definition) {
467
498
  this.emit('workflowCreated', { name, description });
468
499
 
500
+ // Log current configuration state at registration time
501
+ console.debug('Workflow registration config:', {
502
+ defaultWorkers: this.#defaultStepOptions.workers,
503
+ defaultTimeout: this.#defaultStepOptions.timeout,
504
+ stepsConfig: this.#steps
505
+ });
506
+
469
507
  // Validate each step in the definition
470
508
  for (const [stepName, step] of Object.entries(definition)) {
471
509
  try {
472
510
  // Skip null, undefined, or invalid step names
473
511
  if (stepName !== undefined && stepName !== null && stepName !== 'null' && stepName.trim() !== '') {
474
- console.debug('registering queue for step', `${this.#queuePrefix}_${name}_${stepName}`);
512
+ const workerCount = this.getWorkersForStep(stepName);
513
+ const timeoutMs = this.getTimeoutForStep(stepName);
514
+ console.debug('registering queue for step', `${this.#queuePrefix}_${name}_${stepName}`, { workers: workerCount, timeout: timeoutMs, stepConfig: this.#steps[stepName] });
475
515
  app.worker(`${this.#queuePrefix}_${name}_${stepName}`, async (req, res) => {
476
516
  try {
477
517
  const { stepsName, goto, state, instanceId, options } = req.body.payload;
@@ -492,7 +532,7 @@ class Workflow extends EventEmitter {
492
532
  //console.debug('Worker res.end', stepName);
493
533
  res.end();
494
534
  }
495
- });
535
+ }, { workers: workerCount, timeout: timeoutMs });
496
536
  }
497
537
  } catch (error) {
498
538
  console.error(`Invalid step definition '${stepName}': ${error.message}`);
@@ -543,7 +583,7 @@ class Workflow extends EventEmitter {
543
583
  state: newState,
544
584
  instanceId: newState._id,
545
585
  options: {}
546
- }, { workers: this.getWorkersForStep(firstStepName) });
586
+ }, { workers: this.getWorkersForStep(firstStepName), timeout: this.getTimeoutForStep(firstStepName) });
547
587
  resolve(newState);
548
588
  } catch (error) {
549
589
  console.error('Error starting workflow:', error.message);
@@ -624,7 +664,7 @@ class Workflow extends EventEmitter {
624
664
  state: state,
625
665
  options: {},
626
666
  instanceId: instanceId
627
- }, { workers: this.getWorkersForStep(state.nextStep) });
667
+ }, { workers: this.getWorkersForStep(state.nextStep), timeout: this.getTimeoutForStep(state.nextStep) });
628
668
 
629
669
  resolve({ instanceId });
630
670
  });
@@ -744,10 +784,9 @@ class Workflow extends EventEmitter {
744
784
 
745
785
  const step = workflow.stepCount[stepToCheck];
746
786
 
747
- // Get the timeout value for this step
748
- // First try step-specific config, then default options, finally fallback to global timeout
787
+ // Get the timeout value for this step using the helper method
749
788
  const stepConfig = this.#steps[stepToCheck];
750
- const stepTimeout = stepConfig?.timeout ?? this.#defaultStepOptions.timeout ?? this.#timeout;
789
+ const stepTimeout = this.getTimeoutForStep(stepToCheck);
751
790
 
752
791
  // If the step hasn't finished, check if it's been running too long
753
792
  console.debug('isStepTimedOut', stepToCheck, stepTimeout);
@@ -763,7 +802,7 @@ class Workflow extends EventEmitter {
763
802
  step: stepToCheck,
764
803
  startTime: step.startTime,
765
804
  currentTime: now.toISOString(),
766
- timeoutSource: stepConfig ? 'stepConfig' : (this.#defaultStepOptions.timeout ? 'defaultOptions' : 'globalTimeout')
805
+ timeoutSource: stepConfig?.timeout ? 'stepConfig' : (this.#defaultStepOptions.timeout !== 30000 ? 'defaultOptions' : 'globalTimeout')
767
806
  };
768
807
  }
769
808
 
@@ -779,7 +818,7 @@ class Workflow extends EventEmitter {
779
818
  step: stepToCheck,
780
819
  startTime: step.startTime,
781
820
  finishTime: step.finishTime,
782
- timeoutSource: stepConfig ? 'stepConfig' : (this.#defaultStepOptions.timeout ? 'defaultOptions' : 'globalTimeout')
821
+ timeoutSource: stepConfig?.timeout ? 'stepConfig' : (this.#defaultStepOptions.timeout !== 30000 ? 'defaultOptions' : 'globalTimeout')
783
822
  };
784
823
  }
785
824