In Hangfire, which states can a background job be in, how does it move between them, and which ones expire on their own?
answer
- Enqueued, Scheduled, Awaiting, Processing
- Succeeded and Deleted are final
- final states expire after 1 day
- Failed waits for a human
- IMonitoringApi.GetStatistics() for counts
basics
~20 sA job starts Enqueued, Scheduled or Awaiting, becomes Processing on a worker, and ends Succeeded, Failed or Deleted. Succeeded and Deleted are final and expire after a day; Failed stays until someone requeues or deletes it.
solid answer
~50 sHangfire's built-in states are **Enqueued** (in a queue, waiting for a worker), **Scheduled** (waiting for a time: delayed jobs and retries), **Awaiting** (a continuation waiting for its parent), **Processing** (a worker is running it), **Succeeded**, **Failed** and **Deleted**. A worker moves Enqueued to Processing; the result moves it to Succeeded, or, when the method throws, the global `[AutomaticRetry]` filter turns the Failed candidate into Scheduled with a 'Retry attempt n of 10' reason until attempts run out and it stays **Failed**. Only **Succeeded** and **Deleted** are *final*: their data expires after `JobStorage.JobExpirationTimeout`, one day by default. Failed is deliberately not final, so it never expires and waits for someone to requeue or delete it. The dashboard shows these lists, and code reads the same numbers through `IMonitoringApi`, for example `GetStatistics()` or `FailedCount()`, to alert when failures pile up.
code
csharp · 18 linesusing Hangfire;
using Microsoft.Extensions.Diagnostics.HealthChecks;
public sealed class HangfireHealthCheck(JobStorage storage) : IHealthCheck
{
public Task<HealthCheckResult> CheckHealthAsync(
HealthCheckContext context, CancellationToken cancellationToken = default)
{
var stats = storage.GetMonitoringApi().GetStatistics();
if (stats.Servers == 0)
return Task.FromResult(HealthCheckResult.Unhealthy("No Hangfire servers"));
return Task.FromResult(stats.Failed > 50
? HealthCheckResult.Degraded($"{stats.Failed} failed jobs")
: HealthCheckResult.Healthy());
}
}go deeper
Name the states a Hangfire job goes through, from Enqueued to Processing to Succeeded or Failed, and where to see them in the dashboard.
Explain which states are final, why Succeeded and Deleted expire after a day while Failed stays, and how retries pass through Scheduled.
Turn states into operations: alert on Failed and on zero servers through IMonitoringApi, and give failed jobs an owner who requeues or deletes them.
Decide what job history must be kept beyond Hangfire's expiry, and where background-job health sits in the service's overall monitoring.
## The state machine Every Hangfire background job has exactly one current **state**, stored with a history of previous states. Transitions are made through a state machine that runs **job filters** at each election, which is how retries and continuations hook in. | State | Meaning | Typical way in | Final? | |---|---|---|---| | Enqueued | in a queue, waiting for a worker | `Enqueue`, a due delayed job, a requeue | no | | Scheduled | waiting until a time | `Schedule`, an automatic retry | no | | Awaiting | a continuation waiting for its parent | `ContinueJobWith` | no | | Processing | a worker is executing it | fetched by a worker | no | | Succeeded | the method returned | normal completion | **yes** | | Failed | the method threw and no retry was elected | attempts exhausted, or retries off | no | | Deleted | removed by a person or code | dashboard delete, `BackgroundJob.Delete`, `OnAttemptsExceeded = Delete` | **yes** | ## A typical life 1. A marketplace enqueues a seller payout: **Enqueued** on its queue. 2. A worker fetches it: **Processing**, with the server and worker recorded. 3. The payment provider times out and the method throws. The candidate state is Failed, but the global `AutomaticRetryAttribute` elects **Scheduled** instead, with a delay and the reason "Retry attempt 1 of 10: ...". 4. When the delay passes, the scheduler moves it back to **Enqueued**, and the loop repeats. 5. Either it succeeds (**Succeeded**) or the tenth retry fails and it is left **Failed**. ## Final states and expiration A state is **final** when the job's life is over: `SucceededState` and `DeletedState` report `IsFinal = true`. When a job enters a final state Hangfire sets its data to expire after `JobStorage.JobExpirationTimeout`, **1 day** by default, and the storage's expiration manager removes it later. Consequences: - the Succeeded and Deleted lists only show recent history; - Failed jobs, which are not final, **never expire**. They are a to-do list: requeue after fixing the cause, or delete; - an ever-growing Failed count is a signal, not noise. ## Reading the numbers from code The dashboard is built on `IMonitoringApi`, which any code can get from `JobStorage.GetMonitoringApi()` (`JobStorage` is registered in DI by `AddHangfire`): - `GetStatistics()` returns a `StatisticsDto` with counts for Enqueued, Scheduled, Processing, Succeeded, Failed, Deleted, Servers, Recurring and Retries; - `FailedCount()`, `ScheduledCount()`, `EnqueuedCount(queue)` and `ProcessingCount()` return single numbers; - `FailedJobs(from, count)` and `JobDetails(jobId)` return details, including exception messages and state history. A common use is a health check or a metrics exporter that reports the Failed count and the number of live servers, so an alert fires before someone happens to open the dashboard. ## Operating rules of thumb - Watch **Failed** (needs action) and **Servers** (zero means nothing is processing), not Succeeded. - Scheduled jobs carrying a retry reason are failures still in progress; the dashboard lists them under Retries. - Processing jobs whose server has vanished come back through the storage's invisibility timeout, not through a state change.
- Why does a payout that failed last month still appear in the dashboard while last week's successes are gone?Succeeded is a final state and expires after `JobExpirationTimeout`, one day by default. Failed is not final, so Hangfire keeps it with no expiry until someone requeues or deletes it.
- What happens when you requeue a Failed job from the dashboard?It moves to Enqueued on its queue and runs again as a normal attempt. The retry counter stored in the job's RetryCount parameter is not reset by the requeue, so if it fails again it goes straight back to Failed without new automatic retries.
A parcel tracker: received, waiting for pickup, out for delivery, delivered. Delivered and returned-to-sender are archived after a while; 'delivery failed' stays open on the depot's desk until someone reschedules or cancels it.
- Failed jobs are cleaned up automatically after a day like succeeded ones.
- A job that throws goes straight to Failed on the first exception by default.
- Processing is a final state once a worker has started the job.
- Succeeded jobs stay in storage forever unless someone deletes them.
- The only way to read job counts is to scrape the dashboard pages.