@dudousxd/nestjs-durable-core 0.103.0 → 0.104.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 +131 -25
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +33 -7
- package/dist/index.d.ts +33 -7
- package/dist/index.js +130 -25
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -274,6 +274,7 @@ __export(index_exports, {
|
|
|
274
274
|
singletonAdmissionDenialPatch: () => singletonAdmissionDenialPatch,
|
|
275
275
|
singletonAdmissionTags: () => singletonAdmissionTags,
|
|
276
276
|
singletonClaimantIds: () => singletonClaimantIds,
|
|
277
|
+
singletonKeyTag: () => singletonKeyTag,
|
|
277
278
|
sqlComparator: () => sqlComparator,
|
|
278
279
|
stepConfigOf: () => stepConfigOf,
|
|
279
280
|
stepId: () => stepId,
|
|
@@ -4909,6 +4910,14 @@ var LEGACY_SCAN_STATUSES = [
|
|
|
4909
4910
|
"cancelling"
|
|
4910
4911
|
];
|
|
4911
4912
|
var byAdmissionOrder = /* @__PURE__ */ __name((a, b) => a.createdAt.getTime() - b.createdAt.getTime() || a.id.localeCompare(b.id), "byAdmissionOrder");
|
|
4913
|
+
function singletonKeyTag(tags) {
|
|
4914
|
+
if (!tags) return void 0;
|
|
4915
|
+
for (let i = tags.length - 1; i >= 0; i -= 1) {
|
|
4916
|
+
if (tags[i].startsWith("singleton:")) return tags[i];
|
|
4917
|
+
}
|
|
4918
|
+
return void 0;
|
|
4919
|
+
}
|
|
4920
|
+
__name(singletonKeyTag, "singletonKeyTag");
|
|
4912
4921
|
function withoutSingletonMarkers(tags) {
|
|
4913
4922
|
return tags?.filter((tag) => !SINGLETON_RESERVED_TAGS.includes(tag));
|
|
4914
4923
|
}
|
|
@@ -4937,7 +4946,7 @@ function singletonClaimantIds(rows, tag) {
|
|
|
4937
4946
|
__name(singletonClaimantIds, "singletonClaimantIds");
|
|
4938
4947
|
function singletonAdmissionTags(runs, runId, tag, workflow, limit) {
|
|
4939
4948
|
if (!Number.isInteger(limit) || limit < 1) throw new Error("singleton limit must be a positive integer");
|
|
4940
|
-
const active = runs.filter((run) => run.workflow === workflow && SINGLETON_ACTIVE_STATUSES.includes(run.status) && run.tags
|
|
4949
|
+
const active = runs.filter((run) => run.workflow === workflow && SINGLETON_ACTIVE_STATUSES.includes(run.status) && singletonKeyTag(run.tags) === tag);
|
|
4941
4950
|
const candidate = active.find((run) => run.id === runId);
|
|
4942
4951
|
if (!candidate) return null;
|
|
4943
4952
|
if (candidate.tags?.includes(SINGLETON_ADMITTED_TAG)) return candidate.tags;
|
|
@@ -4965,7 +4974,7 @@ async function clearTerminalSingletonAdmission(runId, patch, getRun) {
|
|
|
4965
4974
|
__name(clearTerminalSingletonAdmission, "clearTerminalSingletonAdmission");
|
|
4966
4975
|
function singletonAdmissionDenialPatch(runs, runId, tag, workflow, retryWakeAt) {
|
|
4967
4976
|
if (retryWakeAt === void 0) return void 0;
|
|
4968
|
-
const candidate = runs.find((run) => run.id === runId && run.workflow === workflow && run.tags
|
|
4977
|
+
const candidate = runs.find((run) => run.id === runId && run.workflow === workflow && singletonKeyTag(run.tags) === tag && SINGLETON_ACTIVE_STATUSES.includes(run.status));
|
|
4969
4978
|
if (!candidate || candidate.status === "cancelling") return void 0;
|
|
4970
4979
|
return {
|
|
4971
4980
|
status: "suspended",
|
|
@@ -5014,6 +5023,28 @@ function isLockConflict(error2) {
|
|
|
5014
5023
|
return false;
|
|
5015
5024
|
}
|
|
5016
5025
|
__name(isLockConflict, "isLockConflict");
|
|
5026
|
+
var UnparseableStoredInput = class {
|
|
5027
|
+
static {
|
|
5028
|
+
__name(this, "UnparseableStoredInput");
|
|
5029
|
+
}
|
|
5030
|
+
error;
|
|
5031
|
+
constructor(error2) {
|
|
5032
|
+
this.error = error2;
|
|
5033
|
+
}
|
|
5034
|
+
};
|
|
5035
|
+
async function admissionInput(run, parseInput) {
|
|
5036
|
+
if (!parseInput) return run.input;
|
|
5037
|
+
try {
|
|
5038
|
+
return await parseInput(run.input);
|
|
5039
|
+
} catch (err) {
|
|
5040
|
+
return new UnparseableStoredInput(err instanceof SchemaValidationError && err.code === "invalid_input" ? err : new SchemaValidationError(`workflow "${run.workflow}" input`, [
|
|
5041
|
+
{
|
|
5042
|
+
message: err instanceof Error ? err.message : String(err)
|
|
5043
|
+
}
|
|
5044
|
+
], "invalid_input"));
|
|
5045
|
+
}
|
|
5046
|
+
}
|
|
5047
|
+
__name(admissionInput, "admissionInput");
|
|
5017
5048
|
var SingletonGate = class {
|
|
5018
5049
|
static {
|
|
5019
5050
|
__name(this, "SingletonGate");
|
|
@@ -5050,12 +5081,19 @@ var SingletonGate = class {
|
|
|
5050
5081
|
throw new SingletonQueueFullError(workflow, cfg.key(input), cfg.maxQueueDepth);
|
|
5051
5082
|
}
|
|
5052
5083
|
}
|
|
5053
|
-
/**
|
|
5054
|
-
|
|
5084
|
+
/**
|
|
5085
|
+
* Preserve durable holders and atomically claim one of the remaining slots. With `parseInput` (the
|
|
5086
|
+
* workflow's input schema), the key reads the parsed input, the value `run(ctx, input)` gets; a
|
|
5087
|
+
* stored input that no longer parses answers {@link UnparseableStoredInput}, which the caller
|
|
5088
|
+
* must fail the turn with (never run it).
|
|
5089
|
+
*/
|
|
5090
|
+
async admit(run, cfg, parseInput) {
|
|
5055
5091
|
if (!this.deps.store.tryAdmitSingleton) {
|
|
5056
5092
|
throw new Error("Singleton workflows require StateStore.tryAdmitSingleton for atomic durable admission");
|
|
5057
5093
|
}
|
|
5058
|
-
const
|
|
5094
|
+
const keyInput = await admissionInput(run, parseInput);
|
|
5095
|
+
if (keyInput instanceof UnparseableStoredInput) return keyInput;
|
|
5096
|
+
const admitted = await this.claim(run, cfg, this.admissionTag(run, cfg, keyInput, parseInput));
|
|
5059
5097
|
if (admitted && !run.tags?.includes(SINGLETON_ADMITTED_TAG)) {
|
|
5060
5098
|
run.tags = [
|
|
5061
5099
|
...(run.tags ?? []).filter((tag) => tag !== SINGLETON_TRACKED_TAG),
|
|
@@ -5065,17 +5103,43 @@ var SingletonGate = class {
|
|
|
5065
5103
|
return admitted;
|
|
5066
5104
|
}
|
|
5067
5105
|
/**
|
|
5106
|
+
* The exact tag a turn claims under: the key of the parsed input, the tag `start` stamps. A run
|
|
5107
|
+
* queued by an engine that predates `parseInput` was stamped with the key of its RAW input
|
|
5108
|
+
* instead; when the run carries that tag and not the parsed one, it claims under it, so an
|
|
5109
|
+
* upgrade does not strand it. Both are computed from this run's own input, never read off a tag.
|
|
5110
|
+
* Only that case calls the key with the raw input (what the older engine always did), and only
|
|
5111
|
+
* for a run that carries a `singleton:` tag at all; a key that throws on it gets the parsed tag.
|
|
5112
|
+
*/
|
|
5113
|
+
admissionTag(run, cfg, keyInput, parseInput) {
|
|
5114
|
+
const parsed = this.tag(cfg, keyInput);
|
|
5115
|
+
const stamped = singletonKeyTag(run.tags);
|
|
5116
|
+
if (!parseInput || stamped === void 0 || stamped === parsed) return parsed;
|
|
5117
|
+
let raw;
|
|
5118
|
+
try {
|
|
5119
|
+
raw = this.tag(cfg, run.input);
|
|
5120
|
+
} catch {
|
|
5121
|
+
return parsed;
|
|
5122
|
+
}
|
|
5123
|
+
return stamped === raw ? raw : parsed;
|
|
5124
|
+
}
|
|
5125
|
+
/**
|
|
5068
5126
|
* One claim, retried when the database aborts it as a lock conflict. A conflict that outlasts the
|
|
5069
5127
|
* retries answers "not admitted", so the engine releases this turn's lease, instead of stranding
|
|
5070
5128
|
* the run until the lease lapses. It writes nothing to the run: this turn's lease may have lapsed
|
|
5071
5129
|
* during the claim, and another instance may now hold the run, its slot and newer tags. A later
|
|
5072
5130
|
* sweep re-drives the run: recovery once it is unleased, or its timer if it is parked.
|
|
5131
|
+
*
|
|
5132
|
+
* The claim is under the tag the key computes NOW, the one `start` stamped from the same input
|
|
5133
|
+
* (the schema is deterministic). The store admits a run only when it carries that exact tag, so
|
|
5134
|
+
* a run whose stored tags lack it (a caller tag from before callers' `singleton:` tags were
|
|
5135
|
+
* dropped, a run started before the workflow became a singleton) is never admitted under some
|
|
5136
|
+
* other key; it waits instead.
|
|
5073
5137
|
*/
|
|
5074
|
-
async claim(run, cfg) {
|
|
5138
|
+
async claim(run, cfg, tag) {
|
|
5075
5139
|
const admit = this.deps.store.tryAdmitSingleton;
|
|
5076
5140
|
for (let attempt = 1; ; attempt += 1) {
|
|
5077
5141
|
try {
|
|
5078
|
-
return await admit.call(this.deps.store, run.id,
|
|
5142
|
+
return await admit.call(this.deps.store, run.id, tag, run.workflow, cfg.limit ?? 1, this.retryWakeAt());
|
|
5079
5143
|
} catch (error2) {
|
|
5080
5144
|
if (!isLockConflict(error2)) throw error2;
|
|
5081
5145
|
if (attempt >= ADMISSION_ATTEMPTS) return false;
|
|
@@ -5093,7 +5157,7 @@ var SingletonGate = class {
|
|
|
5093
5157
|
async wakeNext(settled) {
|
|
5094
5158
|
const cfg = this.deps.configFor(settled);
|
|
5095
5159
|
if (!cfg) return;
|
|
5096
|
-
const tag = settled.tags
|
|
5160
|
+
const tag = singletonKeyTag(settled.tags);
|
|
5097
5161
|
if (!tag) return;
|
|
5098
5162
|
const gated = (await this.deps.store.listRuns({
|
|
5099
5163
|
tag,
|
|
@@ -7242,9 +7306,30 @@ function tenantScopedKey(namespace, key) {
|
|
|
7242
7306
|
return namespace && namespace !== "default" ? `${namespace}/${key}` : key;
|
|
7243
7307
|
}
|
|
7244
7308
|
__name(tenantScopedKey, "tenantScopedKey");
|
|
7309
|
+
function failingBody(error2) {
|
|
7310
|
+
return async () => {
|
|
7311
|
+
throw error2;
|
|
7312
|
+
};
|
|
7313
|
+
}
|
|
7314
|
+
__name(failingBody, "failingBody");
|
|
7315
|
+
function storedInputError(invalid) {
|
|
7316
|
+
return {
|
|
7317
|
+
message: invalid.error.message,
|
|
7318
|
+
code: "invalid_input",
|
|
7319
|
+
retryable: false,
|
|
7320
|
+
details: {
|
|
7321
|
+
issues: invalid.error.issues
|
|
7322
|
+
}
|
|
7323
|
+
};
|
|
7324
|
+
}
|
|
7325
|
+
__name(storedInputError, "storedInputError");
|
|
7326
|
+
function callerTags(tags) {
|
|
7327
|
+
return tags?.filter((tag) => !tag.startsWith(SINGLETON_TAG_PREFIX));
|
|
7328
|
+
}
|
|
7329
|
+
__name(callerTags, "callerTags");
|
|
7330
|
+
var SINGLETON_TAG_PREFIX = "singleton:";
|
|
7245
7331
|
function singletonKeyOf(run) {
|
|
7246
|
-
|
|
7247
|
-
return tag?.slice("singleton:".length);
|
|
7332
|
+
return singletonKeyTag(run.tags)?.slice("singleton:".length);
|
|
7248
7333
|
}
|
|
7249
7334
|
__name(singletonKeyOf, "singletonKeyOf");
|
|
7250
7335
|
function conventionSingleton(run, announced, wire) {
|
|
@@ -8264,6 +8349,7 @@ var WorkflowEngine = class _WorkflowEngine {
|
|
|
8264
8349
|
concurrency: opts?.concurrency,
|
|
8265
8350
|
executionTimeoutMs: opts?.executionTimeout != null ? parseDuration(opts.executionTimeout) : void 0,
|
|
8266
8351
|
validateInput: opts?.validateInput,
|
|
8352
|
+
parseInput: opts?.parseInput,
|
|
8267
8353
|
searchAttributesSchema: opts?.searchAttributesSchema,
|
|
8268
8354
|
onEvent: opts?.onEvent,
|
|
8269
8355
|
eventBatch: opts?.eventBatch,
|
|
@@ -9965,24 +10051,31 @@ var WorkflowEngine = class _WorkflowEngine {
|
|
|
9965
10051
|
if (!registered) {
|
|
9966
10052
|
throw new Error(pinned === void 0 ? `workflow ${name} is not registered` : `workflow ${versionKey(name, pinned)} is not registered \u2014 a pinned start never falls back to the newest version (registered: ${this.registeredVersions(name)})`);
|
|
9967
10053
|
}
|
|
9968
|
-
|
|
10054
|
+
const check = registered;
|
|
10055
|
+
const parseAndValidate = /* @__PURE__ */ __name(async () => {
|
|
10056
|
+
const parsed = check.parseInput ? await check.parseInput(input) : input;
|
|
10057
|
+
await check.validateInput?.(parsed);
|
|
10058
|
+
return parsed;
|
|
10059
|
+
}, "parseAndValidate");
|
|
10060
|
+
let keyInput;
|
|
10061
|
+
if (parkUnroutable && (registered.validateInput || registered.parseInput)) {
|
|
9969
10062
|
try {
|
|
9970
|
-
await
|
|
10063
|
+
keyInput = await parseAndValidate();
|
|
9971
10064
|
} catch (err) {
|
|
9972
10065
|
throw new TenantStartInvalid(err);
|
|
9973
10066
|
}
|
|
9974
10067
|
} else {
|
|
9975
|
-
await
|
|
10068
|
+
keyInput = await parseAndValidate();
|
|
9976
10069
|
}
|
|
9977
10070
|
const prior = await this.store.getRun(runId);
|
|
9978
10071
|
if (prior) return this.existingStartResult(prior);
|
|
9979
|
-
const quota = await resolveConcurrency(opts?.concurrency, registered.concurrency,
|
|
10072
|
+
const quota = await resolveConcurrency(opts?.concurrency, registered.concurrency, keyInput);
|
|
9980
10073
|
if (quota) await assertConcurrency(this.store, name, quota);
|
|
9981
10074
|
const now = /* @__PURE__ */ new Date();
|
|
9982
10075
|
const stampedTags = [
|
|
9983
|
-
...opts?.tags ?? [],
|
|
10076
|
+
...callerTags(opts?.tags) ?? [],
|
|
9984
10077
|
...registered.singleton ? [
|
|
9985
|
-
this.singletons.tag(registered.singleton,
|
|
10078
|
+
this.singletons.tag(registered.singleton, keyInput)
|
|
9986
10079
|
] : [],
|
|
9987
10080
|
...quota ? [
|
|
9988
10081
|
concurrencyTag(quota.key)
|
|
@@ -9991,9 +10084,9 @@ var WorkflowEngine = class _WorkflowEngine {
|
|
|
9991
10084
|
VERSION_UNDECLARED_TAG
|
|
9992
10085
|
] : []
|
|
9993
10086
|
];
|
|
9994
|
-
const tags = mergeTags(registered.tags, stampedTags)?.filter((tag) => !SINGLETON_RESERVED_TAGS.includes(tag));
|
|
10087
|
+
const tags = mergeTags(callerTags(registered.tags), stampedTags)?.filter((tag) => !SINGLETON_RESERVED_TAGS.includes(tag));
|
|
9995
10088
|
if (registered.singleton) {
|
|
9996
|
-
await this.singletons.assertCapacity(name, registered.singleton,
|
|
10089
|
+
await this.singletons.assertCapacity(name, registered.singleton, keyInput);
|
|
9997
10090
|
}
|
|
9998
10091
|
const startAtMs = opts?.startAt instanceof Date ? opts.startAt.getTime() : opts?.startAt;
|
|
9999
10092
|
const delayed = startAtMs !== void 0 && Number.isFinite(startAtMs) && startAtMs > this.clock();
|
|
@@ -10082,7 +10175,7 @@ var WorkflowEngine = class _WorkflowEngine {
|
|
|
10082
10175
|
};
|
|
10083
10176
|
const now = /* @__PURE__ */ new Date();
|
|
10084
10177
|
const tags = mergeTags(void 0, [
|
|
10085
|
-
...opts?.tags ?? [],
|
|
10178
|
+
...callerTags(opts?.tags) ?? [],
|
|
10086
10179
|
...singleton ? [
|
|
10087
10180
|
this.singletons.tag(singleton, input)
|
|
10088
10181
|
] : [],
|
|
@@ -10262,7 +10355,7 @@ var WorkflowEngine = class _WorkflowEngine {
|
|
|
10262
10355
|
namespace: poolNamespace(opts.namespace) ?? this.namespace,
|
|
10263
10356
|
input,
|
|
10264
10357
|
// The same derived tags an admitted or parked start carries, so quota-key queries find it.
|
|
10265
|
-
tags: mergeTags(opts.tags, [
|
|
10358
|
+
tags: mergeTags(callerTags(opts.tags), [
|
|
10266
10359
|
...opts.concurrency ? [
|
|
10267
10360
|
concurrencyTag(opts.concurrency.key)
|
|
10268
10361
|
] : [],
|
|
@@ -13789,8 +13882,17 @@ ${run.workflowVersion}`;
|
|
|
13789
13882
|
...await this.sourceTraceOf(run)
|
|
13790
13883
|
});
|
|
13791
13884
|
}
|
|
13792
|
-
if (registered.singleton
|
|
13793
|
-
|
|
13885
|
+
if (registered.singleton) {
|
|
13886
|
+
const admission = await this.singletons.admit(run, registered.singleton, registered.parseInput);
|
|
13887
|
+
if (admission instanceof UnparseableStoredInput) {
|
|
13888
|
+
const result = await this.settleRun(run, {
|
|
13889
|
+
kind: "failed",
|
|
13890
|
+
error: storedInputError(admission)
|
|
13891
|
+
});
|
|
13892
|
+
await this.releaseLeaseBestEffort(run.id);
|
|
13893
|
+
return result;
|
|
13894
|
+
}
|
|
13895
|
+
if (!admission) return this.singletonWaitResult(run);
|
|
13794
13896
|
}
|
|
13795
13897
|
const wfToken = tenantGroup(sanitizeQueueToken(remote.group), void 0);
|
|
13796
13898
|
let wfPlan = await this.planRoute(wfToken, registered.requires);
|
|
@@ -14827,7 +14929,8 @@ ${run.workflowVersion}`;
|
|
|
14827
14929
|
attempt: void 0
|
|
14828
14930
|
}, () => this.runExecutionTurn(run, fn, registered));
|
|
14829
14931
|
}
|
|
14830
|
-
async runExecutionTurn(run,
|
|
14932
|
+
async runExecutionTurn(run, registeredFn, registered) {
|
|
14933
|
+
let fn = registeredFn;
|
|
14831
14934
|
if (run.status === "cancelling") this.cancelRequested.add(run.id);
|
|
14832
14935
|
if (run.status === "pending") {
|
|
14833
14936
|
if (!await this.markStarted(run, registered?.singleton !== void 0)) {
|
|
@@ -14843,8 +14946,10 @@ ${run.workflowVersion}`;
|
|
|
14843
14946
|
});
|
|
14844
14947
|
}
|
|
14845
14948
|
await this.noteServedWakes(run.id);
|
|
14846
|
-
if (run.status !== "cancelling" && registered?.singleton
|
|
14847
|
-
|
|
14949
|
+
if (run.status !== "cancelling" && registered?.singleton) {
|
|
14950
|
+
const admission = await this.singletons.admit(run, registered.singleton, registered.parseInput);
|
|
14951
|
+
if (admission instanceof UnparseableStoredInput) fn = failingBody(admission.error);
|
|
14952
|
+
else if (!admission) return this.singletonWaitResult(run);
|
|
14848
14953
|
}
|
|
14849
14954
|
const stickyOk = this.stickyEligible(run);
|
|
14850
14955
|
let cont = stickyOk ? this.takeContinuation(run, fn) : void 0;
|
|
@@ -19357,6 +19462,7 @@ __name(settleResultsWithPlainCalls, "settleResultsWithPlainCalls");
|
|
|
19357
19462
|
singletonAdmissionDenialPatch,
|
|
19358
19463
|
singletonAdmissionTags,
|
|
19359
19464
|
singletonClaimantIds,
|
|
19465
|
+
singletonKeyTag,
|
|
19360
19466
|
sqlComparator,
|
|
19361
19467
|
stepConfigOf,
|
|
19362
19468
|
stepId,
|