@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.d.cts CHANGED
@@ -4825,6 +4825,12 @@ declare class WorkflowEngine {
4825
4825
  * releases when its budget runs out.
4826
4826
  */
4827
4827
  private readonly executionLeases;
4828
+ /** Runs a caller just locked to execute now, so their turn has nothing to prove and no await to add. */
4829
+ private readonly lockedForTurn;
4830
+ /** Runs this instance holds for a moment and will release, not to execute (see {@link handOffDue}). */
4831
+ private readonly claimsInFlight;
4832
+ /** How many such claims each run has had, so a proof can tell one came and went (see {@link proveOwnLease}). */
4833
+ private readonly claimsMade;
4828
4834
  /** Pager hooks for stranded runs (see {@link onStalled}). */
4829
4835
  private readonly stalledListeners;
4830
4836
  /** Runs already paged about, by id → the `updatedAt` of the paged episode (see {@link sweepStalled}). */
@@ -4900,6 +4906,14 @@ declare class WorkflowEngine {
4900
4906
  * one executor either way. Never rejects.
4901
4907
  */
4902
4908
  private runFromQueue;
4909
+ private markClaim;
4910
+ /**
4911
+ * Whether this instance holds the run's lease for a turn, from the store and by owner. A renewal can land
4912
+ * on a claim marked after it was sent, and that claim is about to be released, so any claim begun while
4913
+ * the renewal was out, even one already over, leaves it unproved.
4914
+ */
4915
+ private proveOwnLease;
4916
+ private unmarkClaim;
4903
4917
  /**
4904
4918
  * Hand due runs (timers, blocked runs due for a fleet re-check) to whichever instance takes them
4905
4919
  * off the run queue. Each is claimed first, under its lease, by pushing its `wakeAt` one lease
package/dist/index.d.ts CHANGED
@@ -4825,6 +4825,12 @@ declare class WorkflowEngine {
4825
4825
  * releases when its budget runs out.
4826
4826
  */
4827
4827
  private readonly executionLeases;
4828
+ /** Runs a caller just locked to execute now, so their turn has nothing to prove and no await to add. */
4829
+ private readonly lockedForTurn;
4830
+ /** Runs this instance holds for a moment and will release, not to execute (see {@link handOffDue}). */
4831
+ private readonly claimsInFlight;
4832
+ /** How many such claims each run has had, so a proof can tell one came and went (see {@link proveOwnLease}). */
4833
+ private readonly claimsMade;
4828
4834
  /** Pager hooks for stranded runs (see {@link onStalled}). */
4829
4835
  private readonly stalledListeners;
4830
4836
  /** Runs already paged about, by id → the `updatedAt` of the paged episode (see {@link sweepStalled}). */
@@ -4900,6 +4906,14 @@ declare class WorkflowEngine {
4900
4906
  * one executor either way. Never rejects.
4901
4907
  */
4902
4908
  private runFromQueue;
4909
+ private markClaim;
4910
+ /**
4911
+ * Whether this instance holds the run's lease for a turn, from the store and by owner. A renewal can land
4912
+ * on a claim marked after it was sent, and that claim is about to be released, so any claim begun while
4913
+ * the renewal was out, even one already over, leaves it unproved.
4914
+ */
4915
+ private proveOwnLease;
4916
+ private unmarkClaim;
4903
4917
  /**
4904
4918
  * Hand due runs (timers, blocked runs due for a fleet re-check) to whichever instance takes them
4905
4919
  * off the run queue. Each is claimed first, under its lease, by pushing its `wakeAt` one lease
package/dist/index.js CHANGED
@@ -5199,6 +5199,12 @@ var WorkflowEngine = class {
5199
5199
  * releases when its budget runs out.
5200
5200
  */
5201
5201
  executionLeases = /* @__PURE__ */ new Map();
5202
+ /** Runs a caller just locked to execute now, so their turn has nothing to prove and no await to add. */
5203
+ lockedForTurn = /* @__PURE__ */ new Set();
5204
+ /** Runs this instance holds for a moment and will release, not to execute (see {@link handOffDue}). */
5205
+ claimsInFlight = /* @__PURE__ */ new Map();
5206
+ /** How many such claims each run has had, so a proof can tell one came and went (see {@link proveOwnLease}). */
5207
+ claimsMade = /* @__PURE__ */ new Map();
5202
5208
  /** Pager hooks for stranded runs (see {@link onStalled}). */
5203
5209
  stalledListeners = /* @__PURE__ */ new Set();
5204
5210
  /** Runs already paged about, by id → the `updatedAt` of the paged episode (see {@link sweepStalled}). */
@@ -5542,6 +5548,27 @@ var WorkflowEngine = class {
5542
5548
  console.error(`[nestjs-durable] could not run queued run ${runId}:`, error2);
5543
5549
  }
5544
5550
  }
5551
+ markClaim(runId) {
5552
+ this.claimsInFlight.set(runId, (this.claimsInFlight.get(runId) ?? 0) + 1);
5553
+ if (this.claimsMade.size >= 4096) this.claimsMade.clear();
5554
+ this.claimsMade.set(runId, (this.claimsMade.get(runId) ?? 0) + 1);
5555
+ }
5556
+ /**
5557
+ * Whether this instance holds the run's lease for a turn, from the store and by owner. A renewal can land
5558
+ * on a claim marked after it was sent, and that claim is about to be released, so any claim begun while
5559
+ * the renewal was out, even one already over, leaves it unproved.
5560
+ */
5561
+ async proveOwnLease(runId, nowMs) {
5562
+ if (this.claimsInFlight.has(runId)) return false;
5563
+ const before = this.claimsMade.get(runId);
5564
+ const renewed = await this.store.renewRunLock(runId, this.instanceId, nowMs + this.leaseMs);
5565
+ return renewed && !this.claimsInFlight.has(runId) && this.claimsMade.get(runId) === before;
5566
+ }
5567
+ unmarkClaim(runId) {
5568
+ const left = (this.claimsInFlight.get(runId) ?? 1) - 1;
5569
+ if (left > 0) this.claimsInFlight.set(runId, left);
5570
+ else this.claimsInFlight.delete(runId);
5571
+ }
5545
5572
  /**
5546
5573
  * Hand due runs (timers, blocked runs due for a fleet re-check) to whichever instance takes them
5547
5574
  * off the run queue. Each is claimed first, under its lease, by pushing its `wakeAt` one lease
@@ -5553,29 +5580,34 @@ var WorkflowEngine = class {
5553
5580
  const claimed = [];
5554
5581
  for (const run of runs) {
5555
5582
  if (this.draining) break;
5556
- if (!await this.store.tryLockRun(run.id, this.instanceId, nowMs + this.leaseMs, nowMs)) {
5557
- continue;
5558
- }
5559
- if (!this.isRegisteredHere(run) && !await this.canResolveWorkflow(run)) {
5560
- const result = await this.settleUnregistered(run, nowMs);
5561
- if (result) settled?.set(run.id, result);
5562
- else if (result === void 0) this.logUnregisteredSkip(run);
5563
- continue;
5564
- }
5583
+ this.markClaim(run.id);
5565
5584
  try {
5566
- const patch = {
5567
- wakeAt: nowMs + this.leaseMs
5568
- };
5569
- if (this.store.updateRunIf) {
5570
- if (await this.store.updateRunIf(run.id, [
5571
- run.status
5572
- ], patch)) claimed.push(run);
5573
- } else {
5574
- await this.store.updateRun(run.id, patch);
5575
- claimed.push(run);
5585
+ if (!await this.store.tryLockRun(run.id, this.instanceId, nowMs + this.leaseMs, nowMs)) {
5586
+ continue;
5587
+ }
5588
+ if (!this.isRegisteredHere(run) && !await this.canResolveWorkflow(run)) {
5589
+ const result = await this.settleUnregistered(run, nowMs);
5590
+ if (result) settled?.set(run.id, result);
5591
+ else if (result === void 0) this.logUnregisteredSkip(run);
5592
+ continue;
5593
+ }
5594
+ try {
5595
+ const patch = {
5596
+ wakeAt: nowMs + this.leaseMs
5597
+ };
5598
+ if (this.store.updateRunIf) {
5599
+ if (await this.store.updateRunIf(run.id, [
5600
+ run.status
5601
+ ], patch)) claimed.push(run);
5602
+ } else {
5603
+ await this.store.updateRun(run.id, patch);
5604
+ claimed.push(run);
5605
+ }
5606
+ } finally {
5607
+ await this.store.releaseRunLock(run.id, this.instanceId);
5576
5608
  }
5577
5609
  } finally {
5578
- await this.store.releaseRunLock(run.id);
5610
+ this.unmarkClaim(run.id);
5579
5611
  }
5580
5612
  }
5581
5613
  try {
@@ -7030,7 +7062,8 @@ var WorkflowEngine = class {
7030
7062
  const nowMs = this.clock();
7031
7063
  const acquired = await this.store.tryLockRun(runId, this.instanceId, nowMs + this.leaseMs, nowMs);
7032
7064
  if (!acquired) return null;
7033
- return this.resumeImplicit(runId).catch(async (err) => {
7065
+ this.lockedForTurn.add(runId);
7066
+ return this.resumeImplicit(runId).finally(() => this.lockedForTurn.delete(runId)).catch(async (err) => {
7034
7067
  if (err instanceof NamespaceMismatch) return null;
7035
7068
  if (err instanceof WorkflowNotRegistered) {
7036
7069
  const run = await this.store.getRun(runId);
@@ -7314,6 +7347,7 @@ var WorkflowEngine = class {
7314
7347
  }
7315
7348
  try {
7316
7349
  const settled = onLocked ? await onLocked(run) : void 0;
7350
+ if (!settled) this.lockedForTurn.add(run.id);
7317
7351
  results.push(settled ?? await this.resumeImplicit(run.id));
7318
7352
  } catch (error2) {
7319
7353
  if (error2 instanceof WorkflowNotRegistered) {
@@ -7322,6 +7356,8 @@ var WorkflowEngine = class {
7322
7356
  if (settled !== void 0) continue;
7323
7357
  }
7324
7358
  console.error(`[nestjs-durable] could not resume run ${run.id} (${run.workflow}@${run.workflowVersion}); leaving it for another instance:`, error2);
7359
+ } finally {
7360
+ this.lockedForTurn.delete(run.id);
7325
7361
  }
7326
7362
  }
7327
7363
  return results;
@@ -8363,7 +8399,9 @@ var WorkflowEngine = class {
8363
8399
  fans: /* @__PURE__ */ new Map()
8364
8400
  };
8365
8401
  this.wakeReasons.delete(run.id);
8366
- if (run.lockedBy !== this.instanceId || (run.lockedUntil ?? 0) <= lockNow) {
8402
+ const lockedForTurn = this.lockedForTurn.delete(run.id);
8403
+ const reentered = run.lockedBy === this.instanceId && (run.lockedUntil ?? 0) > lockNow && (lockedForTurn || await this.proveOwnLease(run.id, lockNow));
8404
+ if (!reentered) {
8367
8405
  if (!await this.store.tryLockRun(run.id, this.instanceId, lockNow + this.leaseMs, lockNow)) {
8368
8406
  if (reason.generic) {
8369
8407
  if (!this.genericWakeRequested.has(run.id)) {