@dudousxd/nestjs-durable-core 0.90.0 → 0.91.0

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
@@ -6695,6 +6695,7 @@ var PARENT_NOTIFY_BACKOFF_MS = 250;
6695
6695
  var LEASE_RECHECK_MAX_MS = 2e3;
6696
6696
  var LEASE_RECHECK_SLACK_MS = 100;
6697
6697
  var LEASE_RECHECK_ATTEMPTS = 30;
6698
+ var RETRY_LEASE_POLL_MS = 100;
6698
6699
  function wakeSeen(request, settledFans, served) {
6699
6700
  if (request.id !== void 0 && served.has(request.id)) return true;
6700
6701
  return isFanWakeRequest(request.payload) && request.payload.fans.every(([first, seq]) => (settledFans.get(first) ?? -1) >= seq);
@@ -11537,7 +11538,7 @@ var WorkflowEngine = class _WorkflowEngine {
11537
11538
  ];
11538
11539
  }
11539
11540
  /** The worker groups this engine dispatches to: every registered remote workflow's group, plus any
11540
- * `extra` the caller declares. Local-step groups (a group consumed by in-process `@DurableStep`
11541
+ * `extra` the caller declares. Local-step groups (a group consumed by in-process `@Step`
11541
11542
  * workers, e.g. `pipeline`) aren't derivable from registrations — pass them via `extra` so a group
11542
11543
  * with backlog and ZERO workers is still reported (the alert case has no heartbeat to discover). */
