@nagi-js/core 0.1.1-rc.25 → 0.1.1-rc.27
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/{chunk-TXWUVL6W.js → chunk-7SKD73QT.js} +93 -13
- package/dist/chunk-7SKD73QT.js.map +1 -0
- package/dist/index.d.ts +32 -3
- package/dist/index.js +296 -19
- package/dist/index.js.map +1 -1
- package/dist/testing.d.ts +1 -1
- package/dist/testing.js +104 -8
- package/dist/testing.js.map +1 -1
- package/dist/{types-B5LHEF3v.d.ts → types-D2xR9diE.d.ts} +7 -0
- package/package.json +1 -1
- package/dist/chunk-TXWUVL6W.js.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { InMemoryClock, makeEmit, registerFlows, fingerprintFlows, makeFlowOf, makeSignals, compact, validationError, NagiRuntimeError, NagiSnapshotDriftError, requireCurrent, serializeError, NagiSignalTimeoutError, validate, Facts, foldRun, stepStateOf, dequeueBackoff, defaultSnapshotGonePolicy, DEFAULT_REAPER_INTERVAL_MS, isTerminalRun, runStatusOf, runCancelCause, isStepTerminal, stepStatusOf, asStepMapWithDefs, getDef, handlerDef, resolveRetry, isAbortRequested, needsStepIds, resolveNeeds, stepBackoff, NagiCanceledError, NagiStepTimeoutError, peekDef, attachDef, NagiNonRetryableError, pinRun, normalizeNeeds } from './chunk-
|
|
2
|
-
export { DEFAULT_REAPER_INTERVAL_MS, DEFAULT_SWEEP_LIMIT, InMemoryClock, InMemoryQueue, InMemoryRunEventHub, InMemoryStore, InMemoryStreamHub, NagiCanceledError, NagiConcurrencyConflictError, NagiFlowSnapshotGoneError, NagiNonRetryableError, NagiRuntimeError, NagiSignalTimeoutError, NagiSnapshotDriftError, NagiStepTimeoutError, NagiValidationError, STREAM_REPLAY_BUFFER_CAP, STREAM_SUBSCRIBER_BUFFER_CAP, admitsRunEnd, attemptOf, decideExpiredLeaseAction, decideSignal, decideTimeout, defaultSnapshotGonePolicy, errorOf, factConsequences, isRunEnd, isStepTerminal, isTerminalRun, isTerminalRunEvent, outputOf, foldRun as projectRunState, resolvedOf, runStatusOf, selectExpired, selectPruneBatch, stepStateOf, stepStatusOf, streamEndOf, supersede, unwrap } from './chunk-
|
|
1
|
+
import { InMemoryClock, makeEmit, registerFlows, fingerprintFlows, makeFlowOf, makeSignals, compact, validationError, NagiRuntimeError, NagiSnapshotDriftError, requireCurrent, serializeError, NagiSignalTimeoutError, validate, Facts, foldRun, stepStateOf, dequeueBackoff, defaultSnapshotGonePolicy, DEFAULT_REAPER_INTERVAL_MS, MAX_DELIVERIES, NagiDeliveryExhaustedError, isTerminalRun, runStatusOf, runCancelCause, NagiRunStrandedError, isStepTerminal, stepStatusOf, asStepMapWithDefs, getDef, handlerDef, resolveRetry, isAbortRequested, needsStepIds, resolveNeeds, stepBackoff, NagiCanceledError, NagiStepTimeoutError, peekDef, attachDef, NagiNonRetryableError, pinRun, normalizeNeeds } from './chunk-7SKD73QT.js';
|
|
2
|
+
export { DEFAULT_REAPER_INTERVAL_MS, DEFAULT_SWEEP_LIMIT, InMemoryClock, InMemoryQueue, InMemoryRunEventHub, InMemoryStore, InMemoryStreamHub, NagiCanceledError, NagiConcurrencyConflictError, NagiDeliveryExhaustedError, NagiFlowSnapshotGoneError, NagiNonRetryableError, NagiRunStrandedError, NagiRuntimeError, NagiSignalTimeoutError, NagiSnapshotDriftError, NagiStepTimeoutError, NagiValidationError, STREAM_REPLAY_BUFFER_CAP, STREAM_SUBSCRIBER_BUFFER_CAP, admitsRunEnd, attemptOf, decideExpiredLeaseAction, decideSignal, decideTimeout, defaultSnapshotGonePolicy, errorOf, factConsequences, isRunEnd, isStepTerminal, isTerminalRun, isTerminalRunEvent, outputOf, foldRun as projectRunState, resolvedOf, runStatusOf, selectExpired, selectPruneBatch, stepStateOf, stepStatusOf, streamEndOf, supersede, unwrap } from './chunk-7SKD73QT.js';
|
|
3
3
|
|
|
4
4
|
// src/builder.ts
|
|
5
5
|
var MAX_TIMER_MS = 2147483647;
|
|
@@ -315,13 +315,9 @@ function startCancelWatcher(args) {
|
|
|
315
315
|
const { store, runId, stepId, attempt, ac, intervalMs } = args;
|
|
316
316
|
return every(intervalMs, async () => {
|
|
317
317
|
try {
|
|
318
|
-
const
|
|
319
|
-
if (
|
|
320
|
-
if (!ac.signal.aborted) ac.abort(new NagiAbortError(runId,
|
|
321
|
-
return "stop";
|
|
322
|
-
}
|
|
323
|
-
if (isAbortRequested(stepStateOf(s, stepId), attempt)) {
|
|
324
|
-
if (!ac.signal.aborted) ac.abort(new NagiAbortError(runId, "step"));
|
|
318
|
+
const cause = await probe(store, runId, stepId, attempt);
|
|
319
|
+
if (cause !== null) {
|
|
320
|
+
if (!ac.signal.aborted) ac.abort(new NagiAbortError(runId, cause));
|
|
325
321
|
return "stop";
|
|
326
322
|
}
|
|
327
323
|
} catch {
|
|
@@ -329,6 +325,13 @@ function startCancelWatcher(args) {
|
|
|
329
325
|
return void 0;
|
|
330
326
|
});
|
|
331
327
|
}
|
|
328
|
+
async function probe(store, runId, stepId, attempt) {
|
|
329
|
+
if (store.probeAbort !== void 0)
|
|
330
|
+
return store.probeAbort(runId, stepId, attempt);
|
|
331
|
+
const s = await store.loadRunState(runId);
|
|
332
|
+
if (isTerminalRun(s)) return "run";
|
|
333
|
+
return isAbortRequested(stepStateOf(s, stepId), attempt) ? "step" : null;
|
|
334
|
+
}
|
|
332
335
|
function startDeadline(args) {
|
|
333
336
|
const { runId, stepId, attempt, timeoutMs, ac } = args;
|
|
334
337
|
const timer = setTimeout(() => {
|
|
@@ -1359,7 +1362,109 @@ function makeDispatcher(deps) {
|
|
|
1359
1362
|
}
|
|
1360
1363
|
return timedOut.length;
|
|
1361
1364
|
}
|
|
1365
|
+
async function redrive(flow2, state) {
|
|
1366
|
+
const { queue } = deps;
|
|
1367
|
+
const defs = asStepMapWithDefs(flow2.steps);
|
|
1368
|
+
let deliberateWait = false;
|
|
1369
|
+
for (const [stepId, step] of Object.entries(state.steps)) {
|
|
1370
|
+
const def = defs[stepId] === void 0 ? void 0 : getDef(defs[stepId]);
|
|
1371
|
+
switch (step.tag) {
|
|
1372
|
+
case "awaitingSignal":
|
|
1373
|
+
if (def?.kind === "signal" && def.timeoutMs === "unbounded") {
|
|
1374
|
+
deliberateWait = true;
|
|
1375
|
+
break;
|
|
1376
|
+
}
|
|
1377
|
+
await queue.enqueue(state.runId, stepId, {
|
|
1378
|
+
attempt: step.attempt,
|
|
1379
|
+
flowId: flow2.id
|
|
1380
|
+
});
|
|
1381
|
+
break;
|
|
1382
|
+
case "running":
|
|
1383
|
+
case "awaitingChild":
|
|
1384
|
+
case "aborting":
|
|
1385
|
+
await queue.enqueue(state.runId, stepId, {
|
|
1386
|
+
attempt: step.attempt,
|
|
1387
|
+
flowId: flow2.id
|
|
1388
|
+
});
|
|
1389
|
+
break;
|
|
1390
|
+
case "backoff":
|
|
1391
|
+
await queue.enqueue(state.runId, stepId, {
|
|
1392
|
+
attempt: step.failedAttempt + 1,
|
|
1393
|
+
delayMs: Math.max(0, step.retryAt.getTime() - Date.now()),
|
|
1394
|
+
flowId: flow2.id
|
|
1395
|
+
});
|
|
1396
|
+
break;
|
|
1397
|
+
}
|
|
1398
|
+
}
|
|
1399
|
+
await progression.advance(state.runId);
|
|
1400
|
+
return deliberateWait;
|
|
1401
|
+
}
|
|
1402
|
+
async function driveQuietRun(runId) {
|
|
1403
|
+
const { store, queue } = deps;
|
|
1404
|
+
const inspect = queue.inspect?.bind(queue);
|
|
1405
|
+
if (inspect === void 0) return false;
|
|
1406
|
+
if ((await inspect(runId)).length > 0) return false;
|
|
1407
|
+
const state = await store.loadRunState(runId);
|
|
1408
|
+
if (isTerminalRun(state)) return false;
|
|
1409
|
+
const resolution = await deps.flowOf(runId);
|
|
1410
|
+
switch (resolution.kind) {
|
|
1411
|
+
case "gone-terminal":
|
|
1412
|
+
return false;
|
|
1413
|
+
case "gone-live":
|
|
1414
|
+
await progression.terminate(
|
|
1415
|
+
{ kind: "gone", runId, flowId: resolution.error.flowId },
|
|
1416
|
+
{ tag: "failed", error: serializeError(resolution.error) }
|
|
1417
|
+
);
|
|
1418
|
+
return true;
|
|
1419
|
+
}
|
|
1420
|
+
const deliberateWait = await redrive(resolution.flow, state);
|
|
1421
|
+
const after = await store.loadRunState(runId);
|
|
1422
|
+
if (isTerminalRun(after) || (await inspect(runId)).length > 0) return true;
|
|
1423
|
+
if (deliberateWait) return false;
|
|
1424
|
+
const open = Object.entries(after.steps).filter(([, s]) => !["completed", "skipped", "failed"].includes(s.tag)).map(([id]) => id);
|
|
1425
|
+
await progression.terminate(
|
|
1426
|
+
{ kind: "resolved", runId, flow: resolution.flow },
|
|
1427
|
+
{
|
|
1428
|
+
tag: "failed",
|
|
1429
|
+
error: serializeError(
|
|
1430
|
+
new NagiRunStrandedError({
|
|
1431
|
+
runId,
|
|
1432
|
+
flowId: resolution.flow.id,
|
|
1433
|
+
stepIds: open,
|
|
1434
|
+
reason: "re-driving it enqueued nothing"
|
|
1435
|
+
})
|
|
1436
|
+
)
|
|
1437
|
+
}
|
|
1438
|
+
);
|
|
1439
|
+
return true;
|
|
1440
|
+
}
|
|
1441
|
+
async function sweepQuietRuns(now, quietMs) {
|
|
1442
|
+
const quiet = await deps.store.listQuietRuns({
|
|
1443
|
+
quietSince: new Date(now.getTime() - quietMs)
|
|
1444
|
+
});
|
|
1445
|
+
let moved = 0;
|
|
1446
|
+
for (const runId of quiet) {
|
|
1447
|
+
try {
|
|
1448
|
+
if (await driveQuietRun(runId)) {
|
|
1449
|
+
moved++;
|
|
1450
|
+
deps.emitLog({
|
|
1451
|
+
level: "warn",
|
|
1452
|
+
msg: "nagi: quiet run re-driven",
|
|
1453
|
+
attrs: { runId }
|
|
1454
|
+
});
|
|
1455
|
+
}
|
|
1456
|
+
} catch (err) {
|
|
1457
|
+
deps.emitLog({
|
|
1458
|
+
level: "error",
|
|
1459
|
+
msg: "nagi: quiet run could not be re-driven",
|
|
1460
|
+
attrs: { runId, error: String(err) }
|
|
1461
|
+
});
|
|
1462
|
+
}
|
|
1463
|
+
}
|
|
1464
|
+
return moved;
|
|
1465
|
+
}
|
|
1362
1466
|
return {
|
|
1467
|
+
sweepQuietRuns,
|
|
1363
1468
|
dispatchMessage: message.dispatchMessage,
|
|
1364
1469
|
advance: progression.advance,
|
|
1365
1470
|
terminate: progression.terminate,
|
|
@@ -1721,6 +1826,8 @@ var DEFAULT_CONCURRENCY = 4;
|
|
|
1721
1826
|
var DEFAULT_POLL_INTERVAL_MS = 1e3;
|
|
1722
1827
|
var DEFAULT_TIMER_SWEEP_INTERVAL_MS = 3e4;
|
|
1723
1828
|
var SNAPSHOT_GONE_FALLBACK_NACK_DELAY_MS = 3e4;
|
|
1829
|
+
var DEFAULT_QUIET_RUN_SWEEP_INTERVAL_MS = 6e4;
|
|
1830
|
+
var DEFAULT_QUIET_RUN_MS = 12e4;
|
|
1724
1831
|
function makeWorker(deps, config) {
|
|
1725
1832
|
return new WorkerImpl(deps, config);
|
|
1726
1833
|
}
|
|
@@ -1747,6 +1854,15 @@ var WorkerImpl = class {
|
|
|
1747
1854
|
intervalMs: config?.reaperIntervalMs ?? DEFAULT_REAPER_INTERVAL_MS,
|
|
1748
1855
|
run: (now) => deps.store.sweepLeases({ now, queue: deps.queue }),
|
|
1749
1856
|
lastAt: bootAt
|
|
1857
|
+
},
|
|
1858
|
+
{
|
|
1859
|
+
label: "quiet-run",
|
|
1860
|
+
intervalMs: config?.quietRunSweepIntervalMs ?? DEFAULT_QUIET_RUN_SWEEP_INTERVAL_MS,
|
|
1861
|
+
run: (now) => this.dispatcher.sweepQuietRuns(
|
|
1862
|
+
now,
|
|
1863
|
+
config?.quietRunMs ?? DEFAULT_QUIET_RUN_MS
|
|
1864
|
+
),
|
|
1865
|
+
lastAt: bootAt
|
|
1750
1866
|
}
|
|
1751
1867
|
];
|
|
1752
1868
|
this.sweeps = sweeps.filter((s) => s.intervalMs > 0);
|
|
@@ -1762,9 +1878,16 @@ var WorkerImpl = class {
|
|
|
1762
1878
|
snapshotGonePolicy;
|
|
1763
1879
|
maxPerFlow;
|
|
1764
1880
|
inFlightByFlow = /* @__PURE__ */ new Map();
|
|
1881
|
+
// Messages over their flow's cap, read once and kept invisible until a slot
|
|
1882
|
+
// of that flow frees. At most `concurrency` of them; past that the old
|
|
1883
|
+
// delayed-nack deferral applies, so one capped flow cannot stop the worker
|
|
1884
|
+
// from reading other flows' messages.
|
|
1885
|
+
held = [];
|
|
1886
|
+
wakePending = false;
|
|
1887
|
+
wakeAc;
|
|
1765
1888
|
// Per-flow blast-radius bound (maxConcurrencyPerFlow). A message over its
|
|
1766
|
-
// flow's cap is
|
|
1767
|
-
//
|
|
1889
|
+
// flow's cap is HELD (run(), up to `concurrency`) or DEFERRED — delayed
|
|
1890
|
+
// nack, redelivered after ~pollIntervalMs — never dropped. Messages without flowId (enqueued pre-upgrade) are exempt.
|
|
1768
1891
|
// Deferral does raise pgmq read_ct, which the snapshot-gone policy reads;
|
|
1769
1892
|
// that only matters if the same flow later goes snapshot-gone, where an
|
|
1770
1893
|
// earlier fail is acceptable.
|
|
@@ -1796,12 +1919,23 @@ var WorkerImpl = class {
|
|
|
1796
1919
|
// timeout killing the loop, then 37h of fleet-wide stuck runs because
|
|
1797
1920
|
// nothing restarts it. Every await below is therefore guarded.
|
|
1798
1921
|
async run() {
|
|
1922
|
+
const unsubscribe = this.deps.onEnqueued?.(() => this.wake());
|
|
1923
|
+
try {
|
|
1924
|
+
await this.loop();
|
|
1925
|
+
} finally {
|
|
1926
|
+
unsubscribe?.();
|
|
1927
|
+
}
|
|
1928
|
+
await this.drain();
|
|
1929
|
+
await this.returnHeld();
|
|
1930
|
+
}
|
|
1931
|
+
async loop() {
|
|
1799
1932
|
let dequeueFailures = 0;
|
|
1800
1933
|
while (!this.aborted()) {
|
|
1934
|
+
await this.extendHeld();
|
|
1801
1935
|
await this.runDueSweeps();
|
|
1802
1936
|
const slots = this.concurrency - this.inFlight.size;
|
|
1803
1937
|
if (slots <= 0) {
|
|
1804
|
-
await this.
|
|
1938
|
+
await this.idle(50);
|
|
1805
1939
|
continue;
|
|
1806
1940
|
}
|
|
1807
1941
|
let messages;
|
|
@@ -1823,15 +1957,76 @@ var WorkerImpl = class {
|
|
|
1823
1957
|
}
|
|
1824
1958
|
dequeueFailures = 0;
|
|
1825
1959
|
if (messages.length === 0) {
|
|
1826
|
-
await this.
|
|
1960
|
+
await this.idle(this.pollIntervalMs);
|
|
1827
1961
|
continue;
|
|
1828
1962
|
}
|
|
1829
1963
|
for (const msg of messages) {
|
|
1830
1964
|
if (this.admitFlow(msg)) this.fire(msg);
|
|
1965
|
+
else if (this.held.length < this.concurrency)
|
|
1966
|
+
this.held.push({ msg, extendedAt: this.deps.clock.now().getTime() });
|
|
1831
1967
|
else await this.defer(msg);
|
|
1832
1968
|
}
|
|
1833
1969
|
}
|
|
1834
|
-
|
|
1970
|
+
}
|
|
1971
|
+
// Called when this process enqueued work or a slot freed.
|
|
1972
|
+
wake() {
|
|
1973
|
+
this.wakePending = true;
|
|
1974
|
+
this.wakeAc?.abort();
|
|
1975
|
+
}
|
|
1976
|
+
// A poll sleep that a wake() cuts short, including one that arrived while
|
|
1977
|
+
// the loop was busy reading.
|
|
1978
|
+
async idle(ms) {
|
|
1979
|
+
if (this.wakePending) {
|
|
1980
|
+
this.wakePending = false;
|
|
1981
|
+
return;
|
|
1982
|
+
}
|
|
1983
|
+
const ac = new AbortController();
|
|
1984
|
+
this.wakeAc = ac;
|
|
1985
|
+
const signal = this.signal === void 0 ? ac.signal : AbortSignal.any([this.signal, ac.signal]);
|
|
1986
|
+
try {
|
|
1987
|
+
await this.deps.clock.sleep(ms, signal);
|
|
1988
|
+
} catch {
|
|
1989
|
+
}
|
|
1990
|
+
this.wakeAc = void 0;
|
|
1991
|
+
this.wakePending = false;
|
|
1992
|
+
}
|
|
1993
|
+
// Fire the oldest held message whose flow is under its cap again.
|
|
1994
|
+
releaseHeld() {
|
|
1995
|
+
if (this.aborted()) return;
|
|
1996
|
+
for (let i = 0; i < this.held.length; i++) {
|
|
1997
|
+
if (this.inFlight.size >= this.concurrency) return;
|
|
1998
|
+
const entry = this.held[i];
|
|
1999
|
+
if (entry === void 0 || !this.admitFlow(entry.msg)) continue;
|
|
2000
|
+
this.held.splice(i, 1);
|
|
2001
|
+
i--;
|
|
2002
|
+
this.fire(entry.msg);
|
|
2003
|
+
}
|
|
2004
|
+
}
|
|
2005
|
+
// A held message stays invisible on the same cadence as a running step's
|
|
2006
|
+
// heartbeat, so no peer reads it while it waits here.
|
|
2007
|
+
async extendHeld() {
|
|
2008
|
+
const now = this.deps.clock.now().getTime();
|
|
2009
|
+
for (const entry of this.held) {
|
|
2010
|
+
if (now - entry.extendedAt < this.deps.heartbeat.intervalMs) continue;
|
|
2011
|
+
entry.extendedAt = now;
|
|
2012
|
+
try {
|
|
2013
|
+
await this.deps.queue.extend(
|
|
2014
|
+
entry.msg.receipt,
|
|
2015
|
+
this.deps.heartbeat.leaseMs
|
|
2016
|
+
);
|
|
2017
|
+
} catch {
|
|
2018
|
+
}
|
|
2019
|
+
}
|
|
2020
|
+
}
|
|
2021
|
+
// On shutdown, hand held messages straight back: a peer reads them now
|
|
2022
|
+
// rather than after the visibility timeout.
|
|
2023
|
+
async returnHeld() {
|
|
2024
|
+
for (const { msg } of this.held.splice(0)) {
|
|
2025
|
+
try {
|
|
2026
|
+
await this.deps.queue.nack(msg.receipt, { delayMs: 0 });
|
|
2027
|
+
} catch {
|
|
2028
|
+
}
|
|
2029
|
+
}
|
|
1835
2030
|
}
|
|
1836
2031
|
async runOnce(opts) {
|
|
1837
2032
|
const limit = Math.max(1, opts?.maxSteps ?? this.concurrency);
|
|
@@ -1885,6 +2080,8 @@ var WorkerImpl = class {
|
|
|
1885
2080
|
const dispatch = this.dispatchSafely(msg).finally(() => {
|
|
1886
2081
|
release();
|
|
1887
2082
|
this.inFlight.delete(dispatch);
|
|
2083
|
+
this.releaseHeld();
|
|
2084
|
+
this.wake();
|
|
1888
2085
|
});
|
|
1889
2086
|
this.inFlight.add(dispatch);
|
|
1890
2087
|
}
|
|
@@ -1893,6 +2090,10 @@ var WorkerImpl = class {
|
|
|
1893
2090
|
try {
|
|
1894
2091
|
result = await this.dispatcher.dispatchMessage(msg);
|
|
1895
2092
|
} catch (err) {
|
|
2093
|
+
if (msg.readCount >= MAX_DELIVERIES) {
|
|
2094
|
+
await this.failExhausted(msg, err);
|
|
2095
|
+
return;
|
|
2096
|
+
}
|
|
1896
2097
|
const delayMs = this.dequeueBackoffMs(msg.readCount);
|
|
1897
2098
|
this.deps.emitLog({
|
|
1898
2099
|
level: "error",
|
|
@@ -1933,6 +2134,52 @@ var WorkerImpl = class {
|
|
|
1933
2134
|
return;
|
|
1934
2135
|
}
|
|
1935
2136
|
}
|
|
2137
|
+
// The last delivery threw too: end the run typed instead of looping. If even
|
|
2138
|
+
// that write fails (the database is what is down), the message is nacked
|
|
2139
|
+
// and the next delivery tries again — it is past the budget, so it fails
|
|
2140
|
+
// the run as soon as the store answers.
|
|
2141
|
+
async failExhausted(msg, cause) {
|
|
2142
|
+
const flowId = msg.flowId ?? "unknown";
|
|
2143
|
+
const error = new NagiDeliveryExhaustedError({
|
|
2144
|
+
runId: msg.runId,
|
|
2145
|
+
flowId,
|
|
2146
|
+
stepId: msg.stepId,
|
|
2147
|
+
attempt: msg.attempt,
|
|
2148
|
+
readCount: msg.readCount,
|
|
2149
|
+
cause
|
|
2150
|
+
});
|
|
2151
|
+
this.deps.emitLog({
|
|
2152
|
+
level: "error",
|
|
2153
|
+
msg: "worker.dispatch: delivery budget exhausted, failing run",
|
|
2154
|
+
attrs: {
|
|
2155
|
+
runId: msg.runId,
|
|
2156
|
+
flowId,
|
|
2157
|
+
stepId: msg.stepId,
|
|
2158
|
+
attempt: msg.attempt,
|
|
2159
|
+
readCount: msg.readCount,
|
|
2160
|
+
error: String(cause)
|
|
2161
|
+
}
|
|
2162
|
+
});
|
|
2163
|
+
try {
|
|
2164
|
+
await this.dispatcher.terminate(
|
|
2165
|
+
{ kind: "gone", runId: msg.runId, flowId },
|
|
2166
|
+
{ tag: "failed", error: serializeError(error) }
|
|
2167
|
+
);
|
|
2168
|
+
await this.deps.queue.ack(msg.receipt);
|
|
2169
|
+
} catch (writeErr) {
|
|
2170
|
+
this.deps.emitLog({
|
|
2171
|
+
level: "error",
|
|
2172
|
+
msg: "worker.dispatch: could not fail an exhausted run; will retry",
|
|
2173
|
+
attrs: { runId: msg.runId, error: String(writeErr) }
|
|
2174
|
+
});
|
|
2175
|
+
try {
|
|
2176
|
+
await this.deps.queue.nack(msg.receipt, {
|
|
2177
|
+
delayMs: this.dequeueBackoffMs(msg.readCount)
|
|
2178
|
+
});
|
|
2179
|
+
} catch {
|
|
2180
|
+
}
|
|
2181
|
+
}
|
|
2182
|
+
}
|
|
1936
2183
|
// A snapshot-gone message is retried (nack, delayed) only while the policy
|
|
1937
2184
|
// says a frozen-version worker might still claim it; past that budget the
|
|
1938
2185
|
// run is terminally failed with the snapshot-gone error and the message
|
|
@@ -2023,6 +2270,23 @@ async function nagiImpl(config) {
|
|
|
2023
2270
|
const clock = config.clock ?? new InMemoryClock();
|
|
2024
2271
|
const emitLog = makeEmit(config.onLog);
|
|
2025
2272
|
await config.queue.ensureSchema?.();
|
|
2273
|
+
const wakers = /* @__PURE__ */ new Set();
|
|
2274
|
+
const wakeWorkers = () => {
|
|
2275
|
+
for (const wake of wakers) wake();
|
|
2276
|
+
};
|
|
2277
|
+
const onEnqueued = (wake) => {
|
|
2278
|
+
wakers.add(wake);
|
|
2279
|
+
return () => {
|
|
2280
|
+
wakers.delete(wake);
|
|
2281
|
+
};
|
|
2282
|
+
};
|
|
2283
|
+
const queue = {
|
|
2284
|
+
...bindQueue(config.queue),
|
|
2285
|
+
async enqueue(runId, stepId, opts) {
|
|
2286
|
+
await config.queue.enqueue(runId, stepId, opts);
|
|
2287
|
+
wakeWorkers();
|
|
2288
|
+
}
|
|
2289
|
+
};
|
|
2026
2290
|
const streamTransport = config.store.stream;
|
|
2027
2291
|
const requireEvents = (caller) => {
|
|
2028
2292
|
const events = config.store.events;
|
|
@@ -2086,7 +2350,7 @@ async function nagiImpl(config) {
|
|
|
2086
2350
|
dispatcher,
|
|
2087
2351
|
hooks,
|
|
2088
2352
|
lifecycle
|
|
2089
|
-
} = makeEngine({ flowOf, queue
|
|
2353
|
+
} = makeEngine({ flowOf, queue, fireHooks: true });
|
|
2090
2354
|
const signals = makeSignals({
|
|
2091
2355
|
dispatcher,
|
|
2092
2356
|
store: config.store,
|
|
@@ -2101,14 +2365,14 @@ async function nagiImpl(config) {
|
|
|
2101
2365
|
});
|
|
2102
2366
|
const replayer = makeReplay({
|
|
2103
2367
|
store: config.store,
|
|
2104
|
-
queue
|
|
2368
|
+
queue,
|
|
2105
2369
|
clock,
|
|
2106
2370
|
registry,
|
|
2107
2371
|
driftPolicy,
|
|
2108
2372
|
flowOfUnder,
|
|
2109
2373
|
dispatcherFor: (scope) => makeEngine({
|
|
2110
2374
|
flowOf: pinRun(scope.run.runId, scope.run.flow, flowOf),
|
|
2111
|
-
...scope.kind === "enqueue" ? { queue
|
|
2375
|
+
...scope.kind === "enqueue" ? { queue, fireHooks: true } : { queue: scope.queue, fireHooks: false }
|
|
2112
2376
|
}).dispatcher
|
|
2113
2377
|
});
|
|
2114
2378
|
const wf = {
|
|
@@ -2124,6 +2388,7 @@ async function nagiImpl(config) {
|
|
|
2124
2388
|
});
|
|
2125
2389
|
if (staged.kind === "started")
|
|
2126
2390
|
await lifecycle.applyEffects(staged.effects);
|
|
2391
|
+
wakeWorkers();
|
|
2127
2392
|
return runId;
|
|
2128
2393
|
},
|
|
2129
2394
|
async startStaged(flow2, input, opts) {
|
|
@@ -2150,7 +2415,7 @@ async function nagiImpl(config) {
|
|
|
2150
2415
|
runId,
|
|
2151
2416
|
started: true,
|
|
2152
2417
|
canceled: staged.effects.superseded.map((e) => e.runId),
|
|
2153
|
-
applyOnCommit: () => applied ??= lifecycle.applyEffects(staged.effects)
|
|
2418
|
+
applyOnCommit: () => applied ??= lifecycle.applyEffects(staged.effects).then(wakeWorkers)
|
|
2154
2419
|
};
|
|
2155
2420
|
},
|
|
2156
2421
|
signal: signals.signal,
|
|
@@ -2167,7 +2432,7 @@ async function nagiImpl(config) {
|
|
|
2167
2432
|
);
|
|
2168
2433
|
}
|
|
2169
2434
|
return makeWorker(
|
|
2170
|
-
{ ...dispatchDeps, clock },
|
|
2435
|
+
{ ...dispatchDeps, clock, onEnqueued },
|
|
2171
2436
|
{
|
|
2172
2437
|
...workerConfig,
|
|
2173
2438
|
...compact({
|
|
@@ -2274,6 +2539,18 @@ var nagi = Object.assign(
|
|
|
2274
2539
|
);
|
|
2275
2540
|
async function noop() {
|
|
2276
2541
|
}
|
|
2542
|
+
function bindQueue(q) {
|
|
2543
|
+
return {
|
|
2544
|
+
enqueue: (...a) => q.enqueue(...a),
|
|
2545
|
+
dequeue: (...a) => q.dequeue(...a),
|
|
2546
|
+
ack: (...a) => q.ack(...a),
|
|
2547
|
+
nack: (...a) => q.nack(...a),
|
|
2548
|
+
extend: (...a) => q.extend(...a),
|
|
2549
|
+
...q.ensureSchema ? { ensureSchema: () => q.ensureSchema?.() ?? Promise.resolve() } : {},
|
|
2550
|
+
...q.inspect ? { inspect: (runId) => q.inspect?.(runId) ?? Promise.resolve([]) } : {},
|
|
2551
|
+
...q.withTx ? { withTx: (tx) => q.withTx?.(tx) ?? q } : {}
|
|
2552
|
+
};
|
|
2553
|
+
}
|
|
2277
2554
|
|
|
2278
2555
|
export { NagiAbortError, RunId, flow, nagi, optional };
|
|
2279
2556
|
//# sourceMappingURL=index.js.map
|