Quick start
In this guide you write a small scheduler, run it as three instances, and watch one instance win each run. Then you stop and kill the instance that is running a job, and watch another start that run again.
It uses a SQLite file as the shared store, so there is nothing to install besides Node 22.13 or newer (which has SQLite built in) or the .NET 10 SDK. The three instances coordinate because they open the same file on the same machine. For instances on several machines, switch to Redis or PostgreSQL.
1. Create the project
Section titled “1. Create the project”npm comes with Node. With pnpm or yarn, use pnpm add or yarn add instead of npm install.
Anarchon.Sqlite brings in Anarchon, the core, and Microsoft.Data.Sqlite with SQLite built in.
2. Write the scheduler
Section titled “2. Write the scheduler”Create app.ts:
Replace Program.cs:
3. Run three instances
Section titled “3. Run three instances”Open three terminals in the project folder and run the same command in each. The folder matters: jobs.db is a relative path, and the instances only see each other when they open the same file.
Each terminal prints its own name, something like started as my-laptop:41822. The number after the colon is the process ID; you need it in step 5. Node also prints ExperimentalWarning: SQLite is an experimental feature, which is expected.
Watch the output. Every 10 seconds, exactly one terminal prints that it started sync, and the other two print that they skipped it:
Each terminal prints its own name, something like started as my-laptop:41822. The number after the colon is the process ID; you need it in step 5.
Watch the output. Every 10 seconds, exactly one terminal logs that it started sync, and the other two log that they skipped it:
Which terminal wins changes from run to run. That is the random delay at work.
4. Stop the runner gracefully
Section titled “4. Stop the runner gracefully”Wait until one terminal prints that it started report, then press Ctrl+C in that terminal.
Within a second or two, another terminal prints that it started the same run as attempt 2, and the stopped terminal prints that it gave the run up:
The stopped instance handed the run over. This is what happens on every rolling deploy. Both lines name the same run (12:00:30), and the second one is attempt 2: the job starts again from the beginning on the new instance; it doesn’t continue where the first one stopped.
5. Crash the runner
Section titled “5. Crash the runner”Start the stopped instance again. Wait for report to start somewhere, then kill that process hard, from a fourth terminal. On Linux and macOS:
This time nothing is handed over: the process is simply gone. About 6 seconds later (the lease you set), a backup notices the lease was not renewed and takes over:
Check that the takeover line has the same time as the run you killed and says attempt 2. A line with a later time and attempt 1 is the next scheduled run, not a takeover. The delay is the lease: with the default lease of 30 seconds (leaseMs in Node.js, Lease in .NET), a takeover comes about 30 seconds after a crash.
The job ran twice for that slot, once in part and once in full. That is why handlers must be safe to run twice.
What just happened
Section titled “What just happened”sequenceDiagram
participant A as Instance A
participant B as Instance B
participant S as jobs.db
A->>S: claim report @ 12:01:00
S-->>A: won (attempt 1)
B->>S: claim report @ 12:01:00
S-->>B: running, so B becomes a backup
loop every second
A->>S: heartbeat: lease ends in 6 s
end
Note over A: kill -9
loop every 1 to 2 seconds
B->>S: is the lease still alive?
end
Note over S: 6 s without a heartbeat
B->>S: claim attempt 2
S-->>B: won (attempt 2)
Note over B: runs the job again
Switch to a real store
Section titled “Switch to a real store”Install the package for your store, have the server running at the address you pass, and change one line. Everything else stays the same.
- Core concepts: slot, claim, jitter and lease, defined
- Defining jobs: cron expressions, intervals and one-off jobs
- Decorators (Node.js) and attributes (.NET): declare the same jobs on the methods of a class
- Writing handlers: what
ctxholds and why its cancellation (ctx.signal,ctx.CancellationToken) matters