skip to content

With Hangfire, how does BackgroundJob.ContinueJobWith chain two jobs, and what happens to the continuation when the parent job fails?

level: middleimportance: should knowfreq 35%

answer

  1. the continuation waits in Awaiting
  2. ContinueWith is [Obsolete]
  3. OnlyOnSucceededState by default
  4. Failed is not a final state
  5. Succeeded and Deleted end the wait

basics

~20 s

ContinueJobWith(parentId, ...) creates a job in the Awaiting state that is enqueued when the parent reaches Succeeded. A failed parent leaves it waiting, because Failed is not a final state; a deleted parent deletes it under the default options.

solid answer

~40 s

`BackgroundJob.ContinueJobWith(parentId, () => ...)`, or the same method on `IBackgroundJobClient`, creates a **continuation**: a job in the `Awaiting` state linked to the parent's id, returning its own id. Hangfire's continuation filter acts when the parent reaches a final state, Succeeded or Deleted. With the default `JobContinuationOptions.OnlyOnSucceededState` the continuation is enqueued when the parent succeeds and deleted ("Continuation condition was not met") when the parent is deleted. A parent that keeps throwing ends up `Failed` after its retries; that state is not final, so the continuation simply keeps waiting until someone requeues or deletes the parent. `OnAnyFinishedState` runs it after Succeeded or Deleted, and `OnlyOnDeletedState` only after Deleted. In the shop: build the nightly report, then e-mail it. The old name `ContinueWith` is obsolete in 1.8.

code

csharp · 14 lines
csharp
var reportDate = DateTime.UtcNow.Date.AddDays(-1);

// Parent: build the report
var buildId = jobs.Enqueue<ISalesReportService>(
    s => s.BuildAsync(reportDate, CancellationToken.None));

// Runs only after the build succeeds (OnlyOnSucceededState is the default)
jobs.ContinueJobWith<IReportMailer>(buildId,
    m => m.SendToManagersAsync(reportDate, CancellationToken.None));

// Runs after the build succeeds or is deleted, but not while it sits in Failed
jobs.ContinueJobWith<IReportFiles>(buildId,
    f => f.DeleteTempFilesAsync(reportDate),
    options: JobContinuationOptions.OnAnyFinishedState);

go deeper

for a junior

Remember that ContinueJobWith takes the parent's job id and runs the new job after the parent succeeds; ContinueWith is the old name.

for a middle

Explain the Awaiting state, the three JobContinuationOptions values, and that only Succeeded and Deleted release a continuation.

for a senior

Plan for the failure path: a continuation stuck behind a Failed parent, cleanup that must live in the job itself, and requeueing the parent to resume the chain.

for a principal

Judge when a chain of continuations is enough and when a multi-step process needs explicit workflow state that survives partial failure.

## What a continuation is A **continuation** is a background job that must not start until another job, its **parent** or antecedent, has finished. You create one with `BackgroundJob.ContinueJobWith(parentId, ...)` or `IBackgroundJobClient.ContinueJobWith(parentId, ...)`, passing the id that `Enqueue` or `Schedule` returned for the parent. The new job is stored in the **Awaiting** state together with the parent id, the state it should move to next (normally Enqueued) and a `JobContinuationOptions` value; the method returns the continuation's own id, so continuations can themselves be chained. In the online shop the natural chain is the nightly report: one job builds the sales report, a continuation e-mails it to the managers, and a second continuation on the same parent cleans up temporary files. ## When the wait ends Hangfire's `ContinuationsSupportAttribute` filter is registered globally. It treats two states as **final** for continuations: `Succeeded` and `Deleted`. When the parent enters one of them, the filter checks every continuation still in Awaiting: | `JobContinuationOptions` | Parent Succeeded | Parent Deleted | |---|---|---| | `OnlyOnSucceededState` (default) | enqueued | deleted: "Continuation condition was not met" | | `OnAnyFinishedState` | enqueued | enqueued | | `OnlyOnDeletedState` | deleted | enqueued | If the parent is already in a final state when the continuation is created, the same decision is made immediately, so you can attach a continuation to a job that has already finished. ## Why a failed parent does not release it A parent that throws is retried automatically and, when its attempts are exhausted, lands in the **Failed** state. Failed is deliberately **not** final: an operator can fix the cause and requeue the job. So the continuation stays in Awaiting, neither run nor deleted, for as long as the parent sits in Failed. Practical consequences: - An e-mail continuation will not send a half-built report: good. - A cleanup continuation with `OnAnyFinishedState` will **not** run on failure either, because Failed is not one of the states it waits for. If cleanup must happen on failure, put it in the job's own `try`/`finally` or delete the failed parent. - Requeueing the failed parent (`BackgroundJob.Requeue(parentId)`) lets it reach Succeeded, which then releases the continuation. ## Passing data along the chain A continuation is an independent job with its own serialized arguments; it does not automatically receive the parent's return value. The usual pattern is for the parent to write its result (the report file, a row in a reports table) and for both jobs to take the same key, such as the report date or an id, as an argument. ## A continuation, or one longer job? A continuation earns its place when the two steps differ in what they need or how they fail: - **Independent retries.** If the e-mail provider is down, only the e-mail job retries; the expensive report build is not repeated. - **Different queues or servers.** The heavy build can run on a reporting server while the e-mail goes through the default queue. - **Visibility.** Each step has its own state and history, so an operator sees which half failed. When the steps share a transaction, or the second is trivial, one job with two method calls is simpler and avoids a waiting job that can be orphaned. ## Names you will still see - `BackgroundJob.ContinueWith` and `IBackgroundJobClient.ContinueWith` are `[Obsolete]` in 1.8.25: "Deprecated for clarity, please use ContinueJobWith ... Will be removed in 2.0.0." The README still shows the old name. - Overloads with a `queue` parameter place the continuation on a named queue once it is released. ## Checklist 1. Keep the parent's id; it is the only link. 2. Choose the options from the business rule; the default fits "only after success". 3. Remember Failed is a waiting room, not an ending. 4. Pass a shared key, not the parent's output, to the continuation.

  • The report build failed after all its retries; how do you get the e-mail sent?
    Fix the cause, then requeue the parent with `BackgroundJob.Requeue(buildId)` (or from the dashboard). When it reaches Succeeded, the continuation that has been sitting in Awaiting is enqueued. Deleting the failed parent instead would delete a continuation created with the default options.
  • Can you attach a continuation to a job that has already succeeded?
    Yes. When the continuation is created, Hangfire reads the parent's current state; if it is already Succeeded or Deleted, the continuation is enqueued or deleted at once according to its options instead of waiting.

  • BackgroundJob.ContinueWith is the current Hangfire API for continuations.
  • A continuation runs as soon as the parent stops, whether it succeeded or failed.
  • When the parent fails, its continuation is deleted immediately.
  • OnAnyFinishedState also releases the continuation when the parent lands in Failed.
  • A continuation automatically receives the parent job's return value as an argument.

Assumes Hangfire 1.8.25, where ContinueWith is [Obsolete] in favour of ContinueJobWith and is scheduled for removal in 2.0.0.