@camunda8/orchestration-cluster-api 10.0.0-alpha.37 → 10.0.0-alpha.39

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.
@@ -111,6 +111,66 @@ var EventualConsistencyTimeoutError = class extends Error {
111
111
  }
112
112
  };
113
113
 
114
+ // src/runtime/clock.ts
115
+ var SLEW_DIVISOR = 16;
116
+ var MAX_TIMER_MS = 2147483647;
117
+ function scheduleLong(ms, onElapsed) {
118
+ let remaining = Math.max(0, ms);
119
+ let timer;
120
+ const step = () => {
121
+ const slice = Math.min(remaining, MAX_TIMER_MS);
122
+ remaining -= slice;
123
+ timer = setTimeout(remaining > 0 ? step : onElapsed, slice);
124
+ };
125
+ step();
126
+ return () => clearTimeout(timer);
127
+ }
128
+ function createLiveClock(source = Date.now) {
129
+ let lastSource = source();
130
+ let offsetMs = 0;
131
+ let slewCreditMs = 0;
132
+ const now2 = () => {
133
+ const observed = source();
134
+ if (observed < lastSource) {
135
+ offsetMs += lastSource - observed;
136
+ } else if (offsetMs > 0) {
137
+ slewCreditMs += observed - lastSource;
138
+ const repay = Math.floor(slewCreditMs / SLEW_DIVISOR);
139
+ if (repay > 0) {
140
+ slewCreditMs -= repay * SLEW_DIVISOR;
141
+ offsetMs -= Math.min(offsetMs, repay);
142
+ }
143
+ }
144
+ lastSource = observed;
145
+ return observed + offsetMs;
146
+ };
147
+ const sleep2 = (ms, signal) => new Promise((resolve, reject) => {
148
+ if (signal?.aborted) {
149
+ reject(signal.reason);
150
+ return;
151
+ }
152
+ const cancel = scheduleLong(ms, () => {
153
+ signal?.removeEventListener("abort", onAbort);
154
+ resolve();
155
+ });
156
+ function onAbort() {
157
+ cancel();
158
+ reject(signal?.reason);
159
+ }
160
+ signal?.addEventListener("abort", onAbort, { once: true });
161
+ });
162
+ const deadline = (ms) => {
163
+ const controller = new AbortController();
164
+ const cancel = scheduleLong(
165
+ ms,
166
+ () => controller.abort(new DOMException(`Timed out after ${ms}ms`, "TimeoutError"))
167
+ );
168
+ return { signal: controller.signal, dispose: cancel };
169
+ };
170
+ return { now: now2, sleep: sleep2, deadline };
171
+ }
172
+ var liveClock = createLiveClock();
173
+
114
174
  // src/runtime/typedVariables.ts
