@dudousxd/nestjs-durable-core 0.86.0 → 0.86.2

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
@@ -5357,6 +5419,12 @@ var WorkflowEngine = class {
5357
5419
  * releases when its budget runs out.
5358
5420
  */
5359
5421
  executionLeases = /* @__PURE__ */ new Map();
5422
+ /** Runs a caller just locked to execute now, so their turn has nothing to prove and no await to add. */
5423
+ lockedForTurn = /* @__PURE__ */ new Set();
5424
+ /** Runs this instance holds for a moment and will release, not to execute (see {@link handOffDue}). */
5425
+ claimsInFlight = /* @__PURE__ */ new Map();
5426
+ /** How many such claims each run has had, so a proof can tell one came and went (see {@link proveOwnLease}). */
5427
+ claimsMade = /* @__PURE__ */ new Map();
5360
5428
  /** Pager hooks for stranded runs (see {@link onStalled}). */
5361
5429
  stalledListeners = /* @__PURE__ */ new Set();
5362
5430
  /** Runs already paged about, by id → the `updatedAt` of the paged episode (see {@link sweepStalled}). */
@@ -5700,6 +5768,27 @@ var WorkflowEngine = class {
5700
5768
  console.error(`[nestjs-durable] could not run queued run ${runId}:`, error2);
5701
5769
  }
5702
5770
  }
5771
+ markClaim(runId) {
5772
+ this.claimsInFlight.set(runId, (this.claimsInFlight.get(runId) ?? 0) + 1);
5773
+ if (this.claimsMade.size >= 4096) this.claimsMade.clear();
5774
+ this.claimsMade.set(runId, (this.claimsMade.get(runId) ?? 0) + 1);
5775
+ }
5776
+ /**
5777
+ * Whether this instance holds the run's lease for a turn, from the store and by owner. A renewal can land
5778
+ * on a claim marked after it was sent, and that claim is about to be released, so any claim begun while
5779
+ * the renewal was out, even one already over, leaves it unproved.
5780
+ */
5781
+ async proveOwnLease(runId, nowMs) {
5782
+ if (this.claimsInFlight.has(runId)) return false;
5783
+ const before = this.claimsMade.get(runId);
5784
+ const renewed = await this.store.renewRunLock(runId, this.instanceId, nowMs + this.leaseMs);
5785
+ return renewed && !this.claimsInFlight.has(runId) && this.claimsMade.get(runId) === before;
5786
+ }
5787
+ unmarkClaim(runId) {
5788
+ const left = (this.claimsInFlight.get(runId) ?? 1) - 1;
5789
+ if (left > 0) this.claimsInFlight.set(runId, left);
5790
+ else this.claimsInFlight.delete(runId);
5791
+ }
5703
5792
  /**
5704
5793
  * Hand due runs (timers, blocked runs due for a fleet re-check) to whichever instance takes them
5705
5794
  * off the run queue. Each is claimed first, under its lease, by pushing its `wakeAt` one lease
@@ -5711,29 +5800,34 @@ var WorkflowEngine = class {
5711
5800
  const claimed = [];
5712
5801
  for (const run of runs) {
5713
5802
  if (this.draining) break;
5714
- if (!await this.store.tryLockRun(run.id, this.instanceId, nowMs + this.leaseMs, nowMs)) {
5715
- continue;
5716
- }
5717
- if (!this.isRegisteredHere(run) && !await this.canResolveWorkflow(run)) {
5718
- const result = await this.settleUnregistered(run, nowMs);
5719
- if (result) settled?.set(run.id, result);
5720
- else if (result === void 0) this.logUnregisteredSkip(run);
5721
- continue;
5722
- }
5803
+ this.markClaim(run.id);
5723
5804
  try {
5724
- const patch = {
5725
- wakeAt: nowMs + this.leaseMs
5726
- };
5727
- if (this.store.updateRunIf) {
5728
- if (await this.store.updateRunIf(run.id, [
5729
- run.status
5730
- ], patch)) claimed.push(run);
5731
- } else {
5732
- await this.store.updateRun(run.id, patch);
5733
- claimed.push(run);
5805
+ if (!await this.store.tryLockRun(run.id, this.instanceId, nowMs + this.leaseMs, nowMs)) {
5806
+ continue;
5807
+ }
5808
+ if (!this.isRegisteredHere(run) && !await this.canResolveWorkflow(run)) {
5809
+ const result = await this.settleUnregistered(run, nowMs);
5810
+ if (result) settled?.set(run.id, result);
5811
+ else if (result === void 0) this.logUnregisteredSkip(run);
5812
+ continue;
5813
+ }
5814
+ try {
5815
+ const patch = {
5816
+ wakeAt: nowMs + this.leaseMs
5817
+ };
5818
+ if (this.store.updateRunIf) {
5819
+ if (await this.store.updateRunIf(run.id, [
5820
+ run.status
5821
+ ], patch)) claimed.push(run);
5822
+ } else {
5823
+ await this.store.updateRun(run.id, patch);
5824
+ claimed.push(run);
5825
+ }
5826
+ } finally {
5827
+ await this.store.releaseRunLock(run.id, this.instanceId);
5734
5828
  }
5735
5829
  } finally {
5736
- await this.store.releaseRunLock(run.id);
5830
+ this.unmarkClaim(run.id);
5737
5831
  }
5738
5832
  }
5739
5833
  try {
@@ -7188,7 +7282,8 @@ var WorkflowEngine = class {
7188
7282
  const nowMs = this.clock();
7189
7283
  const acquired = await this.store.tryLockRun(runId, this.instanceId, nowMs + this.leaseMs, nowMs);
7190
7284
  if (!acquired) return null;
7191
- return this.resumeImplicit(runId).catch(async (err) => {
7285
+ this.lockedForTurn.add(runId);
7286
+ return this.resumeImplicit(runId).finally(() => this.lockedForTurn.delete(runId)).catch(async (err) => {
7192
7287
  if (err instanceof NamespaceMismatch) return null;
7193
7288
  if (err instanceof WorkflowNotRegistered) {
7194
7289
  const run = await this.store.getRun(runId);
@@ -7472,6 +7567,7 @@ var WorkflowEngine = class {
7472
7567
  }
7473
7568
  try {
7474
7569
  const settled = onLocked ? await onLocked(run) : void 0;
7570
+ if (!settled) this.lockedForTurn.add(run.id);
7475
7571
  results.push(settled ?? await this.resumeImplicit(run.id));
7476
7572
  } catch (error2) {
7477
7573
  if (error2 instanceof WorkflowNotRegistered) {
@@ -7480,6 +7576,8 @@ var WorkflowEngine = class {
7480
7576
  if (settled !== void 0) continue;
7481
7577
  }
7482
7578
  console.error(`[nestjs-durable] could not resume run ${run.id} (${run.workflow}@${run.workflowVersion}); leaving it for another instance:`, error2);
7579
+ } finally {
7580
+ this.lockedForTurn.delete(run.id);
7483
7581
  }
7484
7582
  }
7485
7583
  return results;
@@ -8495,7 +8593,7 @@ var WorkflowEngine = class {
8495
8593
  const current = await this.store.getRun(run.id);
8496
8594
  if (!this.isFenced(run.id)) {
8497
8595
  this.handOffLease(run.id);
8498
- await this.store.releaseRunLock(run.id);
8596
+ await this.store.releaseRunLock(run.id, this.instanceId);
8499
8597
  }
8500
8598
  if (!current) throw new Error(`run ${run.id} was deleted during singleton admission`);
8501
8599
  if (current.status === "suspended") {
@@ -8521,7 +8619,9 @@ var WorkflowEngine = class {
8521
8619
  fans: /* @__PURE__ */ new Map()
