Errors and retries

Retry a job that throws, with backoff, until an attempt succeeds, and recover the jobs of a worker that died.

When a job throws, nothing is lost: the worker puts it back to pending and runs it again after a delay, until an attempt succeeds.

sync-to-crmrunningattempt 1
throws
sync-to-crmpendingwaits for the backoff
sync-to-crmrunningattempt 2
succeeds
sync-to-crmcompleted

Each run of the handler is an attempt. An attempt that throws, or ends without committing applyJobOutcome, fails: the job returns to pending, its consecutiveFailures count goes up and the error is kept as lastAttemptError. The chain stays open, and a job that keeps throwing keeps being retried; there is no dead-letter state.

The delay before the next attempt is the backoff. It starts at initialDelayMs, grows by multiplier after every failure in a row, and stops growing at maxDelayMs. The defaults start at 10 seconds, which suits a real outage and is far too slow for a lesson. Set backoffConfig on a processor to choose its own delays; Retry and backoff has the formula.

A worker can also die mid-attempt, from a crash, a deploy or a lost network. While it runs, a worker renews a lease on its row in the database; once the lease runs out, 60 seconds by default, another worker reclaims its running jobs and puts them back to pending as failed attempts, so they run again after the backoff. If the old process wakes up later, its applyJobOutcome finds that the attempt no longer owns the job and throws, so its transaction rolls back and writes nothing. Worker lease has the details.

The sync-to-crm job below calls a CRM that is down for its first two attempts. With the default backoff the retries come too late for the lesson's three-second wait, so the chain never finishes. The output shows each attempt and the error that failed it.

Your task: give sync-to-crm a backoffConfig with an initialDelayMs of 20 and a maxDelayMs of 200, so its third attempt runs within moments.
  • Error handling: return domain errors as outputs, compensate with a follow-up job, and read lastAttemptError.
  • Timeouts: stop an attempt that runs too long so it fails and retries.
esc

Search guides, integrations and every exported symbol.

↑ ↓ to navigate↵ to open