Stalled runs
Get paged about stranded runs instead of hearing about them from a customer. engine.onStalled (or the DurableModule onStalled option) plus stalledAfterMs turn "suspended forever" and "dispatched and lost" into an alert, once per stranded episode; listStalledRuns, findStalledRuns and durable inspect --stale are the same query on demand.
suspended is not an error. A run waiting on a slow worker, a long ctx.sleep and a run whose wake-up
was lost all look the same in the runs list, so nothing alerts anyone when a run is actually stuck.
The stalled-run pager watches for the two shapes a stuck run takes and reports them.
What counts as stranded
A running or suspended run is reported when it has been untouched (updatedAt) for at least
stalledAfterMs and one of these holds:
wakeForever: it issuspendedwith no wake timer at all, so nothing will ever re-drive it. This is thereconcileMs: 0hazard. With the defaultreconcileMsevery event-waiting suspension carries a fallback wake, so this shape means something cleared it.stalePending: its oldestpendingremote step was enqueued longer ago than the threshold and nothing is leasing it. A remote step with atimeoutMs, a heartbeat renewing it, orremoteRedispatchMscarries a lease (wakeAt) in the future and is not reported. A step with no lease, or with a lease that lapsed, most likely lost its dispatch.
Healthy long work is excluded: a long ctx.sleep has a future wakeAt and no pending step, and a
beating long step keeps a future lease.
Page on it
Pass onStalled to the module, or register it on the engine from any provider (exactly like
engine.onDead):
DurableModule.forRoot({
store,
transport,
stalledAfterMs: 30 * 60_000, // above your longest legitimate remote step; default 15 min
onStalled: ({ run, ageMs, wakeForever, stalePending }) => {
pager.alert(`durable run ${run.id} (${run.workflow}) stalled for ${Math.round(ageMs / 60_000)}min`, {
wakeForever,
stalePending, // { seq, name, attempts, ageMs, leaseUntil } when a lost remote step is the cause
});
},
});@Injectable()
export class StalledRunAlerts implements OnModuleInit {
constructor(private readonly engine: WorkflowEngine) {}
onModuleInit() {
this.engine.onStalled((info) => this.pager.alert(info));
}
}onStalled returns an unsubscribe function, and a listener that throws never breaks the sweep.
How the sweep runs
- In the timer poller. Every tick of a driving operator's timer
poller calls
engine.sweepStalled(). The engine throttles it to one pass a minute, skips it while draining, and does nothing when no listener is registered. Adrive: falseoperator or a thin worker never sweeps, so register the listener on a driving instance. If you drive the engine yourself (timerPollMs: 0), callengine.sweepStalled()from your scheduler. - Once per episode, per instance. A run already reported is skipped until it makes progress
(its
updatedAtchanges) and strands again, so a wedged run does not page you every minute. That memory is in-process: with several driving replicas each one reports the episode once, and a restart forgets what it reported. ImportLocksModulefrom@nestjs/lockswith a shared store and the sweep runs on one pod (see Fleet-wide sweeps on one pod), or deduplicate in the pager byrun.id+run.updatedAt. - Bounded and namespaced. A pass inspects up to 1000 in-flight runs (newest first, in pages of
200), reads the checkpoints of the idle ones, and stays in the engine's own
namespace. An operator with no namespace sweeps every namespace.
Set stalledAfterMs above your longest legitimate remote step. A slow but healthy step under the
threshold is exactly the case the engine refuses to guess about. Values under one minute are raised to
one minute.
List them on demand
The same query, without notifying anyone:
const stranded = await engine.listStalledRuns({ thresholdMs: 60 * 60_000 });findStalledRuns(store, { thresholdMs, namespace, scan, now }) from @dudousxd/nestjs-durable-core
runs it on a bare StateStore, for an ops script with no engine. From a terminal:
durable inspect --stale # default threshold, 15 minutes
durable inspect --stale=1h --namespace tenant-aSee the CLI.
Recovering a stranded run
- A lost remote step:
engine.redispatchPending(runId)re-enqueues its pending remote checkpoints now.remoteRedispatchMsmakes future ones heal on their own. - A wake-forever run: deliver the signal or event it waits on, or
engine.resume(runId)to replay it. - A run nobody needs any more: cancel it (
engine.cancel(runId)ordurable cancel <runId>).
Dead-letter queue
Cap crash-recovery with maxRecoveryAttempts so a poison-pill run moves to the terminal dead status instead of crash-looping forever, then route dead runs with engine.onDead or a DLQ workflow — an inline @DeadLetter() method, a per-workflow deadLetterWorkflow reference, or the module-level default — to alert, compensate, or queue for review.
Run retention & pruning
The module's retention option hard-prunes terminal run history (completed/failed/cancelled/dead) on an interval via StateStore.pruneTerminalRuns, per RetentionPolicy rules (maxAge and/or maxCount, optionally scoped to a namespace, tag, workflow set or search attribute) — without it, run history is kept forever by default and the durable tables grow unbounded.