skip to content

With Hangfire, a nightly sales report must run at 04:00 shop time and only once after a server outage; which RecurringJobOptions settings achieve that, and what are their defaults?

level: seniorimportance: should knowfreq 30%

answer

  1. the cron is read in a time zone
  2. TimeZone defaults to TimeZoneInfo.Utc
  3. zone id resolved again on the server
  4. MisfireHandling defaults to Relaxed
  5. Strict replays, Ignorable skips

basics

~10 s

RecurringJobOptions.TimeZone, default UTC, decides whose 04:00 the cron means. MisfireHandling, default Relaxed, makes a gap of missed occurrences produce one job; Strict creates one job per missed occurrence and Ignorable creates none.

solid answer

~40 s

Hangfire reads a recurring job's cron in `RecurringJobOptions.TimeZone`, which defaults to `TimeZoneInfo.Utc`, so `Cron.Daily(4)` alone means 04:00 UTC. For a shop in Berlin pass `new RecurringJobOptions { TimeZone = TimeZoneInfo.FindSystemTimeZoneById("Europe/Berlin") }`; only the zone's id is stored, and whichever server schedules the job resolves it again with `FindSystemTimeZoneById`, so every server must know that id. When all servers were down across one or more occurrences, `MisfireHandling` decides what happens on return: **`Relaxed`** (the default) creates a single job for the whole gap, **`Strict`** creates one job per missed occurrence, each stamped with its own scheduled time, and **`Ignorable`** skips them and waits for the next one. 'Only once after an outage' is therefore the default; you set it explicitly to document the rule. The overloads taking a `TimeZoneInfo` parameter are obsolete in 1.8.

code

csharp · 11 lines
csharp
var berlin = TimeZoneInfo.FindSystemTimeZoneById("Europe/Berlin");

app.Services.GetRequiredService<IRecurringJobManager>().AddOrUpdate<ISalesReportService>(
    "nightly-sales-report",
    s => s.BuildForPreviousDayAsync(CancellationToken.None),
    Cron.Daily(4),                               // "0 4 * * *", read in the zone below
    new RecurringJobOptions
    {
        TimeZone = berlin,                       // default: TimeZoneInfo.Utc
        MisfireHandling = MisfireHandlingMode.Relaxed // default: one run after an outage
    });

go deeper

for a junior

Remember that Hangfire reads recurring schedules in UTC unless you pass RecurringJobOptions with a TimeZone.

for a middle

Explain that only the zone id is stored and resolved on the server, and describe the three misfire modes with Relaxed as the default.

for a senior

Pick a misfire mode per job from its business meaning, keep schedules out of daylight-saving hours, and make sure every server resolves the configured zone id.

for a principal

Decide where wall-clock business rules belong: per-job zones in Hangfire or UTC everywhere with conversion in the job, and how outages are reconciled.

## Two questions every schedule must answer A cron expression such as `0 4 * * *` says "minute 0 of hour 4". It does not say **in which time zone**, and it does not say **what to do about occurrences nobody was around to run**. Hangfire answers both per recurring job through `RecurringJobOptions`, passed to `RecurringJob.AddOrUpdate` or `IRecurringJobManager.AddOrUpdate`. | Option | Default | Decides | |---|---|---| | `TimeZone` | `TimeZoneInfo.Utc` | the zone the cron is evaluated in | | `MisfireHandling` | `MisfireHandlingMode.Relaxed` | how many jobs a gap of missed occurrences produces | ## Time zone: whose 04:00? The `Cron` helpers return bare strings, so `Cron.Daily(4)` is just `"0 4 * * *"`. With default options Hangfire evaluates it in UTC, which for a shop in central Europe means 05:00 or 06:00 local time depending on the season. If the business rule is "after the shop's day has closed", set the zone: - Pass a `TimeZoneInfo` in `RecurringJobOptions.TimeZone`. Hangfire stores only its **id**. - When a server computes the next occurrence it resolves that id with `TimeZoneInfo.FindSystemTimeZoneById` (through the default `ITimeZoneResolver`). Every server that runs the scheduler must recognize the id; a mixed fleet should agree on one naming scheme, such as IANA ids like `Europe/Berlin` in Linux containers. - Avoid `TimeZoneInfo.Local`: it captures the zone of whichever machine registered the job, often UTC in a container and something else on a laptop. - Keep the wall-clock time away from the local daylight-saving switch. In central Europe the clocks jump between 02:00 and 03:00, so an hour inside that window is skipped once a year and repeated once a year; 04:00 sidesteps it. ## Misfires: what happens after an outage The scheduler stores the next execution time for each recurring job. If every server was down when that time passed (a failed deploy, a database outage, a weekend with the job servers scaled to zero), the next scheduler run finds one or more occurrences in the past. `MisfireHandlingMode` decides: | Mode | Jobs created for the missed occurrences | The "Time" job parameter | |---|---|---| | `Relaxed` (default) | exactly one, however many were missed | the moment the scheduler caught up | | `Strict` | one per missed occurrence | each occurrence's own scheduled time | | `Ignorable` | none for occurrences already past | (only on-time runs get one) | `Ignorable` still creates a job for an occurrence that is only slightly late, within the scheduler's own precision, so a normal polling delay does not drop runs. ## Choosing for the online shop 1. **Nightly sales report, "run once after an outage".** `Relaxed` is what you want, and it is the default; writing it out documents the rule. 2. **A report per business day, where each missing day matters.** `Strict`, with the job reading its "Time" parameter (through `PerformContext.GetJobParameter`) to know which night it stands for. 3. **"Send today's deals" e-mail at 08:00.** `Ignorable`: yesterday's deals mailed at noon are worse than none. ## Applying it, and what is obsolete In Hangfire 1.8.25 the `AddOrUpdate` overloads that take a `TimeZoneInfo` (and a queue) as separate parameters are `[Obsolete]`; the supported shape is an id, the expression, the cron and a `RecurringJobOptions` instance. Changing the zone in code and redeploying is enough: `AddOrUpdate` sees a different time zone id and recomputes the next run. - The default time zone is UTC, not the server's. - The default misfire policy collapses a gap into one run. - Both are per recurring job, stored with its definition.

  • Why not set TimeZone = TimeZoneInfo.Local?
    `Local` is the zone of whichever process registered the job: usually UTC in a container, the developer's zone on a laptop. The same code then stores different schedules depending on where it ran. An explicit id states the business rule and behaves the same everywhere that id is known.
  • The report must cover each missed night separately after a three-night outage; what changes?
    Set `MisfireHandling = MisfireHandlingMode.Strict`. Hangfire then creates three jobs, each with its "Time" job parameter set to its own scheduled occurrence, and the job reads that parameter to pick the night it reports on instead of assuming the previous day.

A shop's night-time cleaning crew: the rota says 04:00, but you must say whose 04:00. After a week of closure the manager decides whether the crew comes in once (Relaxed), cleans once for every missed night (Strict), or simply resumes the rota (Ignorable).

  • Cron.Daily(4) runs at 04:00 in the server's local time.
  • After an outage Hangfire replays every missed occurrence by default.
  • Hangfire stores the whole TimeZoneInfo, so servers need not know the zone id.
  • Passing a TimeZoneInfo argument straight to AddOrUpdate is the current Hangfire 1.8 API.
  • MisfireHandlingMode.Ignorable also drops runs that are only a few seconds late.

Assumes Hangfire 1.8.25: TimeZone and MisfireHandling live on RecurringJobOptions, and the AddOrUpdate overloads that take a TimeZoneInfo parameter are [Obsolete].