Concepts
Scheduled tasks
Run a use case at fixed times, such as every day at 9:00, once across all your instances.
A scheduled task runs a use case at fixed times, written as a cron expression. Use it for work nobody asks for with a request: daily reminders, nightly clean-ups, weekly reports.
A scheduled task
Section titled “A scheduled task”You define it in the module’s constructor with base.schedules.define(name, cron), and subscribe a use case to it:
readonly remindOverdueSchedule
private constructor( base: ContextBase, private readonly tasks: Task_Repository, private readonly deps: Task_Deps,) { super('task', base.useCaseRunner, deps) this.remindOverdueSchedule = base.schedules.define( 'task.remindOverdue', '0 9 * * *', )}
@UseCase()async remindOverdue(): Promise<void> {}export function registerTaskWorkers(tasks: Task_Module): void { tasks.on(tasks.remindOverdueSchedule, 'remindOverdue')}define returns an event source, like one of the module’s events. When the time comes, the schedule emits an event named after it, and on runs the use case from a job, as any subscription.
- A schedule is named
<module>.<name>, in camelCase:task.remindOverdue. - Each name is defined once per context. Defining it twice throws.
- The subscription’s queue is
task.remindOverdue.on.task.remindOverdue.
What the use case gets
Section titled “What the use case gets”The event’s payload is { dueAt }: the time the run was due, as an ISO date. A use case with no input ignores it. To pass it on, map it:
tasks.on(tasks.remindOverdueSchedule, 'remindOverdue', tick => [ { until: tick.dueAt },])Cron expressions
Section titled “Cron expressions”A cron expression has five fields, separated by spaces:
minute hour day-of-month month day-of-week0-59 0-23 1-31 1-12 0-7Each field takes:
| Form | Example | Means |
|---|---|---|
* |
* |
every value |
| a number | 9 |
that value |
| a range | 1-5 |
from 1 to 5 |
| a list | 0,30 |
each of them |
| a step | */15, 10-50/10, 5/15 |
every 15, from the start |
In day-of-week, both 0 and 7 are Sunday. When both day-of-month and day-of-week are restricted (neither starts with *), a day matches if it fits either one, as in classic cron.
Some examples:
| Cron | Runs |
|---|---|
0 9 * * * |
every day at 09:00 |
*/15 * * * * |
every 15 minutes |
0 3 * * 1-5 |
Monday to Friday at 03:00 |
0 0 1 * * |
the first of each month at 00:00 |
Names (MON, JAN), @daily, ?, L and # are not supported. A wrong expression throws when the module is built, so the app does not start.
Time zone
Section titled “Time zone”Every cron runs in UTC. There is no time zone option.
What runs it
Section titled “What runs it”Each context starts its schedules when runContexts starts it, alongside its worker. There is no separate process to run.
The schedules look at the clock at least once a minute, and sooner when the next run is closer. A run starts within about a minute of its time.
Each time it looks, it does this for every schedule:
Updating the row and emitting the event happen in one transaction, and the job runs with the same retries as any job.
Once across instances
Section titled “Once across instances”When you run several instances, each one looks at the clock. Only one wins: the row in newt_schedule_tick has a version, and only the first instance to update it emits the event. The use case runs once per due time, whatever the number of instances.
The first run, and missed runs
Section titled “The first run, and missed runs”- The first time a schedule is seen by a database (no row yet), newt only records its latest due time. The first real run is the next one.
- If the app was down when runs were due, it runs once when it comes back, for the latest of them, not once per missed run.
In tests
Section titled “In tests”The test context does not tick on its own. Call schedules.tick(), move the test clock, and wait for the jobs:
test('remindOverdue runs at 0 9 * * *', async () => { await context.base.schedules.tick() context.clock.advance(366 * 24 * 60 * 60 * 1000) await context.base.schedules.tick() await context.settle()
const queued = await context.base.database.outbox.list() expect( queued.find( each => each.queue === 'task.remindOverdue.on.task.remindOverdue', )?.status, ).toBe('done')})The first tick() records the starting point; the second, a year later, finds a run due. See Testing.
With the CLI
Section titled “With the CLI”newt add schedule writes the schedule, the use case and its subscription:
newt add schedule tasks/task remind-overdue --cron "0 9 * * *"It adds remindOverdueSchedule and an empty remindOverdue use case to the module, the line in task.workers.ts, and a test. The use case may not take an input, since a schedule gives none. See newt add schedule.