115
175
  var TypedVariablesError = class extends Error {
116
176
  constructor(message, options) {
@@ -344,6 +404,51 @@ function nextActivationRetryDelayMs(attempt, cfg, rng) {
344
404
  return Number.isFinite(cfg.pollIntervalMs) ? Math.max(0, Math.round(cfg.pollIntervalMs)) : 0;
345
405
  }
346
406
 
407
+ // src/runtime/workerGracefulStop.ts
408
+ async function stopWorkerGracefully(deps, opts) {
409
+ const waitUpToMs = opts?.waitUpToMs ?? 5e3;
410
+ const checkIntervalMs = opts?.checkIntervalMs ?? 10;
411
+ deps.haltPolling();
412
+ const start = liveClock.now();
413
+ const inFlight = deps.inFlightActivation();
414
+ if (inFlight) {
415
+ const budget = liveClock.deadline(waitUpToMs);
416
+ try {
417
+ await Promise.race([
418
+ inFlight,
419
+ new Promise((_, rej) => {
420
+ const reject = () => rej(new Error("activation.wait.timeout"));
421
+ if (budget.signal.aborted) reject();
422
+ else budget.signal.addEventListener("abort", reject, { once: true });
423
+ })
424
+ ]);
425
+ } catch (e) {
426
+ if (e && e.message === "activation.wait.timeout") {
427
+ deps.log.debug("worker.gracefulStop.activationTimeout");
428
+ }
429
+ } finally {
430
+ budget.dispose();
431
+ }
432
+ }
433
+ while (deps.activeJobs() > 0 && liveClock.now() - start < waitUpToMs) {
434
+ await liveClock.sleep(checkIntervalMs);
435
+ }
436
+ const timedOut = deps.activeJobs() > 0;
437
+ if (timedOut) {
438
+ const pending = deps.inFlightActivation();
439
+ if (pending?.cancel) {
440
+ try {
441
+ pending.cancel();
442
+ } catch {
443
+ }
444
+ }
445
+ deps.log.debug("worker.gracefulStop.timeout", { remaining: deps.activeJobs() });
446
+ } else {
447
+ deps.log.debug("worker.gracefulStop.done");
448
+ }
449
+ return { remainingJobs: deps.activeJobs(), timedOut };
450
+ }
451
+
347
452
  // src/runtime/workerStartGate.ts
348
453
  var WorkerStartGate = class {
349
454
  /**
@@ -490,42 +595,19 @@ var JobWorker = class {
490
595
  * If timeout is reached, falls back to hard stop logic (cancels activation if still pending).
491
596
  */
492
597
  async stopGracefully(opts) {
493
- const waitUpToMs = opts?.waitUpToMs ?? 5e3;
494
- const checkIntervalMs = opts?.checkIntervalMs ?? 10;
495
- this._stopped = true;
496
- if (this._pollTimer) clearTimeout(this._pollTimer);
497
- this._pollTimer = null;
498
- const start = Date.now();
499
- if (this._inFlightActivation) {
500
- try {
501
- await Promise.race([
502
- this._inFlightActivation,
503
- new Promise(
504
- (_, rej) => setTimeout(() => rej(new Error("activation.wait.timeout")), waitUpToMs)
505
- )
506
- ]);
507
- } catch (e) {
508
- if (e && e.message === "activation.wait.timeout") {
509
- this._log.debug("worker.gracefulStop.activationTimeout");
510
- }
511
- }
512
- }
513
- while (this._activeJobs > 0 && Date.now() - start < waitUpToMs) {
514
- await new Promise((r) => setTimeout(r, checkIntervalMs));
515
- }
516
- const timedOut = this._activeJobs > 0;
517
- if (timedOut) {
518
- if (this._inFlightActivation?.cancel) {
519
- try {
520
- this._inFlightActivation.cancel();
521
- } catch {
522
- }
523
- }
524
- this._log.debug("worker.gracefulStop.timeout", { remaining: this._activeJobs });
525
- } else {
526
- this._log.debug("worker.gracefulStop.done");
527
- }
528
- return { remainingJobs: this._activeJobs, timedOut };
598
+ return stopWorkerGracefully(
599
+ {
600
+ haltPolling: () => {
601
+ this._stopped = true;
602
+ if (this._pollTimer) clearTimeout(this._pollTimer);
603
+ this._pollTimer = null;
604
+ },
605
+ activeJobs: () => this._activeJobs,
606
+ inFlightActivation: () => this._inFlightActivation,
607
+ log: this._log
608
+ },
609
+ opts
610
+ );
529
611
  }
530
612
  _scheduleNext(delayMs) {
531
613
  if (this._stopped) return;
@@ -5173,7 +5255,7 @@ function installAuthInterceptor(client2, getStrategy, getAuthHeaders) {
5173
5255
  }
5174
5256
 
5175
5257
  // src/runtime/version.ts
5176
- var packageVersion = "10.0.0-alpha.37";
5258
+ var packageVersion = "10.0.0-alpha.39";
5177
5259
 
5178
5260
  // src/runtime/supportLogger.ts
5179
5261
  var NoopSupportLogger = class {
@@ -6369,42 +6451,19 @@ var ThreadedJobWorker = class {
6369
6451
  this._log.info("worker.stop");
6370
6452
  }
6371
6453
  async stopGracefully(opts) {
6372
- const waitUpToMs = opts?.waitUpToMs ?? 5e3;
6373
- const checkIntervalMs = opts?.checkIntervalMs ?? 10;
6374
- this._stopped = true;
6375
- if (this._pollTimer) clearTimeout(this._pollTimer);
6376
- this._pollTimer = null;
6377
- const start = Date.now();
6378
- if (this._inFlightActivation) {
6379
- try {
6380
- await Promise.race([
6381
- this._inFlightActivation,
6382
- new Promise(
6383
- (_, rej) => setTimeout(() => rej(new Error("activation.wait.timeout")), waitUpToMs)
6384
- )
6385
- ]);
6386
- } catch (e) {
6387
- if (e && e.message === "activation.wait.timeout") {
6388
- this._log.debug("worker.gracefulStop.activationTimeout");
6389
- }
6390
- }
6391
- }
6392
- while (this._activeJobs > 0 && Date.now() - start < waitUpToMs) {
6393
- await new Promise((r) => setTimeout(r, checkIntervalMs));
6394
- }
6395
- const timedOut = this._activeJobs > 0;
6396
- if (timedOut) {
6397
- if (this._inFlightActivation?.cancel) {
6398
- try {
6399
- this._inFlightActivation.cancel();
6400
- } catch {
6401
- }
6402
- }
6403
- this._log.debug("worker.gracefulStop.timeout", { remaining: this._activeJobs });
6404
- } else {
6405
- this._log.debug("worker.gracefulStop.done");
6406
- }
6407
- return { remainingJobs: this._activeJobs, timedOut };
6454
+ return stopWorkerGracefully(
6455
+ {
6456
+ haltPolling: () => {
6457
+ this._stopped = true;
6458
+ if (this._pollTimer) clearTimeout(this._pollTimer);
6459
+ this._pollTimer = null;
6460
+ },
6461
+ activeJobs: () => this._activeJobs,
6462
+ inFlightActivation: () => this._inFlightActivation,
6463
+ log: this._log
6464
+ },
6465
+ opts
6466
+ );
6408
6467
  }
6409
6468
  // ─── Job dispatch ───
6410
6469
  _drainQueue() {
@@ -7007,6 +7066,7 @@ var CamundaClientBase = class {
7007
7066
  _validation = new ValidationManager({ req: "none", res: "none" });
7008
7067
  _log = createLogger();
7009
7068
  _bp;
7069
+ _clock = liveClock;
7010
7070
  /** Registered job workers created via createJobWorker (lifecycle managed by user). */
7011
7071
  _workers = [];
7012
7072
  /** Shared thread pool for all threaded job workers (lazy-initialised on first use). */
@@ -7021,6 +7081,7 @@ var CamundaClientBase = class {
7021
7081
  _overrides = {};
7022
7082
  constructor(opts = {}) {
7023
7083
  if (opts.config) this._overrides = { ...opts.config };
7084
+ this._clock = opts.clock ?? liveClock;
7024
7085
  const { config } = hydrateConfig({ overrides: this._overrides, env: opts.env });
7025
7086
  this._config = deepFreeze2(config);
7026
7087
  this._log = createLogger({
@@ -7093,6 +7154,8 @@ var CamundaClientBase = class {
7093
7154
  this.emitSupportLogPreamble();
7094
7155
  this._bp = new BackpressureManager({
7095
7156
  logger: this._log.scope("bp"),
7157
+ now: () => this._clock.now(),
7158
+ sleep: (ms) => this._clock.sleep(ms),
7096
7159
  config: {
7097
7160
  enabled: this._config.backpressure.enabled,
7098
7161
  observeOnly: this._config.backpressure.observeOnly,
@@ -7295,6 +7358,10 @@ var CamundaClientBase = class {
7295
7358
  _evaluateResponse(raw, opId, buildBackpressureError) {
7296
7359
  return evaluateSdkResponse(raw, { opId, buildBackpressureError });
7297
7360
  }
7361
+ /** Clock backing SDK-internal cadence. The injected one when supplied, else the live clock. */
7362
+ get clock() {
7363
+ return this._clock;
7364
+ }
7298
7365
  /** Public accessor for current backpressure adaptive limiter state (stable) */
7299
7366
  getBackpressureState() {
7300
7367
  try {
@@ -21361,6 +21428,8 @@ export {
21361
21428
  isSdkError,
21362
21429
  CamundaValidationError,
21363
21430
  EventualConsistencyTimeoutError,
21431
+ createLiveClock,
21432
+ liveClock,
21364
21433
  TypedVariablesError,
21365
21434
  VariableScopeCollisionError,
21366
21435
  VariableDeserializationError,
@@ -21375,4 +21444,4 @@ export {
21375
21444
  createCamundaClient,
21376
21445
  CamundaClient
21377
21446
  };
21378
- //# sourceMappingURL=chunk-QMVXOINT.js.map
21447
+ //# sourceMappingURL=chunk-LNMC2FKC.js.map