ASP.NET Core
In an ASP.NET Core app the scheduler is a service. AddAnarchon() registers it, the host starts it
with the app and stops it on shutdown, and AddJobs<T>() adds the jobs declared on a class with
attributes. Each run gets a service scope of its own, so jobs can take
scoped services such as a DbContext.
This is the .NET counterpart of the Express and NestJS recipes.
Install
Section titled “Install”Each backend package brings Anarchon and the UsePostgres(), UseRedis() (and so on) methods.
Program.cs
Section titled “Program.cs”What each part does:
| Call | Effect |
|---|---|
AddAnarchon(options => ...) |
Registers one Scheduler as a singleton, and a hosted service that starts and stops it. The options are SchedulerOptions |
UsePostgres(...) |
The backend. The (services, options) form takes clients from the container; the string form takes a connection string |
AddJobs<ReportJobs>() |
Registers ReportJobs as scoped, and adds every job its attributes declare |
Scheduler in a handler |
The same singleton: endpoints read history and start runs through it |
The scheduler’s log goes to the app’s logging, under the category Anarchon.Scheduler. See
Events and logging.
The jobs
Section titled “The jobs”A new ReportJobs is created for each run, from a new scope, and disposed of after it. Keep no state
in its fields between runs; run data and the store are the places for that.
Start-up and shutdown
Section titled “Start-up and shutdown”- Start-up. The hosted service calls
StartAsync()before the app takes requests. If the store is unreachable, start-up fails, and so does the app: a pod that can’t reach its store is not ready. - Shutdown. On
SIGTERMor Ctrl+C the host stops the scheduler. Running jobs see their token cancelled; on a lease backend a job that throws then is handed to another instance at once. The wait isStopTimeout(30 seconds), and no longer than the host’sShutdownTimeout. See Rolling deploys on Kubernetes.
The endpoints
Section titled “The endpoints”| Endpoint | Calls | Notes |
|---|---|---|
GET /jobs |
GetJobs() |
Jobs registered on this instance, with their next run |
GET /jobs/{name}/runs |
HistoryAsync(name, limit) |
Runs from every instance, newest first |
GET /jobs/{name}/last |
LastRunAsync(name) |
The latest run, or 404 |
POST /jobs/{name}/run |
RunNowAsync(name) |
Runs the job here and answers when it finished. Protect it: "admin" stands for an authorization policy of yours |
JsonStringEnumConverter makes statuses and reasons read as "Success" and "Takeover" rather than
numbers.
The health check
Section titled “The health check”CronHealthCheck asks the store, not this instance, when sync last ran. It passes on every
instance as long as some instance runs the job, which is what a load balancer should see. Don’t
use it for liveness: a failing job is an alert, not a reason to restart pods.
Jobs in code instead of attributes
Section titled “Jobs in code instead of attributes”Jobs can also be added in code, once the app is built and before it runs:
Such handlers run on the scheduler’s thread pool, outside any scope: create one yourself with
IServiceScopeFactory when a job needs scoped services.
Several schedulers
Section titled “Several schedulers”AddAnarchon("name") registers a keyed scheduler, for jobs on another store or with other lease
timings. Each has its own backend and jobs, and the host starts and stops all of them:
Run it
Section titled “Run it”The HttpApi example
is a smaller version of this app, on a SQLite file by default:
Start it twice (PORT=3001 for the second) and the runs spread over both instances.
- Worker service: the same without a web server
- A worker process and a read-only API
- Hosting reference