11543
11544
  knownGroups(extra = []) {
@@ -11861,26 +11862,87 @@ var WorkflowEngine = class _WorkflowEngine {
11861
11862
  const reentered = run.lockedBy === this.instanceId && (run.lockedUntil ?? 0) > lockNow && (lockedForTurn || await this.proveOwnLease(run.id, lockNow));
11862
11863
  if (!reentered) {
11863
11864
  if (!await this.store.tryLockRun(run.id, this.instanceId, lockNow + this.leaseMs, lockNow)) {
11864
- if (reason.generic) {
11865
- if (!this.genericWakeRequested.has(run.id)) {
11866
- this.genericWakeRequested.add(run.id);
11867
- this.inBackground(this.requestWakeAfterTurn(run.id, null));
11868
- }
11869
- } else {
11870
- const fans = {
11871
- fans: [
11872
- ...reason.fans
11873
- ]
11874
- };
11875
- this.inBackground(this.requestWakeAfterTurn(run.id, fans));
11865
+ if (run.status === "failed") {
11866
+ const taken = await this.takeLeaseForRetry(run.id);
11867
+ if ("result" in taken) return taken.result;
11868
+ return this.executeLeased(taken.run, fn, registered);
11876
11869
  }
11877
- this.recheckAfterLease(run.id, run.lockedUntil);
11870
+ return this.keepWakeForHeldLease(run, reason);
11871
+ }
11872
+ }
11873
+ return this.executeLeased(run, fn, registered);
11874
+ }
11875
+ /**
11876
+ * A wake that found `run`'s lease held by another execution: keep it for that execution to honor
11877
+ * once it releases the lease, and answer with the run's status as read.
11878
+ */
11879
+ keepWakeForHeldLease(run, reason) {
11880
+ if (reason.generic) {
11881
+ if (!this.genericWakeRequested.has(run.id)) {
11882
+ this.genericWakeRequested.add(run.id);
11883
+ this.inBackground(this.requestWakeAfterTurn(run.id, null));
11884
+ }
11885
+ } else {
11886
+ const fans = {
11887
+ fans: [
11888
+ ...reason.fans
11889
+ ]
11890
+ };
11891
+ this.inBackground(this.requestWakeAfterTurn(run.id, fans));
11892
+ }
11893
+ this.recheckAfterLease(run.id, run.lockedUntil);
11894
+ return {
11895
+ runId: run.id,
11896
+ status: run.status
11897
+ };
11898
+ }
11899
+ /**
11900
+ * Take `runId`'s lease for an explicit retry of a `failed` run that found it held: the turn that
11901
+ * failed the run has not released it yet, or another instance holds it for a moment. Polls the
11902
+ * lease with the usual fenced take ({@link StateStore.tryLockRun}, which also takes over a lease
11903
+ * that expired; the old holder's renewal then fences it) until it is free. Returns the run, read
11904
+ * under the lease, while it is still `failed`; or the run's state when something else moved it on
11905
+ * meanwhile (nothing to retry). Throws when the lease stays held by a live execution for longer
11906
+ * than one lease period, or the engine starts draining: the retry is refused, never dropped.
11907
+ */
11908
+ async takeLeaseForRetry(runId) {
11909
+ const giveUpAt = Date.now() + this.leaseMs + LEASE_RECHECK_MAX_MS;
11910
+ for (; ; ) {
11911
+ const current = await this.store.getRun(runId);
11912
+ if (!current) throw new Error(`run ${runId} not found`);
11913
+ if (current.status !== "failed") return {
11914
+ result: this.resultOf(current)
11915
+ };
11916
+ const nowMs = this.clock();
11917
+ if (await this.store.tryLockRun(runId, this.instanceId, nowMs + this.leaseMs, nowMs)) {
11918
+ const fresh = await this.store.getRun(runId);
11919
+ if (fresh?.status === "failed") return {
11920
+ run: fresh
11921
+ };
11922
+ await this.releaseLeaseBestEffort(runId);
11878
11923
  return {
11879
- runId: run.id,
11880
- status: run.status
11924
+ result: fresh ? this.resultOf(fresh) : await this.echoCurrentStatus(runId)
11881
11925
  };
11882
11926
  }
11927
+ if (this.draining || Date.now() >= giveUpAt) {
11928
+ throw new Error(`run ${runId} could not be retried: its lease is held by ${current.lockedBy ?? "another execution"}${this.draining ? " and this engine is draining" : ""}; resume it again once that execution settles`);
11929
+ }
11930
+ const leaseLeft = (current.lockedUntil ?? nowMs) - nowMs;
11931
+ const wait = Math.min(RETRY_LEASE_POLL_MS, Math.max(10, leaseLeft + LEASE_RECHECK_SLACK_MS));
11932
+ await new Promise((resolve) => setTimeout(resolve, wait));
11883
11933
  }
11934
+ }
11935
+ /** `run` as a {@link RunResult}. */
11936
+ resultOf(run) {
11937
+ return {
11938
+ runId: run.id,
11939
+ status: run.status,
11940
+ output: run.output,
11941
+ error: run.error
11942
+ };
11943
+ }
11944
+ /** The body of {@link execute} once this instance holds `run`'s lease. */
11945
+ async executeLeased(run, fn, registered) {
11884
11946
  const settledFans = /* @__PURE__ */ new Map();
11885
11947
  this.turnSettledFans.set(run.id, settledFans);
11886
11948
  const held = this.holdExecutionLease(run.id);
@@ -16246,12 +16308,12 @@ function readSearchAttributes(schema, run) {
16246
16308
  __name(readSearchAttributes, "readSearchAttributes");
16247
16309
 
16248
16310
  // src/tokens.ts
16249
- var STATE_STORE = Symbol.for("nestjs-durable:STATE_STORE");
16250
- var TRANSPORT = Symbol.for("nestjs-durable:TRANSPORT");
16251
- var DURABLE_OPTIONS = Symbol.for("nestjs-durable:DURABLE_OPTIONS");
16252
16311
  var STATE_STORE_CANONICAL = Symbol.for("@dudousxd/nestjs-durable:state-store");
16253
16312
  var TRANSPORT_CANONICAL = Symbol.for("@dudousxd/nestjs-durable:transport");
16254
16313
  var DURABLE_OPTIONS_CANONICAL = Symbol.for("@dudousxd/nestjs-durable:options");
16314
+ var STATE_STORE = Symbol.for("nestjs-durable:STATE_STORE");
16315
+ var TRANSPORT = Symbol.for("nestjs-durable:TRANSPORT");
16316
+ var DURABLE_OPTIONS = Symbol.for("nestjs-durable:DURABLE_OPTIONS");
16255
16317
 
16256
16318
  // src/testing/in-memory-state-store.ts
16257
16319
  var RETENTION_EXCLUDED = [