@dudousxd/nestjs-durable-core 0.86.1 → 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
@@ -5419,6 +5419,12 @@ var WorkflowEngine = class {
5419
5419
  * releases when its budget runs out.
5420
5420
  */
5421
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();
5422
5428
  /** Pager hooks for stranded runs (see {@link onStalled}). */
5423
5429
  stalledListeners = /* @__PURE__ */ new Set();
5424
5430
  /** Runs already paged about, by id → the `updatedAt` of the paged episode (see {@link sweepStalled}). */
@@ -5762,6 +5768,27 @@ var WorkflowEngine = class {
5762
5768
  console.error(`[nestjs-durable] could not run queued run ${runId}:`, error2);
5763
5769
  }
5764
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
+ }
5765
5792
  /**
5766
5793
  * Hand due runs (timers, blocked runs due for a fleet re-check) to whichever instance takes them
5767
5794
  * off the run queue. Each is claimed first, under its lease, by pushing its `wakeAt` one lease
@@ -5773,29 +5800,34 @@ var WorkflowEngine = class {
5773
5800
  const claimed = [];
5774
5801
  for (const run of runs) {
5775
5802
  if (this.draining) break;
5776
- if (!await this.store.tryLockRun(run.id, this.instanceId, nowMs + this.leaseMs, nowMs)) {
5777
- continue;
5778
- }
5779
- if (!this.isRegisteredHere(run) && !await this.canResolveWorkflow(run)) {
5780
- const result = await this.settleUnregistered(run, nowMs);
5781
- if (result) settled?.set(run.id, result);
5782
- else if (result === void 0) this.logUnregisteredSkip(run);
5783
- continue;
5784
- }
5803
+ this.markClaim(run.id);
5785
5804
  try {
5786
- const patch = {
5787
- wakeAt: nowMs + this.leaseMs
5788
- };
5789
- if (this.store.updateRunIf) {
5790
- if (await this.store.updateRunIf(run.id, [
5791
- run.status
5792
- ], patch)) claimed.push(run);
5793
- } else {
5794
- await this.store.updateRun(run.id, patch);
5795
- 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);
5796
5828
  }
5797
5829
  } finally {
5798
- await this.store.releaseRunLock(run.id);
5830
+ this.unmarkClaim(run.id);
5799
5831
  }
5800
5832
  }
5801
5833
  try {
@@ -7250,7 +7282,8 @@ var WorkflowEngine = class {
7250
7282
  const nowMs = this.clock();
7251
7283
  const acquired = await this.store.tryLockRun(runId, this.instanceId, nowMs + this.leaseMs, nowMs);
7252
7284
  if (!acquired) return null;
7253
- 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) => {
7254
7287
  if (err instanceof NamespaceMismatch) return null;
7255
7288
  if (err instanceof WorkflowNotRegistered) {
7256
7289
  const run = await this.store.getRun(runId);
@@ -7534,6 +7567,7 @@ var WorkflowEngine = class {
7534
7567
  }
7535
7568
  try {
7536
7569
  const settled = onLocked ? await onLocked(run) : void 0;
7570
+ if (!settled) this.lockedForTurn.add(run.id);
7537
7571
  results.push(settled ?? await this.resumeImplicit(run.id));
7538
7572
  } catch (error2) {
7539
7573
  if (error2 instanceof WorkflowNotRegistered) {
@@ -7542,6 +7576,8 @@ var WorkflowEngine = class {
7542
7576
  if (settled !== void 0) continue;
7543
7577
  }
7544
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);
7545
7581
  }
7546
7582
  }
7547
7583
  return results;
@@ -8583,7 +8619,9 @@ var WorkflowEngine = class {
8583
8619
  fans: /* @__PURE__ */ new Map()
8584
8620
  };
8585
8621
  this.wakeReasons.delete(run.id);
8586
- 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) {
8587
8625
  if (!await this.store.tryLockRun(run.id, this.instanceId, lockNow + this.leaseMs, lockNow)) {
8588
8626
  if (reason.generic) {
8589
8627
  if (!this.genericWakeRequested.has(run.id)) {