@dudousxd/nestjs-durable-core 0.86.0 → 0.86.1

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/dist/index.cjs CHANGED
@@ -67,6 +67,7 @@ __export(index_exports, {
67
67
  RunGateway: () => RunGateway,
68
68
  SINGLETON_ACTIVE_STATUSES: () => SINGLETON_ACTIVE_STATUSES,
69
69
  SINGLETON_ADMITTED_TAG: () => SINGLETON_ADMITTED_TAG,
70
+ SINGLETON_LOCK_CHUNK: () => SINGLETON_LOCK_CHUNK,
70
71
  SINGLETON_RESERVED_TAGS: () => SINGLETON_RESERVED_TAGS,
71
72
  SINGLETON_TRACKED_TAG: () => SINGLETON_TRACKED_TAG,
72
73
  STATE_STORE: () => STATE_STORE,
@@ -200,6 +201,7 @@ __export(index_exports, {
200
201
  signTenantToken: () => signTenantToken,
201
202
  singletonAdmissionDenialPatch: () => singletonAdmissionDenialPatch,
202
203
  singletonAdmissionTags: () => singletonAdmissionTags,
204
+ singletonClaimantIds: () => singletonClaimantIds,
203
205
  sqlComparator: () => sqlComparator,
204
206
  stepConfigOf: () => stepConfigOf,
205
207
  stepId: () => stepId,
@@ -3486,6 +3488,11 @@ function legacySingletonHolders(active, runId, limit) {
3486
3488
  return scanned.filter((run) => run.id !== runId && !run.tags?.includes(SINGLETON_ADMITTED_TAG) && (!run.tags?.includes(SINGLETON_TRACKED_TAG) || window.has(run.id)));
3487
3489
  }
3488
3490
  __name(legacySingletonHolders, "legacySingletonHolders");
3491
+ var SINGLETON_LOCK_CHUNK = 500;
3492
+ function singletonClaimantIds(rows, tag) {
3493
+ return rows.filter((row) => Array.isArray(row.tags) && row.tags.includes(tag)).map((row) => row.id);
3494
+ }
3495
+ __name(singletonClaimantIds, "singletonClaimantIds");
3489
3496
  function singletonAdmissionTags(runs, runId, tag, workflow, limit) {
3490
3497
  if (!Number.isInteger(limit) || limit < 1) throw new Error("singleton limit must be a positive integer");
3491
3498
  const active = runs.filter((run) => run.workflow === workflow && SINGLETON_ACTIVE_STATUSES.includes(run.status) && run.tags?.includes(tag));
@@ -3530,6 +3537,41 @@ __name(singletonAdmissionDenialPatch, "singletonAdmissionDenialPatch");
3530
3537
  // src/singleton-gate.ts
3531
3538
  var SINGLETON_RETRY_MS = 1e3;
3532
3539
  var SINGLETON_RETRY_JITTER_MS = 250;
3540
+ var ADMISSION_ATTEMPTS = 3;
3541
+ var ADMISSION_BACKOFF_MS = 40;
3542
+ var LOCK_CONFLICT_CODES = /* @__PURE__ */ new Set([
3543
+ "40P01",
3544
+ "40001",
3545
+ "ER_LOCK_DEADLOCK",
3546
+ "1213",
3547
+ "P2034",
3548
+ "TransactionWriteConflict"
3549
+ ]);
3550
+ function isLockConflict(error2) {
3551
+ const seen = /* @__PURE__ */ new Set();
3552
+ const pending = [
3553
+ error2
3554
+ ];
3555
+ while (pending.length > 0) {
3556
+ const current = pending.pop();
3557
+ if (typeof current !== "object" || current === null || seen.has(current)) continue;
3558
+ seen.add(current);
3559
+ const e = current;
3560
+ for (const code of [
3561
+ e.code,
3562
+ e.errno,
3563
+ e.sqlState,
3564
+ e.sqlstate,
3565
+ e.originalCode,
3566
+ e.kind
3567
+ ]) {
3568
+ if (LOCK_CONFLICT_CODES.has(String(code))) return true;
3569
+ }
3570
+ pending.push(e.cause, e.driverError, e.originalError, e.meta, e.driverAdapterError);
3571
+ }
3572
+ return false;
3573
+ }
3574
+ __name(isLockConflict, "isLockConflict");
3533
3575
  var SingletonGate = class {
3534
3576
  static {
3535
3577
  __name(this, "SingletonGate");
@@ -3571,7 +3613,7 @@ var SingletonGate = class {
3571
3613
  if (!this.deps.store.tryAdmitSingleton) {
3572
3614
  throw new Error("Singleton workflows require StateStore.tryAdmitSingleton for atomic durable admission");
3573
3615
  }
3574
- const admitted = await this.deps.store.tryAdmitSingleton(run.id, this.tag(cfg, run.input), run.workflow, cfg.limit ?? 1, this.retryWakeAt());
3616
+ const admitted = await this.claim(run, cfg);
3575
3617
  if (admitted && !run.tags?.includes(SINGLETON_ADMITTED_TAG)) {
3576
3618
  run.tags = [
3577
3619
  ...(run.tags ?? []).filter((tag) => tag !== SINGLETON_TRACKED_TAG),
@@ -3581,6 +3623,26 @@ var SingletonGate = class {
3581
3623
  return admitted;
3582
3624
  }
3583
3625
  /**
3626
+ * One claim, retried when the database aborts it as a lock conflict. A conflict that outlasts the
3627
+ * retries answers "not admitted", so the engine releases this turn's lease, instead of stranding
3628
+ * the run until the lease lapses. It writes nothing to the run: this turn's lease may have lapsed
3629
+ * during the claim, and another instance may now hold the run, its slot and newer tags. A later
3630
+ * sweep re-drives the run: recovery once it is unleased, or its timer if it is parked.
3631
+ */
3632
+ async claim(run, cfg) {
3633
+ const admit = this.deps.store.tryAdmitSingleton;
3634
+ for (let attempt = 1; ; attempt += 1) {
3635
+ try {
3636
+ return await admit.call(this.deps.store, run.id, this.tag(cfg, run.input), run.workflow, cfg.limit ?? 1, this.retryWakeAt());
3637
+ } catch (error2) {
3638
+ if (!isLockConflict(error2)) throw error2;
3639
+ if (attempt >= ADMISSION_ATTEMPTS) return false;
3640
+ const backoff = ADMISSION_BACKOFF_MS * attempt * (0.5 + Math.random());
3641
+ await new Promise((resolve) => setTimeout(resolve, backoff));
3642
+ }
3643
+ }
3644
+ }
3645
+ /**
3584
3646
  * Notify-on-release: a singleton run settled (freeing a slot), so dispatch the oldest gated
3585
3647
  * (`suspended` + same tag) waiters now instead of waiting for their ~1s retry timer. Each re-checks
3586
3648
  * admission in the executor and runs only if it actually wins a slot, so FIFO/race-free guarantees
@@ -8495,7 +8557,7 @@ var WorkflowEngine = class {
8495
8557
  const current = await this.store.getRun(run.id);
8496
8558
  if (!this.isFenced(run.id)) {
8497
8559
  this.handOffLease(run.id);
8498
- await this.store.releaseRunLock(run.id);
8560
+ await this.store.releaseRunLock(run.id, this.instanceId);
8499
8561
  }
8500
8562
  if (!current) throw new Error(`run ${run.id} was deleted during singleton admission`);
8501
8563
  if (current.status === "suspended") {
@@ -13222,6 +13284,7 @@ var InMemoryRunQueue = class {
13222
13284
  RunGateway,
13223
13285
  SINGLETON_ACTIVE_STATUSES,
13224
13286
  SINGLETON_ADMITTED_TAG,
13287
+ SINGLETON_LOCK_CHUNK,
13225
13288
  SINGLETON_RESERVED_TAGS,
13226
13289
  SINGLETON_TRACKED_TAG,
13227
13290
  STATE_STORE,
@@ -13355,6 +13418,7 @@ var InMemoryRunQueue = class {
13355
13418
  signTenantToken,
13356
13419
  singletonAdmissionDenialPatch,
13357
13420
  singletonAdmissionTags,
13421
+ singletonClaimantIds,
13358
13422
  sqlComparator,
13359
13423
  stepConfigOf,
13360
13424
  stepId,