Background Jobs
Scheduler
Five-field cron scheduler with minute-aligned ticks, exported cron parsing and validation helpers, and a pluggable logger.
On this page4
Usage
TypeScript
TS
1import { Scheduler } from '@axiljs/queue'
2
3const scheduler = new Scheduler()
4
5scheduler.schedule('daily-report', '0 9 * * *', sendReport)
6scheduler.schedule('cleanup', '0 2 * * *', cleanupData)
7
8scheduler.start()
9await scheduler.runNow('daily-report')Ticks are aligned to minute boundaries, so timer drift does not accumulate. Other methods: stop(), setEnabled(name, enabled) and getTasks(). The scheduler emits completed (name, durationMs) and failed (name, error).
Logger
TypeScript
TS
1const scheduler = new Scheduler({ logger: false }) // silent
2const routed = new Scheduler({
3 logger: { info: (m) => log.info(m), warn: (m) => log.warn(m) }
4})The default is the console. Task failures are always observable through the failed event, even when the logger is silent.
Cron format
minute hour day month weekday, with *, lists (1,15), ranges (1-5) and steps (*/15). When both day-of-month and weekday are restricted, a time matches if either matches (standard cron behaviour).
Cron helpers
Note
New in 0.3.3: the cron helpers are exported from @axiljs/queue.
TypeScript
TS
1import { parseCron, matchesCron, isValidCron, validateCron } from '@axiljs/queue'
2
3isValidCron('0 9 * * 1-5') // true
4isValidCron('61 * * * *') // false: no minute can ever match
5validateCron('0 9 * *') // error message string, or null when valid
6
7const cron = parseCron('*/15 9-17 * * 1-5') // ParsedCron; throws on invalid input
8matchesCron(new Date(), cron) // true during weekday office hours on the quarter hourWas this page helpful?