Scheduling
Job lifecycle
A job is stopped, running or paused. These states describe its schedule, not whether its callback happens to be executing.
On this page
States
| Status | Meaning |
|---|---|
stopped |
Nothing is scheduled. New jobs start here unless told otherwise. |
running |
The job is scheduled and will fire at its next tick. |
paused |
The schedule is suspended until resume() is called. |
error |
Part of the Status type, but nothing sets it at the moment. |
isRunning() returning true means a job is scheduled. It doesn’t mean its callback is executing right now, and between ticks nothing is.
A job also stays running after its callback fails, because a failed run doesn’t affect the schedule. To find out whether a job is healthy, check its metrics.
Control methods
Calling a method in a state where it doesn’t apply does nothing and doesn’t throw.
| Method | From stopped |
From running |
From paused |
|---|---|---|---|
start() |
running |
no change | running |
stop() |
no change | stopped |
stopped |
pause() |
no change | paused |
no change |
resume() |
no change | no change | running |
destroy() |
stopped |
stopped |
stopped |
On a Baker, the same methods take the job’s name, except that start() is called bake(). Each one also has a version for all jobs: bakeAll(), stopAll(), pauseAll(), resumeAll() and destroyAll().
destroy() is the only one that fires onComplete. On a Baker it also removes the job, so the name is free to use again. baker.remove(name) does exactly the same thing.
Callbacks already in progress
stop() and pause() cancel future runs but don’t interrupt a run in progress. That callback finishes normally and its result is still recorded. There is no built-in way to wait for it, so see shutting down if you need that.
Ticks during a pause
resume() works out the next tick from the current time. Anything that came due while the job was paused is dropped, not caught up.
Starting jobs
There are three ways to start a job on a Baker.
// 1. Start it yourself, whenever you're ready
baker.add({ name: "a", cron: "@hourly", callback });
baker.bake("a"); // or baker.bakeAll()
// 2. Start one job as soon as it's added
baker.add({ name: "b", cron: "@hourly", start: true, callback });
// 3. Start every job as soon as it's added
const auto = Baker.create({ autoStart: true });
await auto.ready();
auto.add({ name: "c", cron: "@hourly", callback });
auto.add({ name: "d", cron: "@hourly", start: false, callback }); // opts outIn 0.4.0, autoStart only starts the jobs that exist when the baker finishes initialising, so you need to start any added later. New jobs start automatically from the next release.
Running on start
By default a job waits for its first scheduled tick, which could be a month away for @monthly. immediate: true also runs it once as soon as it starts, and delay holds that first run back.
baker.add({
name: "warm-cache",
cron: "@hourly",
immediate: true,
delay: "10s",
callback: warmCache,
});delay is a number of milliseconds, or a string with a unit of ms, s, m, h or d, such as "500ms" or "2m". A string with no unit counts as milliseconds. Negative numbers, decimals in strings and other units throw when the job is created.
delayhas no effect withoutimmediate: true.- Nothing runs until the delay is over, even if a normal tick comes due in the meantime.
- The first run happens on every
start(), including restarting a stopped or paused job.resume()doesn’t trigger it.
Reading the schedule
| Method | Returns | When the job isn’t scheduled |
|---|---|---|
nextExecution() |
The Date of the next tick |
The current time |
remaining() |
Milliseconds until the next tick | 0 |
time() |
Same as remaining() |
0 |
lastExecution() |
The Date of the last actual run |
Not affected by state |
Two of these are easy to misread:
nextExecution()returns the current time for a stopped or paused job, so checkisRunning()first.lastExecution()returns the most recent time the schedule matched if the job has never run. To know whether it has really run, look atgetHistory().
Names that don’t exist
Baker doesn’t throw for an unknown job name. Control methods do nothing, getStatus() returns "stopped", isRunning() returns false, remaining() returns 0, and the date methods return the current time. A typo looks like a stopped job, so use getJobNames() if you need to tell them apart.
Priority
priority is a number that sets the order bakeAll() starts jobs in, highest first. It defaults to 0, and ties start in the order they were added.
baker.add({ name: "first", cron: "@hourly", priority: 10, callback });
baker.add({ name: "second", cron: "@hourly", priority: 5, callback });
baker.bakeAll();That’s all priority does. It doesn’t reserve resources or let one job interrupt another, and it has no effect on two jobs that fall due at the same moment. In 0.4.0, jobs start in the order they were added regardless of priority.