Lifecycle
A scheduler is in one of four states.
stateDiagram-v2 [*] --> idle idle --> starting: start() starting --> running: connected starting --> idle: the backend can't be reached running --> stopping: stop() stopping --> idle: jobs finished or the timeout passed
Nothing connects and nothing runs until the scheduler starts. In .NET the methods are StartAsync(), StopAsync(), RunNowAsync() and Remove(); with a host, the host starts and stops the scheduler for you.
start(), StartAsync()
Section titled “start(), StartAsync()”In order, starting:
- Connects to the backend and checks that the server is new enough.
- Prepares each job: creates what the backend needs (tables, streams, queues).
- Sets a timer for the next slot of each job.
- On a lease backend, recovers what was left behind:
- a run whose runner died is taken over;
- a run that is alive on another instance gets this instance as a backup;
- the latest run is caught up, if nobody ran it and it was due less than the catch-up window ago.
If the backend can’t be reached, starting fails and the scheduler stays idle; it closes what it opened, and can be started again. Handle that at boot:
Starting a scheduler that is already running does nothing. Jobs can be added before or after it.
stop(), StopAsync()
Section titled “stop(), StopAsync()”In order, stopping:
- Stops scheduling. No new run starts on this instance.
- Cancels every job running here (
ctx.signalaborts,ctx.CancellationTokenis cancelled). - Waits for those jobs to end, and for claims still in flight, up to the stop timeout (
stop({ timeoutMs })in Node.js,StopTimeoutin .NET; default 30 seconds). A run whose claim is won after stopping began doesn’t start; it is recorded as interrupted. - Closes the connection, if anarchon opened it. A client you passed in stays open.
After stopping returns, anarchon sends nothing more to the store. You can start it again later. Reading history still works: after a stop, each read opens the connection for itself and closes it when it is done, so the process can exit.
What happens to a job that was running
Section titled “What happens to a job that was running”It depends on what the handler does once its signal aborts.
flowchart TD
stop["stopping cancels the run"] --> q{"what does the handler do?"}
q -- "throws" --> handover["Run recorded as interrupted<br/>and handed over. A backup starts<br/>the next attempt within 2 heartbeats"]
q -- "returns normally" --> done["Run recorded as success.<br/>Nobody continues it"]
q -- "keeps going past the stop timeout" --> zombie["stopping returns anyway.<br/>The lease stops being renewed, so a backup<br/>takes over when it runs out"]
The hand-over needs a lease backend. On claim-only backends an interrupted run is simply recorded as interrupted.
A hand-over doesn’t count against the attempts, so rolling deploys can’t use up a run’s retries.
Graceful shutdown
Section titled “Graceful shutdown”Stop the scheduler when the process is asked to end. Orchestrators send SIGTERM; Ctrl+C sends SIGINT.
Keep the stop timeout shorter than the time your platform allows before it kills the process. On Kubernetes that is terminationGracePeriodSeconds, 30 seconds by default. If the process is killed while the scheduler is still stopping, a running job isn’t handed over; a backup takes it over only after the lease runs out.
With an HTTP server, stop accepting requests first, then stop the scheduler:
Rolling deploys on Kubernetes covers this end to end.
runNow(), RunNowAsync()
Section titled “runNow(), RunNowAsync()”Runs a job immediately on this instance, whatever its schedule, and resolves with its run record when the handler has finished. If the scheduler stops before the handler starts, it resolves with status interrupted and the job doesn’t run.
A manual run is different from a scheduled one:
| Scheduled run | Manual run | |
|---|---|---|
| Deduplicated across instances | Yes | No: calling it on two instances runs it twice |
| Blocked by a run in progress | Yes, with the default overlap (skip) | No |
| Retried by a backup if the runner dies | Yes | No |
| In history | Yes | Yes, marked manual (manual, IsManual) |
| The reason | scheduled | manual |
It throws if the job isn’t registered on this instance, or if the scheduler isn’t started.
A typical use is an admin endpoint:
remove(), Remove()
Section titled “remove(), Remove()”Stops scheduling a job on this instance. Other instances keep running it, its history is kept, and a run in progress here finishes. See Adding and removing jobs.