8522
8620
  };
8523
8621
  this.wakeReasons.delete(run.id);
8524
- if (run.lockedBy !== this.instanceId || (run.lockedUntil ?? 0) <= lockNow) {
8622
+ const lockedForTurn = this.lockedForTurn.delete(run.id);
8623
+ const reentered = run.lockedBy === this.instanceId && (run.lockedUntil ?? 0) > lockNow && (lockedForTurn || await this.proveOwnLease(run.id, lockNow));
8624
+ if (!reentered) {
8525
8625
  if (!await this.store.tryLockRun(run.id, this.instanceId, lockNow + this.leaseMs, lockNow)) {
8526
8626
  if (reason.generic) {
8527
8627
  if (!this.genericWakeRequested.has(run.id)) {
@@ -13222,6 +13322,7 @@ var InMemoryRunQueue = class {
13222
13322
  RunGateway,
13223
13323
  SINGLETON_ACTIVE_STATUSES,
13224
13324
  SINGLETON_ADMITTED_TAG,
13325
+ SINGLETON_LOCK_CHUNK,
13225
13326
  SINGLETON_RESERVED_TAGS,
13226
13327
  SINGLETON_TRACKED_TAG,
13227
13328
  STATE_STORE,
@@ -13355,6 +13456,7 @@ var InMemoryRunQueue = class {
13355
13456
  signTenantToken,
13356
13457
  singletonAdmissionDenialPatch,
13357
13458
  singletonAdmissionTags,
13459
+ singletonClaimantIds,
13358
13460
  sqlComparator,
13359
13461
  stepConfigOf,
13360
13462
  stepId,