Application
Task scheduling
Run code at a time or on a rhythm: every night, every 30 seconds, once after start.
A scheduled task is a method Kest calls for you at set times: archive old orders at 2 a.m., poll an API every 30 seconds, warm a cache five seconds after start-up. schedule is @nestjs/schedule.
@Injectable
class Jobs(private val orders: OrderService) {
@Cron("0 0 2 * * *", timeZone = "Europe/London") fun nightly() { orders.archive() } // every day at 02:00 London time
@Interval(30_000) fun poll() { ... } // every 30 seconds
@Timeout(5_000) fun once() { ... } // once, 5 seconds after start
@Cron("*/10 * * * * *", name = "sync", allowOverlap = false) fun sync() { ... } // every 10 seconds, named so you can control it
}| Annotation | Runs |
|---|---|
@Cron("expression") | at the times a cron expression describes |
@Interval(millis) | repeatedly, with that gap between starts |
@Timeout(millis) | once, that long after start-up |
Reading a cron expression
A cron expression has 5 or 6 fields:
┌───────────── second (0-59) optional: with 5 fields it is left out
│ ┌─────────── minute (0-59)
│ │ ┌───────── hour (0-23)
│ │ │ ┌─────── day of month (1-31)
│ │ │ │ ┌───── month (1-12 or JAN-DEC)
│ │ │ │ │ ┌─── day of week (0-7 or MON-SUN)
0 0 2 * * *| Expression | Means |
|---|---|
0 0 2 * * * | every day at 02:00:00 |
*/10 * * * * * | every 10 seconds |
0 */5 * * * * | every 5 minutes |
0 0 9 * * MON-FRI | weekdays at 09:00 |
0 30 8 1 * * | the 1st of each month at 08:30 |
* means "every", */n "every n", a-b a range, a,b a list. CronExpression.EVERY_5_MINUTES and friends are ready-made strings. Set timeZone or the schedule follows the server's zone, which surprises people when servers move.
Overlap
If a run is still going when the next is due, the new run is skipped by default (allowOverlap = false), so a slow job never piles up on itself. Set allowOverlap = true if concurrent runs are fine.
Controlling jobs at run time
SchedulerRegistry lets you add, stop and delete jobs while the application runs (look a job up by its name).
Every instance runs it
Scheduled methods run on every instance of the application. With three instances, nightly() runs three times. To run once per cluster, either wrap the body in a distributed lock (locks.ifFree("nightly") { ... } or @Locked("nightly", skipIfLocked = true)), or make it a repeatable queue job, whose occurrences are claimed in Redis so exactly one instance runs each.
Failures
An exception in a task is logged and reported to error monitoring (source=schedule, job=<name>); the schedule continues, so one bad run does not stop the job. Put retry logic inside the method if a failed run must be repeated.
Good practice
- Keep the method short: call a service, so the logic can be tested without a clock.
- Make tasks idempotent: a task that crashed halfway and runs again must not do harm.
- Choose names (
name = "nightly-archive") so logs and alerts say which job. - For a durable multi-step process with waiting, use a workflow, not a cron.