@dudousxd/nestjs-durable-core 0.90.0 → 0.90.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 +77 -15
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +19 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.js +77 -15
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
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);
|
|
@@ -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 (
|
|
11865
|
-
|
|
11866
|
-
|
|
11867
|
-
|
|
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.
|
|
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
|
-
|
|
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);
|