Attributes (.NET)
In .NET, jobs can be declared on the methods of a class with [Cron], [Every] and [At], instead
of being passed to Cron(), Every() and At(). In Node.js the same idea is
decorators, with a different way of adding the jobs.
Adding the jobs
Section titled “Adding the jobs”There are two ways, and both add every job of the class or none.
With a host (ASP.NET Core, a worker service), AddJobs<T>() registers the class as a scoped
service. Each run gets a new service scope and a new object from it, so the class can take scoped
services such as a DbContext:
The host starts the scheduler, and stops it on shutdown. See ASP.NET Core and Worker service.
Without a host, Register(object) adds the jobs of one object, which runs every job:
The three attributes
Section titled “The three attributes”| Attribute | The schedule | Like |
|---|---|---|
[Every("10s")] |
An interval on the UTC grid, as text: "10s", "35m", "1h30m", "2d" |
Every() |
[Cron("0 8 * * 1-5")] |
A cron expression, 5 fields or 6 with seconds first | Cron() |
[At("2026-12-01T09:00:00Z")] |
One time, as ISO 8601 text (UTC unless it says otherwise) | At() |
Every option of a job is a property of the attribute. Durations are text, read by Duration.Parse
("30s", "1h30m", "500ms"):
| Property | On | Means |
|---|---|---|
Name |
all | The job’s name. Default Type-Method, such as ReportJobs-SyncAsync |
Jitter, MaxLate, Timeout, HistoryTtl, CatchUp |
all | As the job options. Jitter = "0s" and CatchUp = "0s" turn them off |
HistorySize, MaxAttempts, YieldAfterFailure |
all | As the job options |
Overlap |
[Every], [Cron] |
OverlapPolicy.Skip (default) or OverlapPolicy.Allow |
TimeZone |
[Cron] |
An IANA name such as "Europe/Athens" |
Offset |
[Every] |
Shifts the grid, such as "30m" |
A property left out takes the value of RegisterOptions.Defaults, then of the scheduler.
The methods
Section titled “The methods”A job method is a public instance method of the class, inherited ones included. It returns Task,
ValueTask or nothing, and takes any of JobContext and CancellationToken, in any order:
A method with another parameter, or a class without a single job method, is refused with an
ArgumentException when the jobs are added, not when they run. An override’s own attributes replace
those of the method it overrides.
Several objects of one class
Section titled “Several objects of one class”RegisterOptions lets one class run once per tenant, region or store: a Prefix for the names, a
TimeZone for its cron jobs, and Defaults for every job of the object, run data included.
AddJobs<T>(new RegisterOptions { ... }) takes the same options.
One-off jobs take an absolute time
Section titled “One-off jobs take an absolute time”[At] takes a fixed time. A time computed when the program starts (“in one hour”) belongs in code:
scheduler.At("reminder", DateTimeOffset.UtcNow.AddHours(1), RemindAsync). A one-off job whose time
passed long ago never runs again; within the catch-up window it is caught up once, as other jobs are.
Examples to run
Section titled “Examples to run”Both are in anarchon-dotnet.
- Job options: what each option does
- Attributes reference: every attribute and property
- ASP.NET Core: jobs in a web app