@dudousxd/nestjs-durable 0.54.0 → 0.55.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 +324 -196
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +28 -3
- package/dist/index.d.ts +28 -3
- package/dist/index.js +230 -102
- package/dist/index.js.map +1 -1
- package/dist/migrate-from-nestjs-workflows.cjs +766 -0
- package/dist/migrate-from-nestjs-workflows.cjs.map +1 -0
- package/dist/migrate-from-nestjs-workflows.d.cts +125 -0
- package/dist/migrate-from-nestjs-workflows.d.ts +125 -0
- package/dist/migrate-from-nestjs-workflows.js +741 -0
- package/dist/migrate-from-nestjs-workflows.js.map +1 -0
- package/dist/outbox.cjs +243 -0
- package/dist/outbox.cjs.map +1 -0
- package/dist/outbox.d.cts +117 -0
- package/dist/outbox.d.ts +117 -0
- package/dist/outbox.js +213 -0
- package/dist/outbox.js.map +1 -0
- package/package.json +53 -11
- package/skills/durable-workflows/SKILL.md +8 -2
|
@@ -0,0 +1,766 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
+
|
|
21
|
+
// src/migrate-from-nestjs-workflows/index.ts
|
|
22
|
+
var migrate_from_nestjs_workflows_exports = {};
|
|
23
|
+
__export(migrate_from_nestjs_workflows_exports, {
|
|
24
|
+
CombinedRunGateway: () => CombinedRunGateway,
|
|
25
|
+
DurableMigrationModule: () => DurableMigrationModule,
|
|
26
|
+
MigrationClient: () => MigrationClient
|
|
27
|
+
});
|
|
28
|
+
module.exports = __toCommonJS(migrate_from_nestjs_workflows_exports);
|
|
29
|
+
var import_nestjs_durable_core3 = require("@dudousxd/nestjs-durable-core");
|
|
30
|
+
var import_nestjs_durable_workflows_gateway = require("@dudousxd/nestjs-durable-workflows-gateway");
|
|
31
|
+
var import_common2 = require("@nestjs/common");
|
|
32
|
+
var import_workflows = require("@nestjs/workflows");
|
|
33
|
+
|
|
34
|
+
// src/store-run-gateway.ts
|
|
35
|
+
var import_nestjs_durable_core = require("@dudousxd/nestjs-durable-core");
|
|
36
|
+
var import_common = require("@nestjs/common");
|
|
37
|
+
function _ts_decorate(decorators, target, key, desc) {
|
|
38
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
39
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") {
|
|
40
|
+
r = Reflect.decorate(decorators, target, key, desc);
|
|
41
|
+
} else {
|
|
42
|
+
for (var i = decorators.length - 1; i >= 0; i--) {
|
|
43
|
+
if (d = decorators[i]) {
|
|
44
|
+
r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
49
|
+
}
|
|
50
|
+
__name(_ts_decorate, "_ts_decorate");
|
|
51
|
+
function _ts_metadata(metadataKey, metadataValue) {
|
|
52
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") {
|
|
53
|
+
return Reflect.metadata(metadataKey, metadataValue);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
__name(_ts_metadata, "_ts_metadata");
|
|
57
|
+
function _ts_param(paramIndex, decorator) {
|
|
58
|
+
return function(target, key) {
|
|
59
|
+
decorator(target, key, paramIndex);
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
__name(_ts_param, "_ts_param");
|
|
63
|
+
function detailExtras(links) {
|
|
64
|
+
const { lifecycleSubscribers, lifecycleDeliveries, waitsFor, awaitedBy, triggeredBy } = links;
|
|
65
|
+
if (!lifecycleSubscribers && !lifecycleDeliveries && !waitsFor && !awaitedBy && !triggeredBy) {
|
|
66
|
+
return void 0;
|
|
67
|
+
}
|
|
68
|
+
return {
|
|
69
|
+
lifecycleSubscribers,
|
|
70
|
+
lifecycleDeliveries,
|
|
71
|
+
waitsFor,
|
|
72
|
+
awaitedBy,
|
|
73
|
+
triggeredBy
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
__name(detailExtras, "detailExtras");
|
|
77
|
+
function withoutRegistry(query) {
|
|
78
|
+
if (query.workflow === import_nestjs_durable_core.LIFECYCLE_REGISTRY_WORKFLOW || query.workflows?.includes(import_nestjs_durable_core.LIFECYCLE_REGISTRY_WORKFLOW)) {
|
|
79
|
+
return query;
|
|
80
|
+
}
|
|
81
|
+
return {
|
|
82
|
+
...query,
|
|
83
|
+
excludeWorkflows: [
|
|
84
|
+
...query.excludeWorkflows ?? [],
|
|
85
|
+
import_nestjs_durable_core.LIFECYCLE_REGISTRY_WORKFLOW
|
|
86
|
+
]
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
__name(withoutRegistry, "withoutRegistry");
|
|
90
|
+
var StoreRunGateway = class {
|
|
91
|
+
static {
|
|
92
|
+
__name(this, "StoreRunGateway");
|
|
93
|
+
}
|
|
94
|
+
store;
|
|
95
|
+
engine;
|
|
96
|
+
constructor(store, engine) {
|
|
97
|
+
this.store = store;
|
|
98
|
+
this.engine = engine;
|
|
99
|
+
}
|
|
100
|
+
topology() {
|
|
101
|
+
return {
|
|
102
|
+
role: "control-plane"
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
async getRunDetail(runId, options) {
|
|
106
|
+
const run = await this.store.getRun(runId);
|
|
107
|
+
if (!run) return null;
|
|
108
|
+
const links = await this.lifecycleLinks(run);
|
|
109
|
+
const over = options?.summarizeFansOver;
|
|
110
|
+
if (over === void 0) {
|
|
111
|
+
const [timeline2, children] = await Promise.all([
|
|
112
|
+
this.store.listCheckpoints(runId),
|
|
113
|
+
this.engine.getRunChildren(runId)
|
|
114
|
+
]);
|
|
115
|
+
return {
|
|
116
|
+
run,
|
|
117
|
+
timeline: timeline2,
|
|
118
|
+
children,
|
|
119
|
+
...links
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
if ((0, import_nestjs_durable_core.aggregatesCheckpoints)(this.store)) {
|
|
123
|
+
const stats = await this.store.checkpointStats(runId);
|
|
124
|
+
const etag = (0, import_nestjs_durable_core.extendEtag)((0, import_nestjs_durable_core.runDetailEtag)(run, stats, over), detailExtras(links));
|
|
125
|
+
if ((0, import_nestjs_durable_core.etagMatches)(options?.ifNoneMatch, etag)) {
|
|
126
|
+
return {
|
|
127
|
+
run,
|
|
128
|
+
timeline: [],
|
|
129
|
+
children: [],
|
|
130
|
+
notModified: true,
|
|
131
|
+
etag
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
return {
|
|
135
|
+
run,
|
|
136
|
+
...await (0, import_nestjs_durable_core.summarizeRunFromStore)(this.store, runId, over, stats),
|
|
137
|
+
etag,
|
|
138
|
+
...links
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
const full = await this.store.listCheckpoints(runId);
|
|
142
|
+
const { timeline, fans } = (0, import_nestjs_durable_core.summarizeTimeline)(full, over);
|
|
143
|
+
const ids = (0, import_nestjs_durable_core.childIdsOf)(full);
|
|
144
|
+
return {
|
|
145
|
+
run,
|
|
146
|
+
timeline,
|
|
147
|
+
children: ids.slice(0, import_nestjs_durable_core.SUMMARY_CHILD_LIMIT),
|
|
148
|
+
fans,
|
|
149
|
+
childCount: ids.length,
|
|
150
|
+
...links
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* What a run detail reads beyond the run row and its checkpoints: what this run's settling
|
|
155
|
+
* started, the subscribers its lifecycle event goes to (of its namespace) and where it stands for
|
|
156
|
+
* each, what started it, its workflow's lifecycle links, and the `ctx.waitForRun` waits on either
|
|
157
|
+
* side of it with the other run's status. A few reads per detail: the run's own waiters while it
|
|
158
|
+
* is suspended, the waiters on it (one prefix read), and one batched read of every linked run.
|
|
159
|
+
*/
|
|
160
|
+
async lifecycleLinks(run) {
|
|
161
|
+
const triggered = await this.engine.triggeredRuns(run);
|
|
162
|
+
const source = (0, import_nestjs_durable_core.lifecycleSourceOf)(run);
|
|
163
|
+
const kind = (0, import_nestjs_durable_core.lifecycleKindOf)(run.status);
|
|
164
|
+
const subscribers = kind ? await this.engine.lifecycleSubscribers(run.workflow, kind, run.namespace || "default") : [];
|
|
165
|
+
const workflowLifecycle = await this.engine.workflowLifecycle(run.workflow);
|
|
166
|
+
const linked = workflowLifecycle.startsOnSettle.length > 0 || workflowLifecycle.startedBy.length > 0;
|
|
167
|
+
const deliveries = kind ? (0, import_nestjs_durable_core.lifecycleDeliveriesOf)(run, {
|
|
168
|
+
subscribers,
|
|
169
|
+
live: new Set(subscribers),
|
|
170
|
+
coalesced: this.engine.coalescedSubscribers(run)
|
|
171
|
+
}) : [];
|
|
172
|
+
const [own, onIt] = await Promise.all([
|
|
173
|
+
run.status === "suspended" ? (0, import_nestjs_durable_core.listSignalWaitersOfRuns)(this.store, [
|
|
174
|
+
run.id
|
|
175
|
+
]) : [],
|
|
176
|
+
run.workflow.startsWith("__") ? [] : this.store.listSignalWaiters((0, import_nestjs_durable_core.runWaitPrefix)(run.id))
|
|
177
|
+
]);
|
|
178
|
+
const waitsFor = own.flatMap((w) => {
|
|
179
|
+
const target = (0, import_nestjs_durable_core.runWaitTargetOf)(w);
|
|
180
|
+
return target !== void 0 ? [
|
|
181
|
+
{
|
|
182
|
+
runId: target,
|
|
183
|
+
seq: w.seq
|
|
184
|
+
}
|
|
185
|
+
] : [];
|
|
186
|
+
});
|
|
187
|
+
const awaitedBy = onIt.filter((w) => (0, import_nestjs_durable_core.runWaitTargetOf)(w) === run.id).map((w) => ({
|
|
188
|
+
runId: w.runId,
|
|
189
|
+
seq: w.seq
|
|
190
|
+
}));
|
|
191
|
+
const started = deliveries.flatMap((d) => d.state === "started" && d.runId !== void 0 ? [
|
|
192
|
+
d.runId
|
|
193
|
+
] : []);
|
|
194
|
+
const { runs } = await (0, import_nestjs_durable_core.readRunsByIds)(this.store, [
|
|
195
|
+
...source ? [
|
|
196
|
+
source.runId
|
|
197
|
+
] : [],
|
|
198
|
+
...started,
|
|
199
|
+
...waitsFor.map((l) => l.runId),
|
|
200
|
+
...awaitedBy.map((l) => l.runId)
|
|
201
|
+
]);
|
|
202
|
+
const withRun = /* @__PURE__ */ __name((link) => {
|
|
203
|
+
const other = link.runId !== void 0 ? runs.get(link.runId) : void 0;
|
|
204
|
+
return other ? {
|
|
205
|
+
...link,
|
|
206
|
+
status: other.status,
|
|
207
|
+
namespace: other.namespace
|
|
208
|
+
} : link;
|
|
209
|
+
}, "withRun");
|
|
210
|
+
const sourceRun = source ? runs.get(source.runId) : void 0;
|
|
211
|
+
const traceId = (0, import_nestjs_durable_core.traceIdOfTraceparent)((0, import_nestjs_durable_core.lifecycleTraceparentOf)(sourceRun?.tags));
|
|
212
|
+
return {
|
|
213
|
+
...triggered.length > 0 ? {
|
|
214
|
+
triggered
|
|
215
|
+
} : {},
|
|
216
|
+
...source ? {
|
|
217
|
+
triggeredBy: {
|
|
218
|
+
...source,
|
|
219
|
+
...sourceRun ? {
|
|
220
|
+
status: sourceRun.status,
|
|
221
|
+
namespace: sourceRun.namespace
|
|
222
|
+
} : {},
|
|
223
|
+
...traceId ? {
|
|
224
|
+
traceId
|
|
225
|
+
} : {}
|
|
226
|
+
}
|
|
227
|
+
} : {},
|
|
228
|
+
...subscribers.length > 0 ? {
|
|
229
|
+
lifecycleSubscribers: subscribers
|
|
230
|
+
} : {},
|
|
231
|
+
...linked ? {
|
|
232
|
+
workflowLifecycle
|
|
233
|
+
} : {},
|
|
234
|
+
...deliveries.length > 0 ? {
|
|
235
|
+
lifecycleDeliveries: deliveries.map((d) => d.state === "started" ? withRun(d) : d)
|
|
236
|
+
} : {},
|
|
237
|
+
...waitsFor.length > 0 ? {
|
|
238
|
+
waitsFor: waitsFor.map(withRun)
|
|
239
|
+
} : {},
|
|
240
|
+
...awaitedBy.length > 0 ? {
|
|
241
|
+
awaitedBy: awaitedBy.map(withRun)
|
|
242
|
+
} : {}
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
workflowLifecycle(workflow, opts) {
|
|
246
|
+
return this.engine.workflowLifecycle(workflow, opts);
|
|
247
|
+
}
|
|
248
|
+
lifecycleSubscribers(workflow, kind, namespace) {
|
|
249
|
+
return this.engine.lifecycleSubscribers(workflow, kind, namespace);
|
|
250
|
+
}
|
|
251
|
+
getRun(runId) {
|
|
252
|
+
return this.store.getRun(runId);
|
|
253
|
+
}
|
|
254
|
+
async getFanMembers(runId, group, query) {
|
|
255
|
+
if (!await this.store.getRun(runId)) return null;
|
|
256
|
+
if ((0, import_nestjs_durable_core.aggregatesCheckpoints)(this.store)) {
|
|
257
|
+
const stats = await this.store.checkpointStats(runId, {
|
|
258
|
+
group
|
|
259
|
+
});
|
|
260
|
+
const etag = (0, import_nestjs_durable_core.fanPageEtag)(group, stats, query);
|
|
261
|
+
if ((0, import_nestjs_durable_core.etagMatches)(query.ifNoneMatch, etag)) {
|
|
262
|
+
return {
|
|
263
|
+
items: [],
|
|
264
|
+
total: 0,
|
|
265
|
+
notModified: true,
|
|
266
|
+
etag
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
return {
|
|
270
|
+
...await (0, import_nestjs_durable_core.pageFanFromStore)(this.store, runId, group, query, stats),
|
|
271
|
+
etag
|
|
272
|
+
};
|
|
273
|
+
}
|
|
274
|
+
return (0, import_nestjs_durable_core.pageFan)(await this.store.listCheckpoints(runId), group, query);
|
|
275
|
+
}
|
|
276
|
+
async listRuns(query) {
|
|
277
|
+
const runs = await this.store.listRuns(withoutRegistry(query));
|
|
278
|
+
const waiterByRun = (0, import_nestjs_durable_core.indexWaitersByRun)(await (0, import_nestjs_durable_core.listSignalWaitersOfRuns)(this.store, runs.filter((run) => run.status === "suspended").map((run) => run.id)));
|
|
279
|
+
return runs.map((run) => {
|
|
280
|
+
const waiting = (0, import_nestjs_durable_core.resolveRunWaiting)(run, waiterByRun);
|
|
281
|
+
return waiting ? {
|
|
282
|
+
...run,
|
|
283
|
+
waiting
|
|
284
|
+
} : run;
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* The `(status, origin)` counts behind a console's chips, straight off the store's aggregate. The
|
|
289
|
+
* console needs this precisely BECAUSE its list is paginated: the page bounds what is rendered,
|
|
290
|
+
* these counts stay whole-set exact. A store with no aggregate falls back to counting a full
|
|
291
|
+
* listing — correct, but the unbounded read this exists to avoid, so it is the last resort.
|
|
292
|
+
*/
|
|
293
|
+
async runFacets(query) {
|
|
294
|
+
const scoped = withoutRegistry(query);
|
|
295
|
+
if (this.store.runFacets) return this.store.runFacets(scoped);
|
|
296
|
+
const runs = await this.store.listRuns(scoped);
|
|
297
|
+
return (0, import_nestjs_durable_core.mergeRunFacetRows)(runs.map((r) => ({
|
|
298
|
+
status: r.status,
|
|
299
|
+
origin: r.origin,
|
|
300
|
+
count: 1
|
|
301
|
+
})));
|
|
302
|
+
}
|
|
303
|
+
/**
|
|
304
|
+
* The distinct values behind a console's pickers, straight off the store's enumeration. A store
|
|
305
|
+
* without one answers `[]` rather than falling back to a listing: unlike the facet counts above —
|
|
306
|
+
* where a full listing is at least the RIGHT answer expensively — a picker built from an unbounded
|
|
307
|
+
* scan would be the same unbounded read on every keystroke, for a control whose free-text entry
|
|
308
|
+
* still works without it.
|
|
309
|
+
*/
|
|
310
|
+
async runValueFacets(axis, query, opts) {
|
|
311
|
+
return this.store.runValueFacets?.(axis, withoutRegistry(query), opts) ?? [];
|
|
312
|
+
}
|
|
313
|
+
/**
|
|
314
|
+
* Bulk-resolve what each of `runIds` is currently parked on — for a consumer with its own filtered/
|
|
315
|
+
* paginated run listing (e.g. "which of MY suspended runs are stuck at a breakpoint") without
|
|
316
|
+
* re-deriving `listRuns`' waiter read or querying `durable_step_checkpoints` directly. Mirrors
|
|
317
|
+
* `listRuns`' waiting computation (the requested runs' signal waiters + `resolveRunWaiting`), but ALSO
|
|
318
|
+
* bulk-fetches the currently-suspended runs to check real status: `engine.cancel` (the non-compensate
|
|
319
|
+
* path) doesn't clear a run's signal waiter row, so a cancelled run can leave an ORPHANED waiter
|
|
320
|
+
* behind — trusting waiter presence alone would wrongly report a terminal run as still waiting.
|
|
321
|
+
* Two bulk reads total (never one query per requested id), same as `listRuns`.
|
|
322
|
+
*/
|
|
323
|
+
async waitingFor(runIds) {
|
|
324
|
+
if (runIds.length === 0) return {};
|
|
325
|
+
const idSet = new Set(runIds);
|
|
326
|
+
const [suspended, waiters] = await Promise.all([
|
|
327
|
+
this.store.listRuns({
|
|
328
|
+
statuses: [
|
|
329
|
+
"suspended"
|
|
330
|
+
]
|
|
331
|
+
}),
|
|
332
|
+
(0, import_nestjs_durable_core.listSignalWaitersOfRuns)(this.store, runIds)
|
|
333
|
+
]);
|
|
334
|
+
const waiterByRun = (0, import_nestjs_durable_core.indexWaitersByRun)(waiters);
|
|
335
|
+
const result = {};
|
|
336
|
+
for (const run of suspended) {
|
|
337
|
+
if (!idSet.has(run.id)) continue;
|
|
338
|
+
const waiting = (0, import_nestjs_durable_core.resolveRunWaiting)(run, waiterByRun);
|
|
339
|
+
if (waiting) result[run.id] = waiting;
|
|
340
|
+
}
|
|
341
|
+
return result;
|
|
342
|
+
}
|
|
343
|
+
/** Every group the engine knows about — unscoped. A tenant proxy's request is scoped by the
|
|
344
|
+
* `RunRequestResponder` (to the requester's `@<tenant>` groups); the operator's own UI sees all. */
|
|
345
|
+
workerHealth() {
|
|
346
|
+
return this.engine.workerHealth();
|
|
347
|
+
}
|
|
348
|
+
cancel(runId, opts) {
|
|
349
|
+
return this.engine.cancel(runId, opts);
|
|
350
|
+
}
|
|
351
|
+
/** Re-enqueue (dispatch model) instead of resuming inline — a worker picks the run up and replays it. */
|
|
352
|
+
retry(runId) {
|
|
353
|
+
return this.engine.requeue(runId);
|
|
354
|
+
}
|
|
355
|
+
continue(runId) {
|
|
356
|
+
return this.engine.continue(runId);
|
|
357
|
+
}
|
|
358
|
+
retryWithInput(runId, input) {
|
|
359
|
+
return this.engine.retryWithInput(runId, input);
|
|
360
|
+
}
|
|
361
|
+
redispatchPending(runId) {
|
|
362
|
+
return this.engine.redispatchPending(runId);
|
|
363
|
+
}
|
|
364
|
+
republishLifecycle(target) {
|
|
365
|
+
return this.engine.republishLifecycle(target);
|
|
366
|
+
}
|
|
367
|
+
subscribe(runId, onEvent) {
|
|
368
|
+
return this.engine.subscribe((event) => {
|
|
369
|
+
if (event.runId === runId) onEvent(event);
|
|
370
|
+
});
|
|
371
|
+
}
|
|
372
|
+
};
|
|
373
|
+
StoreRunGateway = _ts_decorate([
|
|
374
|
+
(0, import_common.Injectable)(),
|
|
375
|
+
_ts_param(0, (0, import_common.Inject)(import_nestjs_durable_core.STATE_STORE_CANONICAL)),
|
|
376
|
+
_ts_metadata("design:type", Function),
|
|
377
|
+
_ts_metadata("design:paramtypes", [
|
|
378
|
+
typeof StateStore === "undefined" ? Object : StateStore,
|
|
379
|
+
typeof import_nestjs_durable_core.WorkflowEngine === "undefined" ? Object : import_nestjs_durable_core.WorkflowEngine
|
|
380
|
+
])
|
|
381
|
+
], StoreRunGateway);
|
|
382
|
+
|
|
383
|
+
// src/migrate-from-nestjs-workflows/combined-run-gateway.ts
|
|
384
|
+
var import_nestjs_durable_core2 = require("@dudousxd/nestjs-durable-core");
|
|
385
|
+
var CombinedRunGateway = class extends import_nestjs_durable_core2.RunGateway {
|
|
386
|
+
static {
|
|
387
|
+
__name(this, "CombinedRunGateway");
|
|
388
|
+
}
|
|
389
|
+
primary;
|
|
390
|
+
legacy;
|
|
391
|
+
// Durable-only features (lifecycle events, fan paging): the primary's, when it has them.
|
|
392
|
+
getFanMembers;
|
|
393
|
+
republishLifecycle;
|
|
394
|
+
workflowLifecycle;
|
|
395
|
+
lifecycleSubscribers;
|
|
396
|
+
constructor(primary, legacy) {
|
|
397
|
+
super();
|
|
398
|
+
this.primary = primary;
|
|
399
|
+
this.legacy = legacy;
|
|
400
|
+
if (primary.getFanMembers) this.getFanMembers = primary.getFanMembers.bind(primary);
|
|
401
|
+
if (primary.republishLifecycle) this.republishLifecycle = primary.republishLifecycle.bind(primary);
|
|
402
|
+
if (primary.workflowLifecycle) this.workflowLifecycle = primary.workflowLifecycle.bind(primary);
|
|
403
|
+
if (primary.lifecycleSubscribers) {
|
|
404
|
+
this.lifecycleSubscribers = primary.lifecycleSubscribers.bind(primary);
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
topology() {
|
|
408
|
+
return {
|
|
409
|
+
...this.primary.topology(),
|
|
410
|
+
engine: "nestjs-durable + @nestjs/workflows"
|
|
411
|
+
};
|
|
412
|
+
}
|
|
413
|
+
/** The gateway that has `runId`, durable first; `undefined` when neither does. */
|
|
414
|
+
async owner(runId) {
|
|
415
|
+
for (const gateway of [
|
|
416
|
+
this.primary,
|
|
417
|
+
this.legacy
|
|
418
|
+
]) {
|
|
419
|
+
const run = gateway.getRun ? await gateway.getRun(runId) : (await gateway.getRunDetail(runId))?.run;
|
|
420
|
+
if (run) return gateway;
|
|
421
|
+
}
|
|
422
|
+
return void 0;
|
|
423
|
+
}
|
|
424
|
+
async getRun(runId) {
|
|
425
|
+
return await this.primary.getRun?.(runId) ?? await this.legacy.getRun?.(runId) ?? (await this.getRunDetail(runId))?.run ?? null;
|
|
426
|
+
}
|
|
427
|
+
async getRunDetail(runId, options) {
|
|
428
|
+
return await this.primary.getRunDetail(runId, options) ?? this.legacy.getRunDetail(runId, options);
|
|
429
|
+
}
|
|
430
|
+
/**
|
|
431
|
+
* Both lists, newest first, then the page. The legacy side holds only the rows its gateway can
|
|
432
|
+
* read: the official gateway reads at most `WORKFLOWS_LIST_CAP` (1000) matching instances per list,
|
|
433
|
+
* so legacy rows past that cap are missing here as they are from the legacy console, and deep pages
|
|
434
|
+
* show durable rows only.
|
|
435
|
+
*/
|
|
436
|
+
async listRuns(query) {
|
|
437
|
+
const offset = query.offset ?? 0;
|
|
438
|
+
const limit = query.limit ?? 100;
|
|
439
|
+
const window = {
|
|
440
|
+
...query,
|
|
441
|
+
offset: 0,
|
|
442
|
+
limit: offset + limit
|
|
443
|
+
};
|
|
444
|
+
const [a, b] = await Promise.all([
|
|
445
|
+
this.primary.listRuns(window),
|
|
446
|
+
this.legacy.listRuns(window)
|
|
447
|
+
]);
|
|
448
|
+
return [
|
|
449
|
+
...a,
|
|
450
|
+
...b
|
|
451
|
+
].sort((x, y) => y.createdAt.getTime() - x.createdAt.getTime() || y.id.localeCompare(x.id)).slice(offset, offset + limit);
|
|
452
|
+
}
|
|
453
|
+
async runFacets(query) {
|
|
454
|
+
const [a, b] = await Promise.all([
|
|
455
|
+
this.primary.runFacets(query),
|
|
456
|
+
this.legacy.runFacets(query)
|
|
457
|
+
]);
|
|
458
|
+
const cells = /* @__PURE__ */ new Map();
|
|
459
|
+
for (const row of [
|
|
460
|
+
...a,
|
|
461
|
+
...b
|
|
462
|
+
]) {
|
|
463
|
+
const key = `${row.status}\0${row.origin ?? ""}`;
|
|
464
|
+
const cell = cells.get(key);
|
|
465
|
+
if (cell) cell.count += row.count;
|
|
466
|
+
else cells.set(key, {
|
|
467
|
+
...row
|
|
468
|
+
});
|
|
469
|
+
}
|
|
470
|
+
return [
|
|
471
|
+
...cells.values()
|
|
472
|
+
];
|
|
473
|
+
}
|
|
474
|
+
async runValueFacets(axis, query, opts) {
|
|
475
|
+
const [a, b] = await Promise.all([
|
|
476
|
+
this.primary.runValueFacets(axis, query, opts),
|
|
477
|
+
this.legacy.runValueFacets(axis, query, opts)
|
|
478
|
+
]);
|
|
479
|
+
const counts = /* @__PURE__ */ new Map();
|
|
480
|
+
for (const row of [
|
|
481
|
+
...a,
|
|
482
|
+
...b
|
|
483
|
+
]) counts.set(row.value, (counts.get(row.value) ?? 0) + row.count);
|
|
484
|
+
return [
|
|
485
|
+
...counts
|
|
486
|
+
].map(([value, count]) => ({
|
|
487
|
+
value,
|
|
488
|
+
count
|
|
489
|
+
})).sort((x, y) => y.count - x.count);
|
|
490
|
+
}
|
|
491
|
+
async waitingFor(runIds) {
|
|
492
|
+
const [a, b] = await Promise.all([
|
|
493
|
+
this.primary.waitingFor(runIds),
|
|
494
|
+
this.legacy.waitingFor(runIds)
|
|
495
|
+
]);
|
|
496
|
+
return {
|
|
497
|
+
...b,
|
|
498
|
+
...a
|
|
499
|
+
};
|
|
500
|
+
}
|
|
501
|
+
workerHealth() {
|
|
502
|
+
return this.primary.workerHealth();
|
|
503
|
+
}
|
|
504
|
+
async cancel(runId, opts) {
|
|
505
|
+
return (await this.owner(runId))?.cancel(runId, opts) ?? null;
|
|
506
|
+
}
|
|
507
|
+
async retry(runId) {
|
|
508
|
+
return (await this.owner(runId))?.retry(runId) ?? null;
|
|
509
|
+
}
|
|
510
|
+
async continue(runId) {
|
|
511
|
+
return (await this.owner(runId))?.continue(runId) ?? null;
|
|
512
|
+
}
|
|
513
|
+
async redispatchPending(runId) {
|
|
514
|
+
return (await this.owner(runId))?.redispatchPending(runId) ?? null;
|
|
515
|
+
}
|
|
516
|
+
/** Replays on the engine that ran it (a legacy run's workflow may not exist on durable yet). */
|
|
517
|
+
async retryWithInput(runId, input) {
|
|
518
|
+
return (await this.owner(runId))?.retryWithInput(runId, input) ?? null;
|
|
519
|
+
}
|
|
520
|
+
subscribe(runId, onEvent) {
|
|
521
|
+
const offs = [
|
|
522
|
+
this.primary.subscribe(runId, onEvent),
|
|
523
|
+
this.legacy.subscribe(runId, onEvent)
|
|
524
|
+
];
|
|
525
|
+
return () => {
|
|
526
|
+
for (const off of offs) off();
|
|
527
|
+
};
|
|
528
|
+
}
|
|
529
|
+
};
|
|
530
|
+
|
|
531
|
+
// src/migrate-from-nestjs-workflows/index.ts
|
|
532
|
+
function _ts_decorate2(decorators, target, key, desc) {
|
|
533
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
534
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") {
|
|
535
|
+
r = Reflect.decorate(decorators, target, key, desc);
|
|
536
|
+
} else {
|
|
537
|
+
for (var i = decorators.length - 1; i >= 0; i--) {
|
|
538
|
+
if (d = decorators[i]) {
|
|
539
|
+
r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
544
|
+
}
|
|
545
|
+
__name(_ts_decorate2, "_ts_decorate");
|
|
546
|
+
function _ts_metadata2(metadataKey, metadataValue) {
|
|
547
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") {
|
|
548
|
+
return Reflect.metadata(metadataKey, metadataValue);
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
__name(_ts_metadata2, "_ts_metadata");
|
|
552
|
+
function _ts_param2(paramIndex, decorator) {
|
|
553
|
+
return function(target, key) {
|
|
554
|
+
decorator(target, key, paramIndex);
|
|
555
|
+
};
|
|
556
|
+
}
|
|
557
|
+
__name(_ts_param2, "_ts_param");
|
|
558
|
+
var UNFINISHED = [
|
|
559
|
+
"pending",
|
|
560
|
+
"running",
|
|
561
|
+
"suspended",
|
|
562
|
+
"compensating"
|
|
563
|
+
];
|
|
564
|
+
var TERMINAL = [
|
|
565
|
+
"completed",
|
|
566
|
+
"failed",
|
|
567
|
+
"cancelled",
|
|
568
|
+
"dead"
|
|
569
|
+
];
|
|
570
|
+
var MigrationClient = class {
|
|
571
|
+
static {
|
|
572
|
+
__name(this, "MigrationClient");
|
|
573
|
+
}
|
|
574
|
+
engine;
|
|
575
|
+
store;
|
|
576
|
+
official;
|
|
577
|
+
constructor(engine, store, official) {
|
|
578
|
+
this.engine = engine;
|
|
579
|
+
this.store = store;
|
|
580
|
+
this.official = official;
|
|
581
|
+
}
|
|
582
|
+
start(workflow, input, runId = globalThis.crypto.randomUUID(), opts) {
|
|
583
|
+
return this.engine.start((0, import_nestjs_durable_core3.workflowName)(workflow), input, runId, opts);
|
|
584
|
+
}
|
|
585
|
+
/** The engine that owns `runId`, or `null` when neither has it. */
|
|
586
|
+
async owner(runId) {
|
|
587
|
+
if (await this.store.getRun(runId)) return "nestjs-durable";
|
|
588
|
+
if (await this.official.getStatus(runId)) return "@nestjs/workflows";
|
|
589
|
+
return null;
|
|
590
|
+
}
|
|
591
|
+
/**
|
|
592
|
+
* Signal the instance `runId` waits on: durable gets the token `<signal>:<key>` (deduplicated by
|
|
593
|
+
* `id` per token), the official engine `signal(signal, payload, { key, id })`. Resolves the owner, or `null` for an unknown id
|
|
594
|
+
* (nothing is sent: a signal for nobody would be buffered for a later wait).
|
|
595
|
+
*/
|
|
596
|
+
async signal(runId, signal, payload, opts = {}) {
|
|
597
|
+
const owner = await this.owner(runId);
|
|
598
|
+
if (owner === "nestjs-durable") {
|
|
599
|
+
await this.engine.signal(opts.key ? `${signal}:${opts.key}` : signal, payload, opts.id !== void 0 ? {
|
|
600
|
+
id: opts.id
|
|
601
|
+
} : void 0);
|
|
602
|
+
} else if (owner === "@nestjs/workflows") {
|
|
603
|
+
await this.official.signal(signal, payload, {
|
|
604
|
+
...opts.key !== void 0 ? {
|
|
605
|
+
key: opts.key
|
|
606
|
+
} : {},
|
|
607
|
+
...opts.id !== void 0 ? {
|
|
608
|
+
id: opts.id
|
|
609
|
+
} : {}
|
|
610
|
+
});
|
|
611
|
+
}
|
|
612
|
+
return owner;
|
|
613
|
+
}
|
|
614
|
+
/**
|
|
615
|
+
* Cancel `runId` where it runs. `compensate: true` runs the saga undo (official `cancel`);
|
|
616
|
+
* otherwise it stops without compensating (official `terminate`).
|
|
617
|
+
*/
|
|
618
|
+
async cancel(runId, opts = {}) {
|
|
619
|
+
const owner = await this.owner(runId);
|
|
620
|
+
if (owner === "nestjs-durable") await this.engine.cancel(runId, opts);
|
|
621
|
+
else if (owner === "@nestjs/workflows") {
|
|
622
|
+
if (opts.compensate) await this.official.cancel(runId);
|
|
623
|
+
else await this.official.terminate(runId);
|
|
624
|
+
}
|
|
625
|
+
return owner ? this.getStatus(runId) : null;
|
|
626
|
+
}
|
|
627
|
+
async getStatus(runId) {
|
|
628
|
+
const run = await this.store.getRun(runId);
|
|
629
|
+
if (run) {
|
|
630
|
+
return {
|
|
631
|
+
engine: "nestjs-durable",
|
|
632
|
+
runId,
|
|
633
|
+
workflow: run.workflow,
|
|
634
|
+
status: run.status,
|
|
635
|
+
...run.output !== void 0 ? {
|
|
636
|
+
output: run.output
|
|
637
|
+
} : {},
|
|
638
|
+
...run.error ? {
|
|
639
|
+
error: run.error
|
|
640
|
+
} : {}
|
|
641
|
+
};
|
|
642
|
+
}
|
|
643
|
+
const instance = await this.official.getStatus(runId);
|
|
644
|
+
if (!instance) return null;
|
|
645
|
+
const mapped = (0, import_nestjs_durable_workflows_gateway.toWorkflowRun)(instance);
|
|
646
|
+
return {
|
|
647
|
+
engine: "@nestjs/workflows",
|
|
648
|
+
runId,
|
|
649
|
+
workflow: instance.workflow,
|
|
650
|
+
status: mapped.status,
|
|
651
|
+
...mapped.output !== void 0 ? {
|
|
652
|
+
output: mapped.output
|
|
653
|
+
} : {},
|
|
654
|
+
...mapped.error ? {
|
|
655
|
+
error: mapped.error
|
|
656
|
+
} : {}
|
|
657
|
+
};
|
|
658
|
+
}
|
|
659
|
+
/**
|
|
660
|
+
* Wait for `runId` to finish where it runs and resolve its status (`completed` with `output`, or
|
|
661
|
+
* `failed` / `cancelled` / `dead` with `error`). Never throws for a failed instance.
|
|
662
|
+
*/
|
|
663
|
+
async result(runId, opts = {}) {
|
|
664
|
+
const owner = await this.owner(runId);
|
|
665
|
+
if (owner === "nestjs-durable") {
|
|
666
|
+
await this.engine.waitForRun(runId, {
|
|
667
|
+
until: "terminal",
|
|
668
|
+
...opts.timeoutMs !== void 0 ? {
|
|
669
|
+
timeoutMs: opts.timeoutMs
|
|
670
|
+
} : {}
|
|
671
|
+
});
|
|
672
|
+
} else if (owner === "@nestjs/workflows") {
|
|
673
|
+
await this.official.result(runId, opts.timeoutMs !== void 0 ? {
|
|
674
|
+
timeout: opts.timeoutMs
|
|
675
|
+
} : {}).catch((error) => {
|
|
676
|
+
if (error?.name !== "WorkflowFailedError") throw error;
|
|
677
|
+
});
|
|
678
|
+
} else {
|
|
679
|
+
throw new Error(`no instance ${runId} in nestjs-durable or @nestjs/workflows`);
|
|
680
|
+
}
|
|
681
|
+
const status = await this.getStatus(runId);
|
|
682
|
+
if (!status || !TERMINAL.includes(status.status)) {
|
|
683
|
+
throw new Error(`instance ${runId} did not finish`);
|
|
684
|
+
}
|
|
685
|
+
return status;
|
|
686
|
+
}
|
|
687
|
+
/**
|
|
688
|
+
* How many instances the official engine still has to finish. The cutover is over when it is 0:
|
|
689
|
+
* nothing starts there any more, so it only goes down.
|
|
690
|
+
*/
|
|
691
|
+
async remainingOnOfficial() {
|
|
692
|
+
return (await this.official.list({
|
|
693
|
+
status: UNFINISHED,
|
|
694
|
+
limit: 1e4
|
|
695
|
+
})).length;
|
|
696
|
+
}
|
|
697
|
+
};
|
|
698
|
+
MigrationClient = _ts_decorate2([
|
|
699
|
+
(0, import_common2.Injectable)(),
|
|
700
|
+
_ts_param2(1, (0, import_common2.Inject)(import_nestjs_durable_core3.STATE_STORE_CANONICAL)),
|
|
701
|
+
_ts_metadata2("design:type", Function),
|
|
702
|
+
_ts_metadata2("design:paramtypes", [
|
|
703
|
+
typeof import_nestjs_durable_core3.WorkflowEngine === "undefined" ? Object : import_nestjs_durable_core3.WorkflowEngine,
|
|
704
|
+
typeof StateStore === "undefined" ? Object : StateStore,
|
|
705
|
+
typeof import_workflows.WorkflowClient === "undefined" ? Object : import_workflows.WorkflowClient
|
|
706
|
+
])
|
|
707
|
+
], MigrationClient);
|
|
708
|
+
function combinedGateway(store, engine, client, events) {
|
|
709
|
+
return new CombinedRunGateway(new StoreRunGateway(store, engine), new import_nestjs_durable_workflows_gateway.WorkflowsRunGateway(client, events));
|
|
710
|
+
}
|
|
711
|
+
__name(combinedGateway, "combinedGateway");
|
|
712
|
+
var DurableMigrationModule = class _DurableMigrationModule {
|
|
713
|
+
static {
|
|
714
|
+
__name(this, "DurableMigrationModule");
|
|
715
|
+
}
|
|
716
|
+
static forRoot() {
|
|
717
|
+
return {
|
|
718
|
+
module: _DurableMigrationModule,
|
|
719
|
+
global: true,
|
|
720
|
+
providers: [
|
|
721
|
+
MigrationClient
|
|
722
|
+
],
|
|
723
|
+
exports: [
|
|
724
|
+
MigrationClient
|
|
725
|
+
]
|
|
726
|
+
};
|
|
727
|
+
}
|
|
728
|
+
static console() {
|
|
729
|
+
return {
|
|
730
|
+
module: DurableMigrationConsoleModule,
|
|
731
|
+
providers: [
|
|
732
|
+
{
|
|
733
|
+
provide: import_nestjs_durable_core3.RunGateway,
|
|
734
|
+
inject: [
|
|
735
|
+
import_nestjs_durable_core3.STATE_STORE_CANONICAL,
|
|
736
|
+
import_nestjs_durable_core3.WorkflowEngine,
|
|
737
|
+
import_workflows.WorkflowClient,
|
|
738
|
+
import_workflows.WorkflowEvents
|
|
739
|
+
],
|
|
740
|
+
useFactory: combinedGateway
|
|
741
|
+
}
|
|
742
|
+
],
|
|
743
|
+
exports: [
|
|
744
|
+
import_nestjs_durable_core3.RunGateway
|
|
745
|
+
]
|
|
746
|
+
};
|
|
747
|
+
}
|
|
748
|
+
};
|
|
749
|
+
DurableMigrationModule = _ts_decorate2([
|
|
750
|
+
(0, import_common2.Module)({})
|
|
751
|
+
], DurableMigrationModule);
|
|
752
|
+
var DurableMigrationConsoleModule = class DurableMigrationConsoleModule2 {
|
|
753
|
+
static {
|
|
754
|
+
__name(this, "DurableMigrationConsoleModule");
|
|
755
|
+
}
|
|
756
|
+
};
|
|
757
|
+
DurableMigrationConsoleModule = _ts_decorate2([
|
|
758
|
+
(0, import_common2.Module)({})
|
|
759
|
+
], DurableMigrationConsoleModule);
|
|
760
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
761
|
+
0 && (module.exports = {
|
|
762
|
+
CombinedRunGateway,
|
|
763
|
+
DurableMigrationModule,
|
|
764
|
+
MigrationClient
|
|
765
|
+
});
|
|
766
|
+
//# sourceMappingURL=migrate-from-nestjs-workflows.cjs.map
|