Health
Define when jobs are failing and chains are overdue, for the list and count filters, the dashboard and state metrics.
Failing and overdue are thresholds you set per job type with createHealthDefinitions, and a job type without its own threshold takes the healthDefault, which measures failing from the first failed attempt unless you set it. Core stores nothing for them and applies none: the list and count methods take them as filters, the dashboard labels and counts jobs and chains with them, and the State metrics snapshot exports them.
Caution
createHealthDefinitions and mergeHealthDefinitions are experimental.
Failing
A job is failing while its consecutiveFailures are at or above the minConsecutiveFailures of its job type. Error handling owns how consecutiveFailures counts: a failed or reclaimed attempt adds one, and any other outcome resets it.
Overdue
A running chain is overdue chainOverdueMs after its createdAt; chainOverdueMs is accepted only on entry types. The limit counts from creation, so it includes a deferred start, time blocked on other chains, time waiting for a worker and reschedules:
Set the limit to cover all of those phases, and leave it out for types whose start is deferred by varying amounts.
Define them per slice
Health definitions are typed against a slice's job types like createProcessors. Each slice defines its own, and mergeHealthDefinitions combines them for the consumers:
src/state-metrics.ts
const userHealth = createHealthDefinitions({
jobTypes: userJobTypes,
definitions: {
"send-welcome-email": { minConsecutiveFailures: 1, chainOverdueMs: 0 },
},
});
const healthDefinitions: HealthDefinitions = mergeHealthDefinitions([userHealth]);minConsecutiveFailures— a job is failing while itsconsecutiveFailuresare at or above itchainOverdueMs— a running chain is overdue this long after itscreatedAthealthDefault— the thresholds of every job type, in any slice, that its own definition leaves out; and the slices that set it must agree. A job type whose definition andhealthDefaultboth leaveminConsecutiveFailuresout is failing from its first failed attempt, so every job type is measured for failing
createHealthDefinitions throws a TypeError when a minConsecutiveFailures is not a positive integer or a chainOverdueMs is not a finite non-negative number, in a definition or the healthDefault. mergeHealthDefinitions throws a DuplicateJobTypeError when two slices define the same job type, and a TypeError when two slices set different healthDefaults. The example sets minConsecutiveFailures to 1 and chainOverdueMs to 0 so every signal shows at once.
The runnable code is in examples/guides/src/state-metrics.ts. Related: Error handling for consecutiveFailures, Job, chain and worker queries for the filters, State metrics for exporting them, and Feature slices for merging registries.