Testing and debugging
Testing a handler
Section titled “Testing a handler”A handler is a function. The most useful tests call it directly with a context you build yourself. No scheduler is involved.
In .NET only the scheduler creates a JobContext, so let the handler pass on what it uses (the slot, the attempt, the token) to a method you can call from a test.
Those two tests cover the two things a handler owes the scheduler: being safe to run twice, and stopping when asked.
Testing with a real scheduler
Section titled “Testing with a real scheduler”To test the wiring, use SqliteBackend on a temporary file. It needs no server and behaves like the production lease backends.
Run a job without waiting for its schedule
Section titled “Run a job without waiting for its schedule”runNow() (RunNowAsync()) executes the job at once and returns its record:
catchUpMs: 0 (CatchUp = TimeSpan.Zero) stops the scheduler from also running the latest slot on start, which would otherwise add a run you didn’t ask for.
Two instances
Section titled “Two instances”To see that a run happens once, share the file between two schedulers:
Validation is checked at registration
Section titled “Validation is checked at registration”Bad options throw when the job is added, so they are easy to test without starting anything:
Don’t mock the clock around a real backend
Section titled “Don’t mock the clock around a real backend”Fake timers move your process’s clock, but the store keeps its own. On a lease backend the lease end is the store’s time, so advancing a fake clock by an hour doesn’t expire a lease, and the test ends up exercising something that can’t happen. Test handlers with a fake context, and test wiring with runNow() and short real intervals. In .NET this holds for SchedulerOptions.TimeProvider too: a FakeTimeProvider moves the scheduler’s timers, not the store’s clock.
Debugging a run
Section titled “Debugging a run”The repository ships examples made for a debugger. From a checkout:
examples/walkthrough.ts (examples/Walkthrough in .NET) runs two instances in one process against a SQLite file, with a 10-minute lease and lateness limit, so sitting on a breakpoint doesn’t change the outcome. Its header lists the functions a run passes through, in order.
In VS Code, open Run and Debug, choose Example: walkthrough (step through one run) and press F5. It pauses just before start(). Step in with F11, or set breakpoints on the functions below. The .NET repository has the same launch entry (Example: Walkthrough), which stops on Debugger.Break(); its header lists the C# methods, such as JobTimers.Arm(), LeaseRunner.RunAsync() and Executor.ExecuteAsync().
File under src/scheduler/ |
Function | What happens there |
|---|---|---|
timers.ts |
arm() |
The next slot is computed and a timer is set |
scheduler.ts |
fire() |
The timer fired: the run is due |
lease-runner.ts |
run() |
The claim is sent and its outcome handled |
lease-runner.ts |
executeLeased() |
The heartbeat starts |
executor.ts |
execute() |
Your handler is called, and the result stored |
lease-runner.ts |
watch() |
A backup schedules its next check |
Other examples in the repository. The .NET repository has most of them as projects: Failover, OptionsAndEvents, HttpApi, OwnClients, Basic, Attributes, Hosting, RunData, QuickStart, and the real-life setups below.
| Example | Shows |
|---|---|
examples/failover.ts |
Three instances: a graceful hand-over, then a crash and takeover |
examples/nats-leases.ts |
NATS with strategy: 'lease', in three processes: a hand-over on SIGTERM, a takeover after kill -9, and catch-up after every process was down |
examples/options-and-events.ts |
Every job option and every event |
examples/http-api.ts |
The Express API |
examples/own-clients.ts |
Passing in your own Redis client or Postgres pool |
examples/basic.ts |
Run it in three terminals and kill one |
examples/decorators.ts |
Jobs declared with @Every, @Cron and @At on a class, with standard decorators |
examples/decorators-legacy.ts |
The same with legacy decorators (experimentalDecorators): @Scheduled() and register() |
examples/decorators-startup.ts |
The three ways to start decorated jobs: autoStart, Scheduler.startAll(), or a scheduler created by hand |
examples/decorators-options.ts |
Schedulers created from settings, a scheduler per job, several objects of a class, and run data |
examples/run-data.ts |
Run data: the scheduler’s, a job’s, a function read at each run, runNow() and setData() |
examples/nestjs |
A small NestJS app whose providers declare jobs with decorators. Checked on NestJS 11 and 12 |
Real-life setups: each is a situation solved with a different set of options, and its header explains every choice.
| Example | Situation | Options approach |
|---|---|---|
examples/billing.ts |
Charge customers once a day, and still charge after an outage | maxAttempts: 1, catchUpMs, timeoutMs, timezone, writes keyed by ctx.slot |
examples/cache-refresh.ts |
A cheap refresh every few seconds on three instances | jitterMs to spread it, a short maxLateMs, catchUpMs: 0, a small historySize |
examples/nightly-export.ts |
A long export interrupted by a deploy | ctx.signal between pages, a checkpoint per run, ctx.attempt, stop({ timeoutMs }) |
examples/tenants.ts |
The same jobs per tenant, each in its own time zone | one job per tenant, timezone and data per job, a data function, remove(), runNow() with data |
examples/broker-notifications.ts |
Only RabbitMQ, or NATS with msg-id: no leases |
every run sends everything outstanding, maxLateMs under the broker’s memory, no lease options |
In .NET these are Billing, CacheRefresh, NightlyExport, Tenants and BrokerNotifications.
Seeing what an instance does
Section titled “Seeing what an instance does”Turn on the built-in log while you investigate:
Or log every event with its payload:
Then compare with the cluster-wide view:
Common surprises
Section titled “Common surprises”| What you see | Why |
|---|---|
A run starts right after start() with reason catch-up |
The latest slot was due recently and nobody ran it. Set catchUpMs: 0 (CatchUp = TimeSpan.Zero) to turn that off |
Every instance but one logs skipped (taken) |
That is normal: one winner per run |
skipped (overlap) |
The previous run of that job is still going |
| A run shows twice in history | One record per attempt: it was taken over |
| A job runs on the same instance every time | jitterMs is 0, or that instance’s clock is ahead |
lost events |
The instance couldn’t renew its lease in time: it was paused, blocked or cut off from the store |