@dudousxd/nestjs-durable-core 0.89.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 +565 -106
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +285 -12
- package/dist/index.d.ts +285 -12
- package/dist/index.js +556 -106
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -120,6 +120,7 @@ __export(index_exports, {
|
|
|
120
120
|
axisIsRunColumn: () => axisIsRunColumn,
|
|
121
121
|
bindWorkflowClass: () => bindWorkflowClass,
|
|
122
122
|
breakpointToken: () => breakpointToken,
|
|
123
|
+
bufferedEventReaches: () => bufferedEventReaches,
|
|
123
124
|
buildWorkflowDirectory: () => buildWorkflowDirectory,
|
|
124
125
|
canRoute: () => canRoute,
|
|
125
126
|
capableWorkers: () => capableWorkers,
|
|
@@ -167,6 +168,7 @@ __export(index_exports, {
|
|
|
167
168
|
hmacTenantVerifier: () => hmacTenantVerifier,
|
|
168
169
|
indexWaitersByRun: () => indexWaitersByRun,
|
|
169
170
|
info: () => info,
|
|
171
|
+
inheritTenant: () => inheritTenant,
|
|
170
172
|
isAnyCheckpoint: () => isAnyCheckpoint,
|
|
171
173
|
isAnyMember: () => isAnyMember,
|
|
172
174
|
isEngineMintedTag: () => isEngineMintedTag,
|
|
@@ -193,6 +195,7 @@ __export(index_exports, {
|
|
|
193
195
|
log: () => log,
|
|
194
196
|
markRemoteEvent: () => markRemoteEvent,
|
|
195
197
|
matchesAttributes: () => matchesAttributes,
|
|
198
|
+
matchesTenantPredicates: () => matchesTenantPredicates,
|
|
196
199
|
mergeRunFacetRows: () => mergeRunFacetRows,
|
|
197
200
|
mergeRunValueFacetRows: () => mergeRunValueFacetRows,
|
|
198
201
|
missingCapabilities: () => missingCapabilities,
|
|
@@ -217,6 +220,7 @@ __export(index_exports, {
|
|
|
217
220
|
prevCronFireMs: () => prevCronFireMs,
|
|
218
221
|
prevRRuleFireMs: () => prevRRuleFireMs,
|
|
219
222
|
queryCheckpointsOf: () => queryCheckpointsOf,
|
|
223
|
+
readBufferedEvent: () => readBufferedEvent,
|
|
220
224
|
readRunsByIds: () => readRunsByIds,
|
|
221
225
|
readSearchAttributes: () => readSearchAttributes,
|
|
222
226
|
requiredCapabilities: () => requiredCapabilities,
|
|
@@ -228,6 +232,7 @@ __export(index_exports, {
|
|
|
228
232
|
retentionKeeps: () => retentionKeeps,
|
|
229
233
|
retiredHandoffName: () => retiredHandoffName,
|
|
230
234
|
routeFor: () => routeFor,
|
|
235
|
+
rowTenant: () => rowTenant,
|
|
231
236
|
runCompletedEvent: () => runCompletedEvent,
|
|
232
237
|
runDetailEtag: () => runDetailEtag,
|
|
233
238
|
runDurableExecution: () => runDurableExecution,
|
|
@@ -245,6 +250,8 @@ __export(index_exports, {
|
|
|
245
250
|
sanitizeQueueToken: () => sanitizeQueueToken,
|
|
246
251
|
scheduleRunIdAt: () => scheduleRunIdAt,
|
|
247
252
|
scheduledRunId: () => scheduledRunId,
|
|
253
|
+
scopeBufferedEvent: () => scopeBufferedEvent,
|
|
254
|
+
scopeTenantQuery: () => scopeTenantQuery,
|
|
248
255
|
settleResultsWithPlainCalls: () => settleResultsWithPlainCalls,
|
|
249
256
|
signTenantToken: () => signTenantToken,
|
|
250
257
|
singletonAdmissionDenialPatch: () => singletonAdmissionDenialPatch,
|
|
@@ -261,6 +268,8 @@ __export(index_exports, {
|
|
|
261
268
|
summarizeRunFromStore: () => summarizeRunFromStore,
|
|
262
269
|
summarizeTimeline: () => summarizeTimeline,
|
|
263
270
|
tenantGroup: () => tenantGroup,
|
|
271
|
+
tenantIdsFor: () => tenantIdsFor,
|
|
272
|
+
tenantVisible: () => tenantVisible,
|
|
264
273
|
toEventSubscription: () => toEventSubscription,
|
|
265
274
|
traceIdOfTraceparent: () => traceIdOfTraceparent,
|
|
266
275
|
triggerFailedSubscribersOf: () => triggerFailedSubscribersOf,
|
|
@@ -1044,7 +1053,7 @@ var TERMINAL_RUN_STATUSES = [
|
|
|
1044
1053
|
];
|
|
1045
1054
|
function isNonEmptyRunScope(scope) {
|
|
1046
1055
|
if (!scope) return false;
|
|
1047
|
-
return scope.workflow !== void 0 || scope.workflows !== void 0 || scope.tag !== void 0 || scope.tags !== void 0 || scope.namespace !== void 0 || scope.namespaces !== void 0 || scope.attributes !== void 0 && scope.attributes.length > 0;
|
|
1056
|
+
return scope.workflow !== void 0 || scope.workflows !== void 0 || scope.tag !== void 0 || scope.tags !== void 0 || scope.namespace !== void 0 || scope.namespaces !== void 0 || scope.tenantId !== void 0 || scope.tenantIds !== void 0 || scope.attributes !== void 0 && scope.attributes.length > 0;
|
|
1048
1057
|
}
|
|
1049
1058
|
__name(isNonEmptyRunScope, "isNonEmptyRunScope");
|
|
1050
1059
|
var PARENT_CLOSE_TAG_PREFIX = "parent-close:";
|
|
@@ -1117,6 +1126,31 @@ function acceptingSubscription(subscriptions, payload) {
|
|
|
1117
1126
|
};
|
|
1118
1127
|
}
|
|
1119
1128
|
__name(acceptingSubscription, "acceptingSubscription");
|
|
1129
|
+
var BUFFERED_EVENT_TENANTS = "__durableEventTenants";
|
|
1130
|
+
function scopeBufferedEvent(payload, tenantIds) {
|
|
1131
|
+
return tenantIds === void 0 ? payload : {
|
|
1132
|
+
[BUFFERED_EVENT_TENANTS]: tenantIds,
|
|
1133
|
+
payload
|
|
1134
|
+
};
|
|
1135
|
+
}
|
|
1136
|
+
__name(scopeBufferedEvent, "scopeBufferedEvent");
|
|
1137
|
+
function readBufferedEvent(stored) {
|
|
1138
|
+
if (stored !== null && typeof stored === "object" && Array.isArray(stored[BUFFERED_EVENT_TENANTS]) && Object.keys(stored).length === 2 && "payload" in stored) {
|
|
1139
|
+
const scoped = stored;
|
|
1140
|
+
return {
|
|
1141
|
+
payload: scoped.payload,
|
|
1142
|
+
tenantIds: scoped[BUFFERED_EVENT_TENANTS]
|
|
1143
|
+
};
|
|
1144
|
+
}
|
|
1145
|
+
return {
|
|
1146
|
+
payload: stored
|
|
1147
|
+
};
|
|
1148
|
+
}
|
|
1149
|
+
__name(readBufferedEvent, "readBufferedEvent");
|
|
1150
|
+
function bufferedEventReaches(tenantIds, tenantId) {
|
|
1151
|
+
return tenantIds === void 0 || tenantIds.includes(tenantId ?? null);
|
|
1152
|
+
}
|
|
1153
|
+
__name(bufferedEventReaches, "bufferedEventReaches");
|
|
1120
1154
|
|
|
1121
1155
|
// src/lifecycle-events.ts
|
|
1122
1156
|
function runCompletedEvent(workflow) {
|
|
@@ -1906,7 +1940,8 @@ async function runStepHandler(task, handler) {
|
|
|
1906
1940
|
workflow: void 0,
|
|
1907
1941
|
name: task.name,
|
|
1908
1942
|
seq: task.seq,
|
|
1909
|
-
attempt: task.attempt
|
|
1943
|
+
attempt: task.attempt,
|
|
1944
|
+
context: task.context
|
|
1910
1945
|
},
|
|
1911
1946
|
// Outside any ambient workflow ctx: an in-process transport runs the handler on the parent
|
|
1912
1947
|
// body's async path, but a handler is not the body (see `runOutsideWorkflowCtx`).
|
|
@@ -2247,10 +2282,14 @@ var Entities = class {
|
|
|
2247
2282
|
};
|
|
2248
2283
|
|
|
2249
2284
|
// src/event-accumulators.ts
|
|
2250
|
-
function
|
|
2251
|
-
|
|
2252
|
-
|
|
2253
|
-
|
|
2285
|
+
function escapeIdPart(value) {
|
|
2286
|
+
return encodeURIComponent(value).replace(/[~!'()*.]/g, (c) => `%${c.charCodeAt(0).toString(16).toUpperCase()}`);
|
|
2287
|
+
}
|
|
2288
|
+
__name(escapeIdPart, "escapeIdPart");
|
|
2289
|
+
function accumulatorRunId(target, key, tenant) {
|
|
2290
|
+
const base = tenant === void 0 ? "__evtacc__" : `__evtacc__@${escapeIdPart(tenant)}`;
|
|
2291
|
+
if (key === void 0) return `${base}:${target}`;
|
|
2292
|
+
return `${base}:${target}:${escapeIdPart(key)}`;
|
|
2254
2293
|
}
|
|
2255
2294
|
__name(accumulatorRunId, "accumulatorRunId");
|
|
2256
2295
|
var ACCUMULATOR_SEEN_MAX = 1e3;
|
|
@@ -2295,7 +2334,7 @@ var SeenEvents = class SeenEvents2 {
|
|
|
2295
2334
|
} : rest;
|
|
2296
2335
|
}
|
|
2297
2336
|
};
|
|
2298
|
-
var accToken = /* @__PURE__ */ __name((input) => accumulatorRunId(input.target, input.key), "accToken");
|
|
2337
|
+
var accToken = /* @__PURE__ */ __name((input) => accumulatorRunId(input.target, input.key, input.tenant), "accToken");
|
|
2299
2338
|
var EventAccumulators = class {
|
|
2300
2339
|
static {
|
|
2301
2340
|
__name(this, "EventAccumulators");
|
|
@@ -2314,8 +2353,12 @@ var EventAccumulators = class {
|
|
|
2314
2353
|
* Route an `onEvent` payload into its target's debounce/batch accumulator: the target's one, or
|
|
2315
2354
|
* with a `key`, the one for `String(config.key(payload))`. A `key` that throws rejects the route.
|
|
2316
2355
|
* With `id`, the accumulator counts the event once however many times it is routed.
|
|
2356
|
+
*
|
|
2357
|
+
* `tenant` is the tenant the event belongs to when tenancy is on: a tenant gets its own
|
|
2358
|
+
* accumulator, stamped with it (so its target runs are too); `null` routes into the shared one,
|
|
2359
|
+
* stamped with no tenant. Undefined (tenancy off) starts the accumulator as before.
|
|
2317
2360
|
*/
|
|
2318
|
-
route(target, config, payload, id) {
|
|
2361
|
+
route(target, config, payload, id, tenant) {
|
|
2319
2362
|
const input = {
|
|
2320
2363
|
target,
|
|
2321
2364
|
windowMs: config.windowMs,
|
|
@@ -2324,6 +2367,9 @@ var EventAccumulators = class {
|
|
|
2324
2367
|
} : {},
|
|
2325
2368
|
...config.key ? {
|
|
2326
2369
|
key: String(config.key(payload))
|
|
2370
|
+
} : {},
|
|
2371
|
+
...tenant ? {
|
|
2372
|
+
tenant: tenant.id
|
|
2327
2373
|
} : {}
|
|
2328
2374
|
};
|
|
2329
2375
|
const workflow = config.mode === "debounce" ? "__evt_debounce" : "__evt_batch";
|
|
@@ -2333,10 +2379,18 @@ var EventAccumulators = class {
|
|
|
2333
2379
|
id,
|
|
2334
2380
|
payload
|
|
2335
2381
|
};
|
|
2382
|
+
const opts = tenant === void 0 ? void 0 : tenant === null ? {
|
|
2383
|
+
tenantId: null
|
|
2384
|
+
} : {
|
|
2385
|
+
tenantId: tenant.id,
|
|
2386
|
+
...tenant.kind !== void 0 ? {
|
|
2387
|
+
tenantKind: tenant.kind
|
|
2388
|
+
} : {}
|
|
2389
|
+
};
|
|
2336
2390
|
return this.host.signalWithStart(workflow, input, token, {
|
|
2337
2391
|
token,
|
|
2338
2392
|
payload: signal
|
|
2339
|
-
});
|
|
2393
|
+
}, opts);
|
|
2340
2394
|
}
|
|
2341
2395
|
/** Fire the target with the LAST payload once events have been quiet for `windowMs`. */
|
|
2342
2396
|
async debounce(ctx, input) {
|
|
@@ -3824,6 +3878,78 @@ function remember(memo, key, runId) {
|
|
|
3824
3878
|
}
|
|
3825
3879
|
__name(remember, "remember");
|
|
3826
3880
|
|
|
3881
|
+
// src/tenancy.ts
|
|
3882
|
+
function tenantVisible(row, filter) {
|
|
3883
|
+
const id = row.tenantId ?? null;
|
|
3884
|
+
switch (filter.mode) {
|
|
3885
|
+
case "system":
|
|
3886
|
+
case "all":
|
|
3887
|
+
return true;
|
|
3888
|
+
case "tenantless":
|
|
3889
|
+
return id === null;
|
|
3890
|
+
case "tenant":
|
|
3891
|
+
return id === filter.tenantId || id === null && filter.nullRows === "shared";
|
|
3892
|
+
}
|
|
3893
|
+
}
|
|
3894
|
+
__name(tenantVisible, "tenantVisible");
|
|
3895
|
+
function tenantIdsFor(filter) {
|
|
3896
|
+
switch (filter.mode) {
|
|
3897
|
+
case "system":
|
|
3898
|
+
case "all":
|
|
3899
|
+
return void 0;
|
|
3900
|
+
case "tenantless":
|
|
3901
|
+
return [
|
|
3902
|
+
null
|
|
3903
|
+
];
|
|
3904
|
+
case "tenant":
|
|
3905
|
+
return filter.nullRows === "shared" ? [
|
|
3906
|
+
filter.tenantId,
|
|
3907
|
+
null
|
|
3908
|
+
] : [
|
|
3909
|
+
filter.tenantId
|
|
3910
|
+
];
|
|
3911
|
+
}
|
|
3912
|
+
}
|
|
3913
|
+
__name(tenantIdsFor, "tenantIdsFor");
|
|
3914
|
+
function scopeTenantQuery(query, filter) {
|
|
3915
|
+
const allowed = tenantIdsFor(filter);
|
|
3916
|
+
if (allowed === void 0) return query;
|
|
3917
|
+
const tenantIds = query.tenantIds ? query.tenantIds.filter((id) => allowed.includes(id)) : allowed;
|
|
3918
|
+
return {
|
|
3919
|
+
...query,
|
|
3920
|
+
tenantIds
|
|
3921
|
+
};
|
|
3922
|
+
}
|
|
3923
|
+
__name(scopeTenantQuery, "scopeTenantQuery");
|
|
3924
|
+
function rowTenant(row) {
|
|
3925
|
+
if (row.tenantId == null) return void 0;
|
|
3926
|
+
return row.tenantKind != null ? {
|
|
3927
|
+
id: row.tenantId,
|
|
3928
|
+
kind: row.tenantKind
|
|
3929
|
+
} : {
|
|
3930
|
+
id: row.tenantId
|
|
3931
|
+
};
|
|
3932
|
+
}
|
|
3933
|
+
__name(rowTenant, "rowTenant");
|
|
3934
|
+
function inheritTenant(row) {
|
|
3935
|
+
return row.tenantId == null ? {
|
|
3936
|
+
tenantId: null
|
|
3937
|
+
} : {
|
|
3938
|
+
tenantId: row.tenantId,
|
|
3939
|
+
...row.tenantKind != null ? {
|
|
3940
|
+
tenantKind: row.tenantKind
|
|
3941
|
+
} : {}
|
|
3942
|
+
};
|
|
3943
|
+
}
|
|
3944
|
+
__name(inheritTenant, "inheritTenant");
|
|
3945
|
+
function matchesTenantPredicates(row, query) {
|
|
3946
|
+
const id = row.tenantId ?? null;
|
|
3947
|
+
if (query.tenantId !== void 0 && id !== query.tenantId) return false;
|
|
3948
|
+
if (query.tenantIds !== void 0 && !query.tenantIds.includes(id)) return false;
|
|
3949
|
+
return true;
|
|
3950
|
+
}
|
|
3951
|
+
__name(matchesTenantPredicates, "matchesTenantPredicates");
|
|
3952
|
+
|
|
3827
3953
|
// src/schedules.ts
|
|
3828
3954
|
var ScheduleAlreadyExistsError = class extends Error {
|
|
3829
3955
|
static {
|
|
@@ -3881,6 +4007,16 @@ var ScheduleClient = class {
|
|
|
3881
4007
|
listSchedules: /* @__PURE__ */ __name((q) => s.listSchedules.call(s, q), "listSchedules")
|
|
3882
4008
|
};
|
|
3883
4009
|
}
|
|
4010
|
+
filter() {
|
|
4011
|
+
return this.host.tenantFilter?.() ?? {
|
|
4012
|
+
mode: "system"
|
|
4013
|
+
};
|
|
4014
|
+
}
|
|
4015
|
+
/** The schedule `id` if it exists and the caller may see it (tenancy), else null. */
|
|
4016
|
+
async visible(id) {
|
|
4017
|
+
const record = await this.requireStore().getSchedule(id);
|
|
4018
|
+
return record && tenantVisible(record, this.filter()) ? record : null;
|
|
4019
|
+
}
|
|
3884
4020
|
/** Create a schedule. Throws {@link ScheduleAlreadyExistsError} if the id is taken. */
|
|
3885
4021
|
async create(opts) {
|
|
3886
4022
|
const store = this.requireStore();
|
|
@@ -3894,19 +4030,24 @@ var ScheduleClient = class {
|
|
|
3894
4030
|
*/
|
|
3895
4031
|
async upsert(opts) {
|
|
3896
4032
|
const store = this.requireStore();
|
|
3897
|
-
|
|
4033
|
+
const existing = await store.getSchedule(opts.id);
|
|
4034
|
+
if (existing && !tenantVisible(existing, this.filter())) {
|
|
4035
|
+
throw new ScheduleAlreadyExistsError(opts.id);
|
|
4036
|
+
}
|
|
4037
|
+
return this.write(opts, existing);
|
|
3898
4038
|
}
|
|
3899
|
-
/** The schedule `id`, or null. */
|
|
4039
|
+
/** The schedule `id`, or null (also null for another tenant's schedule). */
|
|
3900
4040
|
async get(id) {
|
|
3901
|
-
const record = await this.
|
|
4041
|
+
const record = await this.visible(id);
|
|
3902
4042
|
return record ? describe(record) : null;
|
|
3903
4043
|
}
|
|
3904
|
-
/** Schedules matching `query
|
|
4044
|
+
/** Schedules matching `query` (and visible to the caller's tenant), soonest-due first. */
|
|
3905
4045
|
async list(query = {}) {
|
|
3906
|
-
return (await this.requireStore().listSchedules(query)).map(describe);
|
|
4046
|
+
return (await this.requireStore().listSchedules(scopeTenantQuery(query, this.filter()))).map(describe);
|
|
3907
4047
|
}
|
|
3908
4048
|
/** Delete the schedule. Runs it already started are left alone. Returns whether it existed. */
|
|
3909
|
-
delete(id) {
|
|
4049
|
+
async delete(id) {
|
|
4050
|
+
if (!await this.visible(id)) return false;
|
|
3910
4051
|
return this.requireStore().deleteSchedule(id);
|
|
3911
4052
|
}
|
|
3912
4053
|
/** Stop firing until {@link resume}. `note` records why (shown by `get`/`list`). */
|
|
@@ -4041,10 +4182,34 @@ var ScheduleClient = class {
|
|
|
4041
4182
|
return !!prev && !TERMINAL_RUN_STATUSES.includes(prev.status);
|
|
4042
4183
|
}
|
|
4043
4184
|
async mustGet(id) {
|
|
4044
|
-
const record = await this.
|
|
4185
|
+
const record = await this.visible(id);
|
|
4045
4186
|
if (!record) throw new ScheduleNotFoundError(id);
|
|
4046
4187
|
return record;
|
|
4047
4188
|
}
|
|
4189
|
+
/** Explicit `opts.tenantId` → the existing schedule's tenant (upsert) → the context tenant → none. */
|
|
4190
|
+
scheduleTenant(opts, existing) {
|
|
4191
|
+
let tenant;
|
|
4192
|
+
if (opts.tenantId !== void 0) {
|
|
4193
|
+
tenant = opts.tenantId === null ? void 0 : {
|
|
4194
|
+
id: opts.tenantId,
|
|
4195
|
+
kind: opts.tenantKind
|
|
4196
|
+
};
|
|
4197
|
+
} else if (existing) {
|
|
4198
|
+
tenant = existing.tenantId == null ? void 0 : {
|
|
4199
|
+
id: existing.tenantId,
|
|
4200
|
+
kind: existing.tenantKind ?? void 0
|
|
4201
|
+
};
|
|
4202
|
+
} else {
|
|
4203
|
+
tenant = this.host.tenant?.();
|
|
4204
|
+
}
|
|
4205
|
+
if (!tenant) return {};
|
|
4206
|
+
return tenant.kind !== void 0 ? {
|
|
4207
|
+
tenantId: tenant.id,
|
|
4208
|
+
tenantKind: tenant.kind
|
|
4209
|
+
} : {
|
|
4210
|
+
tenantId: tenant.id
|
|
4211
|
+
};
|
|
4212
|
+
}
|
|
4048
4213
|
async write(opts, existing) {
|
|
4049
4214
|
const store = this.requireStore();
|
|
4050
4215
|
const spec = toSpec(opts);
|
|
@@ -4063,6 +4228,7 @@ var ScheduleClient = class {
|
|
|
4063
4228
|
...existing?.state ?? {},
|
|
4064
4229
|
window
|
|
4065
4230
|
}),
|
|
4231
|
+
...this.scheduleTenant(opts, existing),
|
|
4066
4232
|
createdAt: existing?.createdAt ?? new Date(now),
|
|
4067
4233
|
updatedAt: new Date(now)
|
|
4068
4234
|
};
|
|
@@ -4145,7 +4311,10 @@ function startOptions(record) {
|
|
|
4145
4311
|
namespace: record.namespace,
|
|
4146
4312
|
searchAttributes: spec.searchAttributes,
|
|
4147
4313
|
priority: spec.priority,
|
|
4148
|
-
concurrency: spec.concurrency
|
|
4314
|
+
concurrency: spec.concurrency,
|
|
4315
|
+
// Every fire belongs to the schedule's customer (or explicitly to none), never to whichever
|
|
4316
|
+
// poller or request happened to fire it.
|
|
4317
|
+
...inheritTenant(record)
|
|
4149
4318
|
});
|
|
4150
4319
|
}
|
|
4151
4320
|
__name(startOptions, "startOptions");
|
|
@@ -4175,6 +4344,8 @@ function describe(record) {
|
|
|
4175
4344
|
lastRunId: state.lastRunId,
|
|
4176
4345
|
fires: state.fires ?? 0,
|
|
4177
4346
|
lastError: state.lastError,
|
|
4347
|
+
tenantId: record.tenantId ?? void 0,
|
|
4348
|
+
tenantKind: record.tenantKind ?? void 0,
|
|
4178
4349
|
createdAt: record.createdAt,
|
|
4179
4350
|
updatedAt: record.updatedAt
|
|
4180
4351
|
});
|
|
@@ -5528,8 +5699,14 @@ function createWorkflowCtx(host, runId, compensations, workflow = "", searchAttr
|
|
|
5528
5699
|
}, "consumeBuffered");
|
|
5529
5700
|
const consumeBufferedEvent = /* @__PURE__ */ __name(async (name, match, token, seq) => {
|
|
5530
5701
|
const candidates = await store.listBufferedEvents(name, EVENT_BUFFER_SCAN_LIMIT);
|
|
5702
|
+
let runTenantId;
|
|
5531
5703
|
for (const candidate of candidates) {
|
|
5532
|
-
|
|
5704
|
+
const { payload, tenantIds } = readBufferedEvent(candidate.payload);
|
|
5705
|
+
if (!eventMatches(payload, match ?? {})) continue;
|
|
5706
|
+
if (tenantIds !== void 0) {
|
|
5707
|
+
runTenantId ??= store.getRun(runId).then((run) => run?.tenantId ?? null);
|
|
5708
|
+
if (!bufferedEventReaches(tenantIds, await runTenantId)) continue;
|
|
5709
|
+
}
|
|
5533
5710
|
if (!await store.removeBufferedEvent(candidate.id)) continue;
|
|
5534
5711
|
await store.removeSignalWaiter({
|
|
5535
5712
|
token,
|
|
@@ -5541,10 +5718,10 @@ function createWorkflowCtx(host, runId, compensations, workflow = "", searchAttr
|
|
|
5541
5718
|
seq,
|
|
5542
5719
|
name: `signal:${token}`,
|
|
5543
5720
|
kind: "signal",
|
|
5544
|
-
output:
|
|
5721
|
+
output: payload
|
|
5545
5722
|
}));
|
|
5546
5723
|
return {
|
|
5547
|
-
value:
|
|
5724
|
+
value: payload
|
|
5548
5725
|
};
|
|
5549
5726
|
}
|
|
5550
5727
|
return null;
|
|
@@ -6518,6 +6695,7 @@ var PARENT_NOTIFY_BACKOFF_MS = 250;
|
|
|
6518
6695
|
var LEASE_RECHECK_MAX_MS = 2e3;
|
|
6519
6696
|
var LEASE_RECHECK_SLACK_MS = 100;
|
|
6520
6697
|
var LEASE_RECHECK_ATTEMPTS = 30;
|
|
6698
|
+
var RETRY_LEASE_POLL_MS = 100;
|
|
6521
6699
|
function wakeSeen(request, settledFans, served) {
|
|
6522
6700
|
if (request.id !== void 0 && served.has(request.id)) return true;
|
|
6523
6701
|
return isFanWakeRequest(request.payload) && request.payload.fans.every(([first, seq]) => (settledFans.get(first) ?? -1) >= seq);
|
|
@@ -6709,6 +6887,8 @@ var WorkflowEngine = class _WorkflowEngine {
|
|
|
6709
6887
|
context;
|
|
6710
6888
|
/** Establish the originating context ambiently around a local step body (see {@link WorkflowEngineDeps.rehydrate}). Default passthrough. */
|
|
6711
6889
|
rehydrate;
|
|
6890
|
+
/** Tenancy seam (see {@link WorkflowEngineDeps.tenancy}); undefined = off. */
|
|
6891
|
+
tenancy;
|
|
6712
6892
|
compensationRetries;
|
|
6713
6893
|
/** Persist a `running` checkpoint at the start of a local step body (see {@link WorkflowEngineDeps.trackStepStart}). */
|
|
6714
6894
|
trackStepStart;
|
|
@@ -6918,6 +7098,7 @@ var WorkflowEngine = class _WorkflowEngine {
|
|
|
6918
7098
|
this.traceparent = deps.traceparent;
|
|
6919
7099
|
this.context = deps.context;
|
|
6920
7100
|
this.rehydrate = deps.rehydrate ?? ((_carrier, fn) => fn());
|
|
7101
|
+
this.tenancy = deps.tenancy;
|
|
6921
7102
|
this.compensationRetries = Math.max(1, deps.compensationRetries ?? 1);
|
|
6922
7103
|
this.trackStepStart = deps.trackStepStart ?? true;
|
|
6923
7104
|
this.localWork = new BoundedRunner(LOCAL_RUN_CONCURRENCY);
|
|
@@ -6947,7 +7128,9 @@ var WorkflowEngine = class _WorkflowEngine {
|
|
|
6947
7128
|
this.schedules = new ScheduleClient(this.store, {
|
|
6948
7129
|
start: /* @__PURE__ */ __name((workflow, input, runId, opts) => this.start(workflow, input, runId, opts), "start"),
|
|
6949
7130
|
getRun: /* @__PURE__ */ __name((runId) => this.store.getRun(runId), "getRun"),
|
|
6950
|
-
isDraining: /* @__PURE__ */ __name(() => this.draining, "isDraining")
|
|
7131
|
+
isDraining: /* @__PURE__ */ __name(() => this.draining, "isDraining"),
|
|
7132
|
+
tenant: /* @__PURE__ */ __name(() => this.tenancy?.current(), "tenant"),
|
|
7133
|
+
tenantFilter: /* @__PURE__ */ __name(() => this.tenantFilter(), "tenantFilter")
|
|
6951
7134
|
}, {
|
|
6952
7135
|
namespace: this.namespace,
|
|
6953
7136
|
clock: this.clock
|
|
@@ -7051,7 +7234,18 @@ var WorkflowEngine = class _WorkflowEngine {
|
|
|
7051
7234
|
}
|
|
7052
7235
|
}
|
|
7053
7236
|
});
|
|
7054
|
-
this.accumulators = new EventAccumulators(
|
|
7237
|
+
this.accumulators = new EventAccumulators({
|
|
7238
|
+
register: /* @__PURE__ */ __name((name, version, fn, opts) => this.register(name, version, fn, opts), "register"),
|
|
7239
|
+
// The engine picks the accumulator's tenant itself, so its signal is the engine's own
|
|
7240
|
+
// delivery: not subject to the publisher's tenancy filter.
|
|
7241
|
+
signalWithStart: /* @__PURE__ */ __name(async (workflow, input, runId, signal, opts) => {
|
|
7242
|
+
await this.start(workflow, input, runId, opts);
|
|
7243
|
+
await this.signalUnchecked(signal.token, signal.payload);
|
|
7244
|
+
return {
|
|
7245
|
+
runId
|
|
7246
|
+
};
|
|
7247
|
+
}, "signalWithStart")
|
|
7248
|
+
});
|
|
7055
7249
|
this.entities = new Entities(this, {
|
|
7056
7250
|
bufferSignal: /* @__PURE__ */ __name((token, payload) => this.store.bufferSignal(token, payload), "bufferSignal"),
|
|
7057
7251
|
wakeWaiter: /* @__PURE__ */ __name((token, payload) => this.wakeSignalWaiter(token, payload), "wakeWaiter"),
|
|
@@ -7527,15 +7721,19 @@ var WorkflowEngine = class _WorkflowEngine {
|
|
|
7527
7721
|
* time from {@link context} — the SAME reader stamped on dispatched remote tasks — so a local step
|
|
7528
7722
|
* sees the live tenant / user / trace ids ambiently (via the {@link rehydrate} bridge). Default
|
|
7529
7723
|
* `rehydrate` is a passthrough, so this is byte-identical to a bare body call when unwired.
|
|
7724
|
+
*
|
|
7725
|
+
* The run row's tenant is re-applied INSIDE the rehydrated context: an app carrier (a custom
|
|
7726
|
+
* `serialize`/`deserialize`) may not carry the tenant, and its deserializer would otherwise replace
|
|
7727
|
+
* the tenant the turn was started with. Local steps always see the row's tenant.
|
|
7530
7728
|
*/
|
|
7531
|
-
interceptStep(invocation, body) {
|
|
7729
|
+
interceptStep(invocation, body, tenant) {
|
|
7532
7730
|
const carrier = this.context?.();
|
|
7533
7731
|
const run = /* @__PURE__ */ __name(() => {
|
|
7534
7732
|
if (this.interceptors.length === 0) return body();
|
|
7535
7733
|
const chain = this.interceptors.reduceRight((next, interceptor) => () => interceptor(invocation, next), body);
|
|
7536
7734
|
return chain();
|
|
7537
7735
|
}, "run");
|
|
7538
|
-
return this.rehydrate(carrier, run);
|
|
7736
|
+
return this.rehydrate(carrier, () => tenant ? this.inRunTenant(tenant, run) : run());
|
|
7539
7737
|
}
|
|
7540
7738
|
/**
|
|
7541
7739
|
* Be notified when a run is cancelled — on ANY instance, via the transport control plane. A
|
|
@@ -7726,9 +7924,12 @@ var WorkflowEngine = class _WorkflowEngine {
|
|
|
7726
7924
|
* an announcement carries no `where` or `input`. `routeIdOf` names the event for a subscriber's
|
|
7727
7925
|
* debounce/batch accumulator, which counts an id once. Returns the subscribers reached, with the
|
|
7728
7926
|
* run each started (none when the payload went into its debounce/batch accumulator), and the ones
|
|
7729
|
-
* that could not be started, with why.
|
|
7927
|
+
* that could not be started, with why. `batchTenant` is the tenant whose debounce/batch accumulator
|
|
7928
|
+
* the payload goes into (see {@link EventAccumulators.route}). `runTenant`, when set, is the tenant
|
|
7929
|
+
* every directly started subscriber run gets (a lifecycle event: the settled run's), instead of the
|
|
7930
|
+
* context's at publish time (which may be none, e.g. the background retry pass).
|
|
7730
7931
|
*/
|
|
7731
|
-
async startSubscribers(name, payload, runIdOf, only, remote = [], routeIdOf) {
|
|
7932
|
+
async startSubscribers(name, payload, runIdOf, only, remote = [], routeIdOf, batchTenant, runTenant) {
|
|
7732
7933
|
const reached = [];
|
|
7733
7934
|
const failed = [];
|
|
7734
7935
|
for (const [workflow, subscriptions] of this.eventTriggers.get(name) ?? []) {
|
|
@@ -7745,13 +7946,13 @@ var WorkflowEngine = class _WorkflowEngine {
|
|
|
7745
7946
|
const input = accepted.input ? accepted.input(payload) : payload;
|
|
7746
7947
|
const batch = this.latest.get(workflow)?.eventBatch;
|
|
7747
7948
|
if (batch) {
|
|
7748
|
-
await this.accumulators.route(workflow, batch, input, routeIdOf?.(workflow));
|
|
7949
|
+
await this.accumulators.route(workflow, batch, input, routeIdOf?.(workflow), batchTenant);
|
|
7749
7950
|
reached.push({
|
|
7750
7951
|
workflow
|
|
7751
7952
|
});
|
|
7752
7953
|
} else {
|
|
7753
7954
|
const runId = runIdOf(workflow);
|
|
7754
|
-
await this.start(workflow, input, runId);
|
|
7955
|
+
await this.start(workflow, input, runId, runTenant);
|
|
7755
7956
|
reached.push({
|
|
7756
7957
|
workflow,
|
|
7757
7958
|
runId
|
|
@@ -7769,7 +7970,7 @@ var WorkflowEngine = class _WorkflowEngine {
|
|
|
7769
7970
|
if (local?.has(workflow) || only && !only.has(workflow)) continue;
|
|
7770
7971
|
try {
|
|
7771
7972
|
const runId = runIdOf(workflow);
|
|
7772
|
-
await this.start(workflow, payload, runId);
|
|
7973
|
+
await this.start(workflow, payload, runId, runTenant);
|
|
7773
7974
|
reached.push({
|
|
7774
7975
|
workflow,
|
|
7775
7976
|
runId
|
|
@@ -8126,11 +8327,12 @@ var WorkflowEngine = class _WorkflowEngine {
|
|
|
8126
8327
|
const triggers = this.eventTriggers.get(lifecycleEventName(run.workflow, kind));
|
|
8127
8328
|
if (!triggers || !this.servesNamespace(run.namespace)) return out;
|
|
8128
8329
|
let payload;
|
|
8330
|
+
const tenant = this.lifecycleBatchTenant(run)?.id;
|
|
8129
8331
|
for (const [workflow, subscriptions] of triggers) {
|
|
8130
8332
|
const batch = this.latest.get(workflow)?.eventBatch;
|
|
8131
8333
|
if (!batch) continue;
|
|
8132
8334
|
if (!batch.key && subscriptions.every((sub2) => !sub2.where)) {
|
|
8133
|
-
out.set(workflow, accumulatorRunId(workflow));
|
|
8335
|
+
out.set(workflow, accumulatorRunId(workflow, void 0, tenant));
|
|
8134
8336
|
continue;
|
|
8135
8337
|
}
|
|
8136
8338
|
try {
|
|
@@ -8138,7 +8340,7 @@ var WorkflowEngine = class _WorkflowEngine {
|
|
|
8138
8340
|
const { accepted } = acceptingSubscription(subscriptions, payload);
|
|
8139
8341
|
if (!accepted) continue;
|
|
8140
8342
|
const input = accepted.input ? accepted.input(payload) : payload;
|
|
8141
|
-
out.set(workflow, accumulatorRunId(workflow, batch.key ? String(batch.key(input)) : void 0));
|
|
8343
|
+
out.set(workflow, accumulatorRunId(workflow, batch.key ? String(batch.key(input)) : void 0, tenant));
|
|
8142
8344
|
} catch {
|
|
8143
8345
|
out.set(workflow, void 0);
|
|
8144
8346
|
}
|
|
@@ -8317,7 +8519,9 @@ var WorkflowEngine = class _WorkflowEngine {
|
|
|
8317
8519
|
*/
|
|
8318
8520
|
async triggeredRuns(source) {
|
|
8319
8521
|
const run = typeof source === "string" ? await this.store.getRun(source) : source;
|
|
8320
|
-
|
|
8522
|
+
if (!run) return [];
|
|
8523
|
+
const filter = this.restrictiveFilter();
|
|
8524
|
+
return filter && !tenantVisible(run, filter) ? [] : triggeredRunsOf(run);
|
|
8321
8525
|
}
|
|
8322
8526
|
/** The live path of {@link publishSettled}: right after this instance settled the run. */
|
|
8323
8527
|
publishLifecycle(event) {
|
|
@@ -8450,7 +8654,18 @@ var WorkflowEngine = class _WorkflowEngine {
|
|
|
8450
8654
|
}
|
|
8451
8655
|
if (targets.length === 0 && owe.size === 0 && !resolved) return void 0;
|
|
8452
8656
|
const runIdOf = /* @__PURE__ */ __name((sub2) => lifecycleRunId(run.id, kind, sub2), "runIdOf");
|
|
8453
|
-
const { reached, failed } = targets.length > 0 ? await this.startSubscribers(
|
|
8657
|
+
const { reached, failed } = targets.length > 0 ? await this.startSubscribers(
|
|
8658
|
+
name,
|
|
8659
|
+
lifecycleEventOf(run),
|
|
8660
|
+
runIdOf,
|
|
8661
|
+
new Set(targets),
|
|
8662
|
+
remote,
|
|
8663
|
+
runIdOf,
|
|
8664
|
+
this.lifecycleBatchTenant(run),
|
|
8665
|
+
// The downstream run belongs to the settled run's customer, whoever publishes it (the
|
|
8666
|
+
// turn that settled it, or the background retry pass outside any request).
|
|
8667
|
+
inheritTenant(run)
|
|
8668
|
+
) : {
|
|
8454
8669
|
reached: [],
|
|
8455
8670
|
failed: []
|
|
8456
8671
|
};
|
|
@@ -8993,14 +9208,7 @@ var WorkflowEngine = class _WorkflowEngine {
|
|
|
8993
9208
|
}
|
|
8994
9209
|
await registered.validateInput?.(input);
|
|
8995
9210
|
const prior = await this.store.getRun(runId);
|
|
8996
|
-
if (prior)
|
|
8997
|
-
return {
|
|
8998
|
-
runId,
|
|
8999
|
-
status: prior.status,
|
|
9000
|
-
output: prior.output,
|
|
9001
|
-
error: prior.error
|
|
9002
|
-
};
|
|
9003
|
-
}
|
|
9211
|
+
if (prior) return this.existingStartResult(prior);
|
|
9004
9212
|
const quota = await resolveConcurrency(opts?.concurrency, registered.concurrency, input);
|
|
9005
9213
|
if (quota) await assertConcurrency(this.store, name, quota);
|
|
9006
9214
|
const now = /* @__PURE__ */ new Date();
|
|
@@ -9040,6 +9248,7 @@ var WorkflowEngine = class _WorkflowEngine {
|
|
|
9040
9248
|
tags,
|
|
9041
9249
|
searchAttributes: opts?.searchAttributes,
|
|
9042
9250
|
priority: opts?.priority,
|
|
9251
|
+
...this.startTenant(opts),
|
|
9043
9252
|
createdAt: now,
|
|
9044
9253
|
updatedAt: now
|
|
9045
9254
|
};
|
|
@@ -9048,12 +9257,7 @@ var WorkflowEngine = class _WorkflowEngine {
|
|
|
9048
9257
|
} catch (err) {
|
|
9049
9258
|
const winner = await this.store.getRun(runId).catch(() => null);
|
|
9050
9259
|
if (!winner) throw err;
|
|
9051
|
-
return
|
|
9052
|
-
runId,
|
|
9053
|
-
status: winner.status,
|
|
9054
|
-
output: winner.output,
|
|
9055
|
-
error: winner.error
|
|
9056
|
-
};
|
|
9260
|
+
return this.existingStartResult(winner);
|
|
9057
9261
|
}
|
|
9058
9262
|
if (delayed) {
|
|
9059
9263
|
this.emit({
|
|
@@ -9083,13 +9287,125 @@ var WorkflowEngine = class _WorkflowEngine {
|
|
|
9083
9287
|
status: "pending"
|
|
9084
9288
|
};
|
|
9085
9289
|
}
|
|
9086
|
-
/**
|
|
9290
|
+
/**
|
|
9291
|
+
* The tenant columns a new run gets: an explicit `opts.tenantId` (`null` = none) wins, else the
|
|
9292
|
+
* context tenant when tenancy is on, else none (no keys at all, so a run row is unchanged when
|
|
9293
|
+
* tenancy is off).
|
|
9294
|
+
*/
|
|
9295
|
+
startTenant(opts) {
|
|
9296
|
+
const tenant = opts?.tenantId !== void 0 ? opts.tenantId === null ? void 0 : {
|
|
9297
|
+
id: opts.tenantId,
|
|
9298
|
+
kind: opts.tenantKind
|
|
9299
|
+
} : this.tenancy?.current();
|
|
9300
|
+
if (!tenant) return {};
|
|
9301
|
+
return tenant.kind !== void 0 ? {
|
|
9302
|
+
tenantId: tenant.id,
|
|
9303
|
+
tenantKind: tenant.kind
|
|
9304
|
+
} : {
|
|
9305
|
+
tenantId: tenant.id
|
|
9306
|
+
};
|
|
9307
|
+
}
|
|
9308
|
+
/**
|
|
9309
|
+
* The context tenant a run started NOW would be stamped with (tenancy on), else undefined. For code
|
|
9310
|
+
* that records a start to replay it later outside the request, e.g. a transactional outbox.
|
|
9311
|
+
*/
|
|
9312
|
+
contextTenant() {
|
|
9313
|
+
return this.tenancy?.current();
|
|
9314
|
+
}
|
|
9315
|
+
/**
|
|
9316
|
+
* The tenant whose debounce/batch accumulator a named event published now goes into: the
|
|
9317
|
+
* publisher's tenant, or `null` (the shared accumulator, no tenant) for a tenantless, `system` or
|
|
9318
|
+
* `all` publisher. Undefined when tenancy is off (the accumulator starts as it always did).
|
|
9319
|
+
*/
|
|
9320
|
+
publishBatchTenant() {
|
|
9321
|
+
if (!this.tenancy) return void 0;
|
|
9322
|
+
const filter = this.tenantFilter();
|
|
9323
|
+
if (filter.mode !== "tenant") return null;
|
|
9324
|
+
const current = this.tenancy.current();
|
|
9325
|
+
return current?.id === filter.tenantId ? current : {
|
|
9326
|
+
id: filter.tenantId
|
|
9327
|
+
};
|
|
9328
|
+
}
|
|
9329
|
+
/**
|
|
9330
|
+
* The tenant whose debounce/batch accumulator `run`'s lifecycle event goes into: the run's own
|
|
9331
|
+
* (its publisher), or `null` for a run with no tenant. Undefined when tenancy is off.
|
|
9332
|
+
*/
|
|
9333
|
+
lifecycleBatchTenant(run) {
|
|
9334
|
+
if (!this.tenancy) return void 0;
|
|
9335
|
+
return rowTenant(run) ?? null;
|
|
9336
|
+
}
|
|
9337
|
+
/** The matching filter for an app-facing call made now: `system` (sees all) when tenancy is off. */
|
|
9338
|
+
tenantFilter() {
|
|
9339
|
+
return this.tenancy?.filter() ?? {
|
|
9340
|
+
mode: "system"
|
|
9341
|
+
};
|
|
9342
|
+
}
|
|
9343
|
+
/**
|
|
9344
|
+
* The run `runId` if the caller may see it under the tenancy matching rule, else null — an
|
|
9345
|
+
* invisible run reads exactly like a missing one (not found, never forbidden).
|
|
9346
|
+
*/
|
|
9347
|
+
visibleRun(runId) {
|
|
9348
|
+
const filter = this.restrictiveFilter();
|
|
9349
|
+
if (!filter) return this.store.getRun(runId);
|
|
9350
|
+
return this.store.getRun(runId).then((run) => run && tenantVisible(run, filter) ? run : null);
|
|
9351
|
+
}
|
|
9352
|
+
/**
|
|
9353
|
+
* What `start` returns for an id that already has a run. A run the caller may not see (tenancy)
|
|
9354
|
+
* returns its id and status only: never its output or error.
|
|
9355
|
+
*/
|
|
9356
|
+
existingStartResult(run) {
|
|
9357
|
+
const filter = this.restrictiveFilter();
|
|
9358
|
+
if (filter && !tenantVisible(run, filter)) return {
|
|
9359
|
+
runId: run.id,
|
|
9360
|
+
status: run.status
|
|
9361
|
+
};
|
|
9362
|
+
return {
|
|
9363
|
+
runId: run.id,
|
|
9364
|
+
status: run.status,
|
|
9365
|
+
output: run.output,
|
|
9366
|
+
error: run.error
|
|
9367
|
+
};
|
|
9368
|
+
}
|
|
9369
|
+
/** The caller's filter when it can hide rows (`tenant` / `tenantless`), else undefined. */
|
|
9370
|
+
restrictiveFilter() {
|
|
9371
|
+
const filter = this.tenantFilter();
|
|
9372
|
+
return filter.mode === "system" || filter.mode === "all" ? void 0 : filter;
|
|
9373
|
+
}
|
|
9374
|
+
/**
|
|
9375
|
+
* True when the caller may NOT act on `runId` (it is missing or invisible to it). Callers guard
|
|
9376
|
+
* with `this.restrictiveFilter() && (await this.hidden(runId))`, so nothing is awaited when the
|
|
9377
|
+
* caller sees everything.
|
|
9378
|
+
*/
|
|
9379
|
+
async hidden(runId) {
|
|
9380
|
+
return await this.visibleRun(runId) === null;
|
|
9381
|
+
}
|
|
9382
|
+
/**
|
|
9383
|
+
* Narrow `query` to the runs the caller may see (the tenancy matching rule). Use it before an
|
|
9384
|
+
* app-facing `store.listRuns`; a no-op when tenancy is off or the caller is `system`/`all`.
|
|
9385
|
+
*/
|
|
9386
|
+
scopeRunQuery(query) {
|
|
9387
|
+
return scopeTenantQuery(query, this.tenantFilter());
|
|
9388
|
+
}
|
|
9389
|
+
/**
|
|
9390
|
+
* Run `fn` with `run`'s tenant restored as the context tenant (so a step body, a downstream lib and
|
|
9391
|
+
* every dispatched task's context carrier see it), whatever path drove the turn: a request, a
|
|
9392
|
+
* timer, a signal, recovery or a worker. A run with no tenant, or tenancy off, runs `fn` as is.
|
|
9393
|
+
*/
|
|
9394
|
+
inRunTenant(run, fn) {
|
|
9395
|
+
const tenant = this.tenancy ? rowTenant(run) : void 0;
|
|
9396
|
+
return tenant && this.tenancy ? this.tenancy.run(tenant, fn) : fn();
|
|
9397
|
+
}
|
|
9398
|
+
/**
|
|
9399
|
+
* Read a run's current persisted state (or null if unknown). With tenancy on, a run of another
|
|
9400
|
+
* tenant reads as null (see {@link WorkflowEngineDeps.tenancy}).
|
|
9401
|
+
*/
|
|
9087
9402
|
getRun(runId) {
|
|
9088
|
-
return this.
|
|
9403
|
+
return this.visibleRun(runId);
|
|
9089
9404
|
}
|
|
9090
|
-
/** A run's full checkpoint timeline, ordered by seq
|
|
9091
|
-
*
|
|
9092
|
-
listCheckpoints(runId) {
|
|
9405
|
+
/** A run's full checkpoint timeline, ordered by seq (what the testing kit's `captureHistory`
|
|
9406
|
+
* reads, next to {@link getRun}). Empty for a run the caller may not see. */
|
|
9407
|
+
async listCheckpoints(runId) {
|
|
9408
|
+
if (this.restrictiveFilter() && await this.hidden(runId)) return [];
|
|
9093
9409
|
return this.store.listCheckpoints(runId);
|
|
9094
9410
|
}
|
|
9095
9411
|
/**
|
|
@@ -9101,6 +9417,13 @@ var WorkflowEngine = class _WorkflowEngine {
|
|
|
9101
9417
|
* terminal too.
|
|
9102
9418
|
*/
|
|
9103
9419
|
resume(runId) {
|
|
9420
|
+
if (this.restrictiveFilter()) {
|
|
9421
|
+
return this.hidden(runId).then((hidden) => {
|
|
9422
|
+
if (hidden) throw new Error(`run ${runId} not found`);
|
|
9423
|
+
this.noteWakeReason(runId, void 0);
|
|
9424
|
+
return this.resumeRun(runId, true);
|
|
9425
|
+
});
|
|
9426
|
+
}
|
|
9104
9427
|
this.noteWakeReason(runId, void 0);
|
|
9105
9428
|
return this.resumeRun(runId, true);
|
|
9106
9429
|
}
|
|
@@ -9979,10 +10302,12 @@ var WorkflowEngine = class _WorkflowEngine {
|
|
|
9979
10302
|
await this.store.removeBufferedEvent(candidate.id);
|
|
9980
10303
|
continue;
|
|
9981
10304
|
}
|
|
9982
|
-
|
|
10305
|
+
const { payload, tenantIds } = readBufferedEvent(candidate.payload);
|
|
10306
|
+
if (!bufferedEventReaches(tenantIds, run.tenantId)) continue;
|
|
10307
|
+
if (!eventMatches(payload, match)) continue;
|
|
9983
10308
|
if (!await this.store.removeBufferedEvent(candidate.id)) continue;
|
|
9984
10309
|
await this.store.removeSignalWaiter(waiter);
|
|
9985
|
-
const settled = await this.deliverSignal(waiter,
|
|
10310
|
+
const settled = await this.deliverSignal(waiter, payload, true);
|
|
9986
10311
|
return settled ?? {
|
|
9987
10312
|
runId: run.id,
|
|
9988
10313
|
status: run.status
|
|
@@ -10021,6 +10346,7 @@ var WorkflowEngine = class _WorkflowEngine {
|
|
|
10021
10346
|
* the HTTP request on workflow execution.
|
|
10022
10347
|
*/
|
|
10023
10348
|
async requeue(runId, _seen) {
|
|
10349
|
+
if (_seen === void 0 && this.restrictiveFilter() && await this.hidden(runId)) return null;
|
|
10024
10350
|
const seen = _seen ?? /* @__PURE__ */ new Set();
|
|
10025
10351
|
if (seen.has(runId)) return null;
|
|
10026
10352
|
seen.add(runId);
|
|
@@ -10128,7 +10454,7 @@ var WorkflowEngine = class _WorkflowEngine {
|
|
|
10128
10454
|
* run's current status (with the count re-dispatched), or null if the run is unknown.
|
|
10129
10455
|
*/
|
|
10130
10456
|
async redispatchPending(runId) {
|
|
10131
|
-
const run = await this.
|
|
10457
|
+
const run = await this.visibleRun(runId);
|
|
10132
10458
|
if (!run) return null;
|
|
10133
10459
|
const pending = (await this.store.listCheckpoints(runId)).filter((cp) => cp.kind === "remote" && cp.status === "pending");
|
|
10134
10460
|
let redispatched = 0;
|
|
@@ -10143,7 +10469,7 @@ var WorkflowEngine = class _WorkflowEngine {
|
|
|
10143
10469
|
startedAt: reEnqueuedAt,
|
|
10144
10470
|
finishedAt: reEnqueuedAt
|
|
10145
10471
|
});
|
|
10146
|
-
await this.dispatchRemoteTask({
|
|
10472
|
+
await this.inRunTenant(run, () => this.dispatchRemoteTask({
|
|
10147
10473
|
runId,
|
|
10148
10474
|
seq: cp.seq,
|
|
10149
10475
|
name: cp.name,
|
|
@@ -10151,7 +10477,7 @@ var WorkflowEngine = class _WorkflowEngine {
|
|
|
10151
10477
|
group: cp.workerGroup ?? sanitizeQueueToken(cp.name),
|
|
10152
10478
|
input: cp.input,
|
|
10153
10479
|
attempt: reAttempt
|
|
10154
|
-
});
|
|
10480
|
+
}));
|
|
10155
10481
|
redispatched += 1;
|
|
10156
10482
|
}
|
|
10157
10483
|
return {
|
|
@@ -10166,12 +10492,13 @@ var WorkflowEngine = class _WorkflowEngine {
|
|
|
10166
10492
|
* the original stays inspectable. Returns the new run's id, or null if `runId` is unknown.
|
|
10167
10493
|
*/
|
|
10168
10494
|
async retryWithInput(runId, input, newRunId) {
|
|
10169
|
-
const run = await this.
|
|
10495
|
+
const run = await this.visibleRun(runId);
|
|
10170
10496
|
if (!run) return null;
|
|
10171
10497
|
const id = newRunId ?? `${runId}~retry~${globalThis.crypto.randomUUID().slice(0, 8)}`;
|
|
10172
10498
|
await this.start(run.workflow, input, id, {
|
|
10173
10499
|
tags: run.tags,
|
|
10174
|
-
namespace: run.namespace
|
|
10500
|
+
namespace: run.namespace,
|
|
10501
|
+
...inheritTenant(run)
|
|
10175
10502
|
});
|
|
10176
10503
|
return {
|
|
10177
10504
|
runId: id
|
|
@@ -10220,6 +10547,7 @@ var WorkflowEngine = class _WorkflowEngine {
|
|
|
10220
10547
|
output: run.output,
|
|
10221
10548
|
error: run.error
|
|
10222
10549
|
}), "toResult");
|
|
10550
|
+
const filter = this.restrictiveFilter();
|
|
10223
10551
|
return new Promise((resolve, reject) => {
|
|
10224
10552
|
let done = false;
|
|
10225
10553
|
let off = /* @__PURE__ */ __name(() => {
|
|
@@ -10250,6 +10578,10 @@ var WorkflowEngine = class _WorkflowEngine {
|
|
|
10250
10578
|
const check = /* @__PURE__ */ __name(() => {
|
|
10251
10579
|
if (done) return;
|
|
10252
10580
|
void this.store.getRun(runId).then((run) => {
|
|
10581
|
+
if (run && filter && !tenantVisible(run, filter)) {
|
|
10582
|
+
fail(new Error(`run ${runId} not found`));
|
|
10583
|
+
return;
|
|
10584
|
+
}
|
|
10253
10585
|
if (run && isSettled(run)) finish(run);
|
|
10254
10586
|
}, fail);
|
|
10255
10587
|
}, "check");
|
|
@@ -10367,17 +10699,23 @@ var WorkflowEngine = class _WorkflowEngine {
|
|
|
10367
10699
|
*/
|
|
10368
10700
|
async publishEvent(name, payload, opts) {
|
|
10369
10701
|
let touched = 0;
|
|
10702
|
+
const filter = this.restrictiveFilter();
|
|
10703
|
+
const reaches = /* @__PURE__ */ __name(async (runId) => {
|
|
10704
|
+
if (!filter) return true;
|
|
10705
|
+
const run = await this.store.getRun(runId);
|
|
10706
|
+
return run !== null && tenantVisible(run, filter);
|
|
10707
|
+
}, "reaches");
|
|
10370
10708
|
const waiters = await this.store.listSignalWaiters(eventPrefix(name));
|
|
10371
10709
|
for (const w of waiters) {
|
|
10372
|
-
if (eventMatches(payload, eventMatchOf(w.token))) {
|
|
10373
|
-
await this.
|
|
10710
|
+
if (eventMatches(payload, eventMatchOf(w.token)) && await reaches(w.runId)) {
|
|
10711
|
+
await this.signalUnchecked(w.token, payload);
|
|
10374
10712
|
touched += 1;
|
|
10375
10713
|
}
|
|
10376
10714
|
}
|
|
10377
10715
|
const remote = await this.announcedRemoteSubscribers(name, null);
|
|
10378
10716
|
if (this.eventTriggers.get(name)?.size || remote.length > 0) {
|
|
10379
10717
|
const eventId = opts?.id ?? globalThis.crypto.randomUUID();
|
|
10380
|
-
const { reached, failed } = await this.startSubscribers(name, payload, (workflow) => `evt:${eventId}:${workflow}`, void 0, remote);
|
|
10718
|
+
const { reached, failed } = await this.startSubscribers(name, payload, (workflow) => `evt:${eventId}:${workflow}`, void 0, remote, void 0, this.publishBatchTenant());
|
|
10381
10719
|
touched += reached.length;
|
|
10382
10720
|
for (const { workflow, error: error2 } of failed) {
|
|
10383
10721
|
console.warn(`[nestjs-durable] event ${name}: subscriber ${workflow} not started:`, error2);
|
|
@@ -10387,12 +10725,18 @@ var WorkflowEngine = class _WorkflowEngine {
|
|
|
10387
10725
|
const bufferedId = globalThis.crypto.randomUUID();
|
|
10388
10726
|
await this.store.bufferEvent({
|
|
10389
10727
|
name,
|
|
10390
|
-
payload,
|
|
10728
|
+
payload: scopeBufferedEvent(payload, filter ? tenantIdsFor(filter) : void 0),
|
|
10391
10729
|
id: bufferedId,
|
|
10392
10730
|
publishedAt: this.clock()
|
|
10393
10731
|
});
|
|
10394
10732
|
const lateWaiters = await this.store.listSignalWaiters(eventPrefix(name));
|
|
10395
|
-
|
|
10733
|
+
let lateWaiter;
|
|
10734
|
+
for (const w of lateWaiters) {
|
|
10735
|
+
if (eventMatches(payload, eventMatchOf(w.token)) && await reaches(w.runId)) {
|
|
10736
|
+
lateWaiter = w;
|
|
10737
|
+
break;
|
|
10738
|
+
}
|
|
10739
|
+
}
|
|
10396
10740
|
if (!lateWaiter) return touched;
|
|
10397
10741
|
if (!await this.store.removeBufferedEvent(bufferedId)) return touched;
|
|
10398
10742
|
const waiter = await this.store.takeSignalWaiter(lateWaiter.token);
|
|
@@ -10699,7 +11043,23 @@ var WorkflowEngine = class _WorkflowEngine {
|
|
|
10699
11043
|
* Either way, the destructive `take*` ops (on the waiter row, on the buffer) arbitrate: whichever
|
|
10700
11044
|
* side actually removes the row is the one that delivers, so a signal is never delivered twice.
|
|
10701
11045
|
*/
|
|
10702
|
-
|
|
11046
|
+
signal(token, payload, opts) {
|
|
11047
|
+
const filter = parseWebhookToken(token) ? void 0 : this.restrictiveFilter();
|
|
11048
|
+
if (!filter) return this.signalUnchecked(token, payload, opts);
|
|
11049
|
+
return this.signalTargetVisible(token, filter).then((visible) => visible ? this.signalUnchecked(token, payload, opts) : null);
|
|
11050
|
+
}
|
|
11051
|
+
/**
|
|
11052
|
+
* False when a run waits on `token` and `filter` may not see it (the tenancy matching rule). A
|
|
11053
|
+
* token nobody waits on yet is buffered as before (there is no run to check).
|
|
11054
|
+
*/
|
|
11055
|
+
async signalTargetVisible(token, filter) {
|
|
11056
|
+
const waiter = (await this.store.listSignalWaiters(token)).find((w) => w.token === token);
|
|
11057
|
+
if (!waiter) return true;
|
|
11058
|
+
const run = await this.store.getRun(waiter.runId);
|
|
11059
|
+
return !run || tenantVisible(run, filter);
|
|
11060
|
+
}
|
|
11061
|
+
/** {@link signal} without the tenancy check — the engine's own deliveries. */
|
|
11062
|
+
async signalUnchecked(token, payload, opts) {
|
|
10703
11063
|
const id = opts?.id;
|
|
10704
11064
|
if (id === void 0) return this.deliverOrBuffer(token, payload);
|
|
10705
11065
|
if (!this.store.claimSignalId) {
|
|
@@ -10783,14 +11143,16 @@ var WorkflowEngine = class _WorkflowEngine {
|
|
|
10783
11143
|
* null if no run is waiting on the task (e.g. a duplicate/late delivery) — a safe no-op.
|
|
10784
11144
|
*/
|
|
10785
11145
|
async completeTask(runId, name, result) {
|
|
10786
|
-
|
|
11146
|
+
if (this.restrictiveFilter() && await this.hidden(runId)) return null;
|
|
11147
|
+
return this.signalUnchecked(`task:${runId}:${name}`, {
|
|
10787
11148
|
ok: true,
|
|
10788
11149
|
value: result
|
|
10789
11150
|
});
|
|
10790
11151
|
}
|
|
10791
11152
|
/** Report that a `ctx.task` failed — the run resumes and throws a FatalError at the task. */
|
|
10792
11153
|
async failTask(runId, name, error2) {
|
|
10793
|
-
|
|
11154
|
+
if (this.restrictiveFilter() && await this.hidden(runId)) return null;
|
|
11155
|
+
return this.signalUnchecked(`task:${runId}:${name}`, {
|
|
10794
11156
|
ok: false,
|
|
10795
11157
|
error: error2
|
|
10796
11158
|
});
|
|
@@ -10829,7 +11191,7 @@ var WorkflowEngine = class _WorkflowEngine {
|
|
|
10829
11191
|
if (cancelled) return;
|
|
10830
11192
|
const childId = token.slice(CHILD_TOKEN_PREFIX.length);
|
|
10831
11193
|
if (!await childCompletionNeedsBuffer(this.store, childId, completion)) return;
|
|
10832
|
-
await this.
|
|
11194
|
+
await this.signalUnchecked(token, completion);
|
|
10833
11195
|
return;
|
|
10834
11196
|
} catch (err) {
|
|
10835
11197
|
if (err instanceof NamespaceMismatch) return;
|
|
@@ -10878,7 +11240,12 @@ var WorkflowEngine = class _WorkflowEngine {
|
|
|
10878
11240
|
* steps' compensations run in reverse (visible as `compensate:<step>` events), then it's marked
|
|
10879
11241
|
* cancelled. Without it, cancellation is immediate (no undo).
|
|
10880
11242
|
*/
|
|
10881
|
-
|
|
11243
|
+
cancel(runId, opts) {
|
|
11244
|
+
if (!this.restrictiveFilter()) return this.cancelUnchecked(runId, opts);
|
|
11245
|
+
return this.hidden(runId).then((hidden) => hidden ? null : this.cancelUnchecked(runId, opts));
|
|
11246
|
+
}
|
|
11247
|
+
/** {@link cancel} without the tenancy check — the engine's own cascades and bulk paths. */
|
|
11248
|
+
async cancelUnchecked(runId, opts) {
|
|
10882
11249
|
const run = await this.store.getRun(runId);
|
|
10883
11250
|
if (!run) return null;
|
|
10884
11251
|
if (TERMINAL_RUN_STATUSES.includes(run.status)) {
|
|
@@ -10960,10 +11327,10 @@ var WorkflowEngine = class _WorkflowEngine {
|
|
|
10960
11327
|
* their terminal status — `cancel` is a no-op on them, never clobbering a completed/dead run).
|
|
10961
11328
|
*/
|
|
10962
11329
|
async cancelWhere(filter, opts) {
|
|
10963
|
-
const runs = await this.store.listRuns(filter);
|
|
11330
|
+
const runs = await this.store.listRuns(this.scopeRunQuery(filter));
|
|
10964
11331
|
const results = [];
|
|
10965
11332
|
for (const run of runs) {
|
|
10966
|
-
const r = await this.
|
|
11333
|
+
const r = await this.cancelUnchecked(run.id, opts);
|
|
10967
11334
|
if (r) results.push(r);
|
|
10968
11335
|
}
|
|
10969
11336
|
return results;
|
|
@@ -10979,11 +11346,16 @@ var WorkflowEngine = class _WorkflowEngine {
|
|
|
10979
11346
|
* its worker (its next checkpoint write fails). Returns the number of runs deleted (0 if absent).
|
|
10980
11347
|
*/
|
|
10981
11348
|
async deleteRun(runId) {
|
|
11349
|
+
if (this.restrictiveFilter() && await this.hidden(runId)) return 0;
|
|
11350
|
+
return await this.deleteRunUnchecked(runId);
|
|
11351
|
+
}
|
|
11352
|
+
/** {@link deleteRun} without the tenancy check (its own subtree recursion). */
|
|
11353
|
+
async deleteRunUnchecked(runId) {
|
|
10982
11354
|
const run = await this.store.getRun(runId);
|
|
10983
11355
|
if (!run) return 0;
|
|
10984
11356
|
let deleted = 0;
|
|
10985
|
-
for (const childId of await this.
|
|
10986
|
-
deleted += await this.
|
|
11357
|
+
for (const childId of await this.runChildrenUnchecked(runId)) {
|
|
11358
|
+
deleted += await this.deleteRunUnchecked(childId);
|
|
10987
11359
|
}
|
|
10988
11360
|
await this.store.deleteRun(runId);
|
|
10989
11361
|
return deleted + 1;
|
|
@@ -11012,6 +11384,7 @@ var WorkflowEngine = class _WorkflowEngine {
|
|
|
11012
11384
|
if (!isNonEmptyRunScope(scope)) {
|
|
11013
11385
|
throw new Error("purgeRuns: refusing an empty scope (it would delete every run) \u2014 pass at least one predicate, e.g. { namespace }");
|
|
11014
11386
|
}
|
|
11387
|
+
const scoped = this.scopeRunQuery(scope);
|
|
11015
11388
|
const batchSize = Math.max(1, opts?.batchSize ?? 500);
|
|
11016
11389
|
const cancelLive = opts?.cancelLive ?? true;
|
|
11017
11390
|
const withChildren = opts?.children ?? true;
|
|
@@ -11022,7 +11395,7 @@ var WorkflowEngine = class _WorkflowEngine {
|
|
|
11022
11395
|
let previous = "";
|
|
11023
11396
|
for (; ; ) {
|
|
11024
11397
|
const batch = await this.store.listRuns({
|
|
11025
|
-
...
|
|
11398
|
+
...scoped,
|
|
11026
11399
|
statuses,
|
|
11027
11400
|
limit: batchSize
|
|
11028
11401
|
});
|
|
@@ -11043,7 +11416,7 @@ var WorkflowEngine = class _WorkflowEngine {
|
|
|
11043
11416
|
if (status === void 0) continue;
|
|
11044
11417
|
if (!TERMINAL_RUN_STATUSES.includes(status)) {
|
|
11045
11418
|
if (!cancelLive) continue;
|
|
11046
|
-
await this.
|
|
11419
|
+
await this.cancelUnchecked(id).catch(() => void 0);
|
|
11047
11420
|
}
|
|
11048
11421
|
doomed.push(id);
|
|
11049
11422
|
}
|
|
@@ -11104,10 +11477,10 @@ var WorkflowEngine = class _WorkflowEngine {
|
|
|
11104
11477
|
* explicitly cancelled one's does, or the children are orphans nobody is holding a reference to.
|
|
11105
11478
|
*/
|
|
11106
11479
|
async cancelChildren(parentRunId, opts) {
|
|
11107
|
-
for (const id of await this.
|
|
11480
|
+
for (const id of await this.runChildrenUnchecked(parentRunId)) {
|
|
11108
11481
|
const child = await this.store.getRun(id).catch(() => null);
|
|
11109
11482
|
if (child?.tags?.includes(`${PARENT_CLOSE_TAG_PREFIX}abandon`)) continue;
|
|
11110
|
-
await this.
|
|
11483
|
+
await this.cancelUnchecked(id, opts).catch(() => void 0);
|
|
11111
11484
|
}
|
|
11112
11485
|
}
|
|
11113
11486
|
/**
|
|
@@ -11125,7 +11498,7 @@ var WorkflowEngine = class _WorkflowEngine {
|
|
|
11125
11498
|
if (event.type === "run.suspended") return;
|
|
11126
11499
|
for (const [childId, policy] of intents) {
|
|
11127
11500
|
if (policy === "abandon") continue;
|
|
11128
|
-
queueMicrotask(() => void this.
|
|
11501
|
+
queueMicrotask(() => void this.cancelUnchecked(childId, {
|
|
11129
11502
|
compensate: policy === "cancel"
|
|
11130
11503
|
}).catch(() => void 0));
|
|
11131
11504
|
}
|
|
@@ -11149,7 +11522,13 @@ var WorkflowEngine = class _WorkflowEngine {
|
|
|
11149
11522
|
parentRunId
|
|
11150
11523
|
])).filter((w) => w.token.startsWith("child:") && !isAnyMember(w)).map((w) => w.token.slice("child:".length));
|
|
11151
11524
|
}
|
|
11525
|
+
/** The child runs of `parentRunId`; empty for a parent the caller may not see. */
|
|
11152
11526
|
async getRunChildren(parentRunId) {
|
|
11527
|
+
if (this.restrictiveFilter() && await this.hidden(parentRunId)) return [];
|
|
11528
|
+
return this.runChildrenUnchecked(parentRunId);
|
|
11529
|
+
}
|
|
11530
|
+
/** {@link getRunChildren} without the tenancy check (the engine's own subtree walks). */
|
|
11531
|
+
async runChildrenUnchecked(parentRunId) {
|
|
11153
11532
|
const childIds = new Set(await this.awaitedChildIds(parentRunId));
|
|
11154
11533
|
const prefixes = CHILD_EDGE_PREFIXES;
|
|
11155
11534
|
const childCheckpoints = this.store.listCheckpointsByNamePrefix ? await this.store.listCheckpointsByNamePrefix(parentRunId, prefixes) : (await this.store.listCheckpoints(parentRunId)).filter((cp) => prefixes.some((p) => cp.name.startsWith(p)));
|
|
@@ -11483,26 +11862,87 @@ var WorkflowEngine = class _WorkflowEngine {
|
|
|
11483
11862
|
const reentered = run.lockedBy === this.instanceId && (run.lockedUntil ?? 0) > lockNow && (lockedForTurn || await this.proveOwnLease(run.id, lockNow));
|
|
11484
11863
|
if (!reentered) {
|
|
11485
11864
|
if (!await this.store.tryLockRun(run.id, this.instanceId, lockNow + this.leaseMs, lockNow)) {
|
|
11486
|
-
if (
|
|
11487
|
-
|
|
11488
|
-
|
|
11489
|
-
|
|
11490
|
-
}
|
|
11491
|
-
} else {
|
|
11492
|
-
const fans = {
|
|
11493
|
-
fans: [
|
|
11494
|
-
...reason.fans
|
|
11495
|
-
]
|
|
11496
|
-
};
|
|
11497
|
-
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);
|
|
11498
11869
|
}
|
|
11499
|
-
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);
|
|
11500
11923
|
return {
|
|
11501
|
-
|
|
11502
|
-
status: run.status
|
|
11924
|
+
result: fresh ? this.resultOf(fresh) : await this.echoCurrentStatus(runId)
|
|
11503
11925
|
};
|
|
11504
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));
|
|
11505
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) {
|
|
11506
11946
|
const settledFans = /* @__PURE__ */ new Map();
|
|
11507
11947
|
this.turnSettledFans.set(run.id, settledFans);
|
|
11508
11948
|
const held = this.holdExecutionLease(run.id);
|
|
@@ -11510,7 +11950,7 @@ var WorkflowEngine = class _WorkflowEngine {
|
|
|
11510
11950
|
const served = /* @__PURE__ */ new Set();
|
|
11511
11951
|
this.turnServedWakes.set(run.id, served);
|
|
11512
11952
|
if (registered?.remote) await this.noteServedWakes(run.id);
|
|
11513
|
-
const result = registered?.remote ?
|
|
11953
|
+
const result = await this.inRunTenant(run, () => registered?.remote ? this.runRemoteExecution(run, registered) : this.runExecution(run, fn, registered));
|
|
11514
11954
|
if (registered?.singleton && (result.status === "completed" || result.status === "failed" || result.status === "cancelled" || result.status === "dead")) {
|
|
11515
11955
|
void this.singletons.wakeNext(run).catch(() => void 0);
|
|
11516
11956
|
}
|
|
@@ -11826,7 +12266,8 @@ var WorkflowEngine = class _WorkflowEngine {
|
|
|
11826
12266
|
error: error2
|
|
11827
12267
|
});
|
|
11828
12268
|
this.startChildDeferred(handler.workflow, handler.input, handlerId, {
|
|
11829
|
-
namespace: run.namespace
|
|
12269
|
+
namespace: run.namespace,
|
|
12270
|
+
...inheritTenant(run)
|
|
11830
12271
|
});
|
|
11831
12272
|
return {
|
|
11832
12273
|
kind: "suspended"
|
|
@@ -11873,7 +12314,8 @@ var WorkflowEngine = class _WorkflowEngine {
|
|
|
11873
12314
|
if (!handlerRun) {
|
|
11874
12315
|
const handler = handoff.input;
|
|
11875
12316
|
this.startChildDeferred(handler.workflow, handler.input, handlerId, {
|
|
11876
|
-
namespace: run.namespace
|
|
12317
|
+
namespace: run.namespace,
|
|
12318
|
+
...inheritTenant(run)
|
|
11877
12319
|
});
|
|
11878
12320
|
}
|
|
11879
12321
|
return {
|
|
@@ -12658,7 +13100,8 @@ ${run.workflowVersion}`;
|
|
|
12658
13100
|
const existingChild = await this.store.getRun(childId);
|
|
12659
13101
|
if (!existingChild) {
|
|
12660
13102
|
this.startChildDeferred(cmd.workflow, cmd.input, childId, {
|
|
12661
|
-
namespace: run.namespace
|
|
13103
|
+
namespace: run.namespace,
|
|
13104
|
+
...inheritTenant(run)
|
|
12662
13105
|
});
|
|
12663
13106
|
} else {
|
|
12664
13107
|
this.redeliverFinishedChild(childId, existingChild);
|
|
@@ -12777,7 +13220,7 @@ ${run.workflowVersion}`;
|
|
|
12777
13220
|
this.awaitedRemoteSteps.set(run.id, awaitedSteps);
|
|
12778
13221
|
const awaitedFans = [];
|
|
12779
13222
|
this.awaitedStepFans.set(run.id, awaitedFans);
|
|
12780
|
-
const ctx = warm ? void 0 : createWorkflowCtx(this.ctxHostFor(replay, run.namespace, cont), run.id, compensations, run.workflow, registered?.searchAttributesSchema);
|
|
13223
|
+
const ctx = warm ? void 0 : createWorkflowCtx(this.ctxHostFor(replay, run.namespace, cont, inheritTenant(run)), run.id, compensations, run.workflow, registered?.searchAttributesSchema);
|
|
12781
13224
|
try {
|
|
12782
13225
|
const handoff = liveHandoff(run.id, snapshot);
|
|
12783
13226
|
if (handoff && !this.cancelRequested.has(run.id)) {
|
|
@@ -12835,7 +13278,7 @@ ${run.workflowVersion}`;
|
|
|
12835
13278
|
value: void 0
|
|
12836
13279
|
});
|
|
12837
13280
|
const nextId = nextContinuationId(run.id);
|
|
12838
|
-
queueMicrotask(() => void this.start(run.workflow, err.input, nextId).catch(() => void 0));
|
|
13281
|
+
queueMicrotask(() => void this.start(run.workflow, err.input, nextId, inheritTenant(run)).catch(() => void 0));
|
|
12839
13282
|
return {
|
|
12840
13283
|
runId: run.id,
|
|
12841
13284
|
status: "completed"
|
|
@@ -13108,7 +13551,7 @@ ${run.workflowVersion}`;
|
|
|
13108
13551
|
/** The seam handed to {@link createWorkflowCtx}: the authoring API reaches durability + lifecycle
|
|
13109
13552
|
* (checkpointing, dispatch, child start) through this, so the ctx primitives live in their own
|
|
13110
13553
|
* module and the engine stays the orchestrator. */
|
|
13111
|
-
ctxHostFor(replay, parentNamespace, sticky) {
|
|
13554
|
+
ctxHostFor(replay, parentNamespace, sticky, parentTenant) {
|
|
13112
13555
|
return {
|
|
13113
13556
|
store: this.store,
|
|
13114
13557
|
replay,
|
|
@@ -13155,6 +13598,8 @@ ${run.workflowVersion}`;
|
|
|
13155
13598
|
this.startChildDeferred(workflow, input, id, {
|
|
13156
13599
|
priority,
|
|
13157
13600
|
namespace: parentNamespace,
|
|
13601
|
+
// The child belongs to the parent's customer, whichever instance or context drives it.
|
|
13602
|
+
...parentTenant,
|
|
13158
13603
|
// Absent unless the body pinned one (`ctx.child(ref, input, { version })`) — an omitted
|
|
13159
13604
|
// pin must stay omitted, so the child keeps resolving `latest` exactly as before.
|
|
13160
13605
|
version,
|
|
@@ -13178,7 +13623,7 @@ ${run.workflowVersion}`;
|
|
|
13178
13623
|
// idempotent on a terminal/cancelled run (returns its existing status without side effects), so
|
|
13179
13624
|
// no extra guard is needed here for the failFast replay case (re-issuing the same cancel calls).
|
|
13180
13625
|
cancelChild: /* @__PURE__ */ __name((childId) => {
|
|
13181
|
-
queueMicrotask(() => void this.
|
|
13626
|
+
queueMicrotask(() => void this.cancelUnchecked(childId).catch(() => void 0));
|
|
13182
13627
|
}, "cancelChild"),
|
|
13183
13628
|
// Shallow-merge into the run's searchAttributes (the ctx primitive makes this exactly-once).
|
|
13184
13629
|
upsertSearchAttributes: /* @__PURE__ */ __name(async (runId, attrs) => {
|
|
@@ -13205,7 +13650,7 @@ ${run.workflowVersion}`;
|
|
|
13205
13650
|
console.warn(`[nestjs-durable] waking entity "${name}" key "${key}" failed (its op stays buffered for the next wake):`, err);
|
|
13206
13651
|
}));
|
|
13207
13652
|
}, "kickEntity"),
|
|
13208
|
-
interceptStep: /* @__PURE__ */ __name((invocation, body) => this.interceptStep(invocation, body), "interceptStep")
|
|
13653
|
+
interceptStep: /* @__PURE__ */ __name((invocation, body) => this.interceptStep(invocation, body, parentTenant), "interceptStep")
|
|
13209
13654
|
};
|
|
13210
13655
|
}
|
|
13211
13656
|
/**
|
|
@@ -13214,10 +13659,11 @@ ${run.workflowVersion}`;
|
|
|
13214
13659
|
* paused at a breakpoint.
|
|
13215
13660
|
*/
|
|
13216
13661
|
async continue(runId) {
|
|
13662
|
+
if (this.restrictiveFilter() && await this.hidden(runId)) return null;
|
|
13217
13663
|
const checkpoints = await this.store.listCheckpoints(runId);
|
|
13218
13664
|
const bp = checkpoints.find(isBreakpoint);
|
|
13219
13665
|
if (!bp) return null;
|
|
13220
|
-
return this.
|
|
13666
|
+
return this.signalUnchecked(breakpointToken(runId, bp.seq), void 0);
|
|
13221
13667
|
}
|
|
13222
13668
|
/**
|
|
13223
13669
|
* Read the latest value a run published for `key` via {@link WorkflowCtx.setEvent} — a
|
|
@@ -13225,6 +13671,7 @@ ${run.workflowVersion}`;
|
|
|
13225
13671
|
* never published that key. The suspend-model counterpart of a Temporal query.
|
|
13226
13672
|
*/
|
|
13227
13673
|
async getEvent(runId, key) {
|
|
13674
|
+
if (this.restrictiveFilter() && await this.hidden(runId)) return void 0;
|
|
13228
13675
|
const name = `event:${key}`;
|
|
13229
13676
|
if (this.store.getLatestCheckpointByName) {
|
|
13230
13677
|
const latest2 = await this.store.getLatestCheckpointByName(runId, name);
|
|
@@ -13249,7 +13696,7 @@ ${run.workflowVersion}`;
|
|
|
13249
13696
|
* if nothing was waiting — a too-early or duplicate update).
|
|
13250
13697
|
*/
|
|
13251
13698
|
async update(runId, name, arg) {
|
|
13252
|
-
const run = await this.
|
|
13699
|
+
const run = await this.visibleRun(runId);
|
|
13253
13700
|
if (!run) return {
|
|
13254
13701
|
accepted: false,
|
|
13255
13702
|
reason: `run ${runId} not found`
|
|
@@ -13269,7 +13716,7 @@ ${run.workflowVersion}`;
|
|
|
13269
13716
|
};
|
|
13270
13717
|
}
|
|
13271
13718
|
}
|
|
13272
|
-
const result = await this.
|
|
13719
|
+
const result = await this.signalUnchecked(`update:${runId}:${name}`, arg);
|
|
13273
13720
|
return {
|
|
13274
13721
|
accepted: true,
|
|
13275
13722
|
run: result
|
|
@@ -16454,6 +16901,9 @@ var InMemoryStateStore = class {
|
|
|
16454
16901
|
if (query.origins) {
|
|
16455
16902
|
runs = runs.filter((r) => query.origins?.includes(r.origin ?? null));
|
|
16456
16903
|
}
|
|
16904
|
+
if (query.tenantId !== void 0 || query.tenantIds !== void 0) {
|
|
16905
|
+
runs = runs.filter((r) => matchesTenantPredicates(r, query));
|
|
16906
|
+
}
|
|
16457
16907
|
if (query.attributes?.length) {
|
|
16458
16908
|
let candidates = null;
|
|
16459
16909
|
for (const f of query.attributes) {
|
|
@@ -16527,7 +16977,7 @@ var InMemoryStateStore = class {
|
|
|
16527
16977
|
async listSchedules(query) {
|
|
16528
16978
|
const rows = [
|
|
16529
16979
|
...this.schedules.values()
|
|
16530
|
-
].filter((s) => query.namespace === void 0 || s.namespace === query.namespace).filter((s) => query.workflow === void 0 || s.workflow === query.workflow).filter((s) => query.tag === void 0 || (s.tags ?? []).includes(query.tag)).filter((s) => query.paused === void 0 || s.paused === query.paused).filter((s) => query.dueBy === void 0 || !s.paused && s.nextFireAt !== null && s.nextFireAt <= query.dueBy).sort((a, b) => (a.nextFireAt ?? Number.POSITIVE_INFINITY) - (b.nextFireAt ?? Number.POSITIVE_INFINITY) || a.id.localeCompare(b.id));
|
|
16980
|
+
].filter((s) => query.namespace === void 0 || s.namespace === query.namespace).filter((s) => query.workflow === void 0 || s.workflow === query.workflow).filter((s) => query.tag === void 0 || (s.tags ?? []).includes(query.tag)).filter((s) => query.paused === void 0 || s.paused === query.paused).filter((s) => matchesTenantPredicates(s, query)).filter((s) => query.dueBy === void 0 || !s.paused && s.nextFireAt !== null && s.nextFireAt <= query.dueBy).sort((a, b) => (a.nextFireAt ?? Number.POSITIVE_INFINITY) - (b.nextFireAt ?? Number.POSITIVE_INFINITY) || a.id.localeCompare(b.id));
|
|
16531
16981
|
const offset = query.offset ?? 0;
|
|
16532
16982
|
return rows.slice(offset, query.limit != null ? offset + query.limit : void 0).map((s) => structuredClone(s));
|
|
16533
16983
|
}
|
|
@@ -16899,6 +17349,7 @@ __name(settleResultsWithPlainCalls, "settleResultsWithPlainCalls");
|
|
|
16899
17349
|
axisIsRunColumn,
|
|
16900
17350
|
bindWorkflowClass,
|
|
16901
17351
|
breakpointToken,
|
|
17352
|
+
bufferedEventReaches,
|
|
16902
17353
|
buildWorkflowDirectory,
|
|
16903
17354
|
canRoute,
|
|
16904
17355
|
capableWorkers,
|
|
@@ -16946,6 +17397,7 @@ __name(settleResultsWithPlainCalls, "settleResultsWithPlainCalls");
|
|
|
16946
17397
|
hmacTenantVerifier,
|
|
16947
17398
|
indexWaitersByRun,
|
|
16948
17399
|
info,
|
|
17400
|
+
inheritTenant,
|
|
16949
17401
|
isAnyCheckpoint,
|
|
16950
17402
|
isAnyMember,
|
|
16951
17403
|
isEngineMintedTag,
|
|
@@ -16972,6 +17424,7 @@ __name(settleResultsWithPlainCalls, "settleResultsWithPlainCalls");
|
|
|
16972
17424
|
log,
|
|
16973
17425
|
markRemoteEvent,
|
|
16974
17426
|
matchesAttributes,
|
|
17427
|
+
matchesTenantPredicates,
|
|
16975
17428
|
mergeRunFacetRows,
|
|
16976
17429
|
mergeRunValueFacetRows,
|
|
16977
17430
|
missingCapabilities,
|
|
@@ -16996,6 +17449,7 @@ __name(settleResultsWithPlainCalls, "settleResultsWithPlainCalls");
|
|
|
16996
17449
|
prevCronFireMs,
|
|
16997
17450
|
prevRRuleFireMs,
|
|
16998
17451
|
queryCheckpointsOf,
|
|
17452
|
+
readBufferedEvent,
|
|
16999
17453
|
readRunsByIds,
|
|
17000
17454
|
readSearchAttributes,
|
|
17001
17455
|
requiredCapabilities,
|
|
@@ -17007,6 +17461,7 @@ __name(settleResultsWithPlainCalls, "settleResultsWithPlainCalls");
|
|
|
17007
17461
|
retentionKeeps,
|
|
17008
17462
|
retiredHandoffName,
|
|
17009
17463
|
routeFor,
|
|
17464
|
+
rowTenant,
|
|
17010
17465
|
runCompletedEvent,
|
|
17011
17466
|
runDetailEtag,
|
|
17012
17467
|
runDurableExecution,
|
|
@@ -17024,6 +17479,8 @@ __name(settleResultsWithPlainCalls, "settleResultsWithPlainCalls");
|
|
|
17024
17479
|
sanitizeQueueToken,
|
|
17025
17480
|
scheduleRunIdAt,
|
|
17026
17481
|
scheduledRunId,
|
|
17482
|
+
scopeBufferedEvent,
|
|
17483
|
+
scopeTenantQuery,
|
|
17027
17484
|
settleResultsWithPlainCalls,
|
|
17028
17485
|
signTenantToken,
|
|
17029
17486
|
singletonAdmissionDenialPatch,
|
|
@@ -17040,6 +17497,8 @@ __name(settleResultsWithPlainCalls, "settleResultsWithPlainCalls");
|
|
|
17040
17497
|
summarizeRunFromStore,
|
|
17041
17498
|
summarizeTimeline,
|
|
17042
17499
|
tenantGroup,
|
|
17500
|
+
tenantIdsFor,
|
|
17501
|
+
tenantVisible,
|
|
17043
17502
|
toEventSubscription,
|
|
17044
17503
|
traceIdOfTraceparent,
|
|
17045
17504
|
triggerFailedSubscribersOf,
|