Task Scheduling

Task scheduling allows you to schedule arbitrary code (methods/functions) to execute at a fixed date/time, at recurring intervals, or once after a specified interval. In the Linux world, this is often handled by packages like cron at the OS level. For Node.js apps, there are several packages that emulate cron-like functionality. Nest provides the @nestjs/schedule package, which integrates with the popular Node.js node-cron package. We’ll cover this package in the current chapter.

Installation

To begin using it, we first install the required dependencies.

  1. $ npm install --save @nestjs/schedule

To activate job scheduling, import the ScheduleModule into the root AppModule and run the forRoot() static method as shown below:

app.module.ts

  1. import { Module } from '@nestjs/common';
  2. import { ScheduleModule } from '@nestjs/schedule';
  3. @Module({
  4. imports: [
  5. ScheduleModule.forRoot()
  6. ],
  7. })
  8. export class AppModule {}

The .forRoot() call initializes the scheduler and registers any declarative cron jobs, timeouts and intervals that exist within your app. Registration occurs when the onApplicationBootstrap lifecycle hook occurs, ensuring that all modules have loaded and declared any scheduled jobs.

Declarative cron jobs

A cron job schedules an arbitrary function (method call) to run automatically. Cron jobs can run:

  • Once, at a specified date/time.
  • On a recurring basis; recurring jobs can run at a specified instant within a specified interval (for example, once per hour, once per week, once every 5 minutes)

Declare a cron job with the @Cron() decorator preceding the method definition containing the code to be executed, as follows:

  1. import { Injectable, Logger } from '@nestjs/common';
  2. import { Cron } from '@nestjs/schedule';
  3. @Injectable()
  4. export class TasksService {
  5. private readonly logger = new Logger(TasksService.name);
  6. @Cron('45 * * * * *')
  7. handleCron() {
  8. this.logger.debug('Called when the current second is 45');
  9. }
  10. }

In this example, the handleCron() method will be called each time the current second is 45. In other words, the method will be run once per minute, at the 45 second mark.

The @Cron() decorator supports all standard cron patterns:

  • Asterisk (e.g. *)
  • Ranges (e.g. 1-3,5)
  • Steps (e.g. */2)

In the example above, we passed 45 * * * * * to the decorator. The following key shows how each position in the cron pattern string is interpreted:

  1. * * * * * *
  2. | | | | | |
  3. | | | | | day of week
  4. | | | | month
  5. | | | day of month
  6. | | hour
  7. | minute
  8. second (optional)

Some sample cron patterns are:

every second
45 every minute, on the 45th second
10 every hour, at the start of the 10th minute
0 /30 9-17 every 30 minutes between 9am and 5pm
0 30 11 1-5Monday to Friday at 11:30am

The @nestjs/schedule package provides a convenience enum with commonly used cron patterns. You can use this enum as follows:

  1. import { Injectable, Logger } from '@nestjs/common';
  2. import { Cron, CronExpression } from '@nestjs/schedule';
  3. @Injectable()
  4. export class TasksService {
  5. private readonly logger = new Logger(TasksService.name);
  6. @Cron(CronExpression.EVERY_45_SECONDS)
  7. handleCron() {
  8. this.logger.debug('Called every 45 seconds');
  9. }
  10. }

In this example, the handleCron() method will be called every 45 seconds.

Alternatively, you can supply a JavaScript Date object to the @Cron() decorator. Doing so causes the job to execute exactly once, at the specified date.

Hint Use JavaScript date arithmetic to schedule jobs relative to the current date. For example, @Cron(new Date(Date.now() + 10 * 1000)) to schedule a job to run 10 seconds after the app starts.

You can access and control a cron job after it’s been declared, or dynamically create a cron job (where its cron pattern is defined at runtime) with the Dynamic API. To access a declarative cron job via the API, you must associate the job with a name by passing the name property in an optional options object as the second argument of the decorator, as shown below:

  1. @Cron('* * 8 * * *', {
  2. name: 'notifications',
  3. })
  4. triggerNotifications() {}

Declarative intervals

To declare that a method should run at a (recurring) specified interval, prefix the method definition with the @Interval() decorator. Pass the interval value, as a number in milliseconds, to the decorator as shown below:

  1. @Interval(10000)
  2. handleInterval() {
  3. this.logger.debug('Called every 10 seconds');
  4. }

Hint This mechanism uses the JavaScript setInterval() function under the hood. You can also utilize a cron job to schedule recurring jobs.

If you want to control your declarative interval from outside the declaring class via the Dynamic API, associate the interval with a name using the following construction:

  1. @Interval('notifications', 2500)
  2. handleInterval() {}

The Dynamic API also enables creating dynamic intervals, where the interval’s properties are defined at runtime, and listing and deleting them.

Official enterprise support

  • Task scheduling - 图1 Providing technical guidance
  • Task scheduling - 图2 Performing in-depth code reviews
  • Task scheduling - 图3 Mentoring team members
  • Task scheduling - 图4 Advising best practices

Explore more

Declarative timeouts

To declare that a method should run (once) at a specified timeout, prefix the method definition with the @Timeout() decorator. Pass the relative time offset (in milliseconds), from application startup, to the decorator as shown below:

  1. @Timeout(5000)
  2. handleTimeout() {
  3. this.logger.debug('Called once after 5 seconds');
  4. }

Hint This mechanism uses the JavaScript setTimeout() function under the hood.

If you want to control your declarative timeout from outside the declaring class via the Dynamic API, associate the timeout with a name using the following construction:

  1. @Timeout('notifications', 2500)
  2. handleTimeout() {}

The Dynamic API also enables creating dynamic timeouts, where the timeout’s properties are defined at runtime, and listing and deleting them.

Dynamic schedule module API

The @nestjs/schedule module provides a dynamic API that enables managing declarative cron jobs, timeouts and intervals. The API also enables creating and managing dynamic cron jobs, timeouts and intervals, where the properties are defined at runtime.

Dynamic cron jobs

Obtain a reference to a CronJob instance by name from anywhere in your code using the SchedulerRegistry API. First, inject SchedulerRegistry using standard constructor injection:

  1. constructor(private schedulerRegistry: SchedulerRegistry) {}

Hint Import the SchedulerRegistry from the @nestjs/schedule package.

Then use it in a class as follows. Assume a cron job was created with the following declaration:

  1. @Cron('* * 8 * * *', {
  2. name: 'notifications',
  3. })
  4. triggerNotifications() {}

Access this job using the following:

  1. const job = this.schedulerRegistry.getCronJob('notifications');
  2. job.stop();
  3. console.log(job.lastDate());

The getCronJob() method returns the named cron job. The returned CronJob object has the following methods:

  • stop() - stops a job that is scheduled to run.
  • start() - restarts a job that has been stopped.
  • setTime(time: CronTime) - stops a job, sets a new time for it, and then starts it
  • lastDate() - returns a string representation of the last date a job executed
  • nextDates(count: number) - returns an array (size count) of moment objects representing upcoming job execution dates.

Hint Use toDate() on moment objects to render them in human readable form.

Create a new cron job dynamically using the SchedulerRegistry.addCronJob() method, as follows:

  1. addCronJob(name: string, seconds: string) {
  2. const job = new CronJob(`${seconds} * * * * *`, () => {
  3. this.logger.warn(`time (${seconds}) for job ${name} to run!`);
  4. });
  5. this.scheduler.addCronJob(name, job);
  6. job.start();
  7. this.logger.warn(
  8. `job ${name} added for each minute at ${seconds} seconds!`,
  9. );
  10. }

In this code, we use the CronJob object from the cron package to create the cron job. The CronJob constructor takes a cron pattern (just like the @Cron()decorator) as its first argument, and a callback to be executed when the cron timer fires as its second argument. The SchedulerRegistry.addCronJob() method takes two arguments: a name for the CronJob, and the CronJob object itself.

Warning Remember to inject the SchedulerRegistry before accessing it. Import CronJob from the cron package.

Delete a named cron job using the SchedulerRegistry.deleteCronJob() method, as follows:

  1. deleteCron(name: string) {
  2. this.scheduler.deleteCronJob(name);
  3. this.logger.warn(`job ${name} deleted!`);
  4. }

List all cron jobs using the SchedulerRegistry.getCronJobs() method as follows:

  1. getCrons() {
  2. const jobs = this.scheduler.getCronJobs();
  3. jobs.forEach((value, key, map) => {
  4. let next;
  5. try {
  6. next = value.nextDates().toDate();
  7. } catch (e) {
  8. next = 'error: next fire date is in the past!';
  9. }
  10. this.logger.log(`job: ${key} -> next: ${next}`);
  11. });
  12. }

The getCronJobs() method returns a map. In this code, we iterate over the map and attempt to access the nextDates() method of each CronJob. In the CronJob API, if a job has already fired and has no future firing dates, it throws an exception.

Dynamic intervals

Obtain a reference to an interval with the SchedulerRegistry.getInterval() method. As above, inject SchedulerRegistry using standard constructor injection:

  1. constructor(private schedulerRegistry: SchedulerRegistry) {}

And use it as follows:

  1. const interval = this.schedulerRegistry.getInterval('notifications');
  2. clearInterval(interval);

Create a new interval dynamically using the SchedulerRegistry.addInterval() method, as follows:

  1. addInterval(name: string, seconds: string) {
  2. const callback = () => {
  3. this.logger.warn(`Interval ${name} executing at time (${seconds})!`);
  4. };
  5. const interval = setInterval(callback, seconds);
  6. this.scheduler.addInterval(name, interval);
  7. }

In this code, we create a standard JavaScript interval, then pass it to the ScheduleRegistry.addInterval() method. That method takes two arguments: a name for the interval, and the interval itself.

Delete a named interval using the SchedulerRegistry.deleteInterval() method, as follows:

  1. deleteInterval(name: string) {
  2. this.scheduler.deleteInterval(name);
  3. this.logger.warn(`Interval ${name} deleted!`);
  4. }

List all intervals using the SchedulerRegistry.getIntervals() method as follows:

  1. getIntervals() {
  2. const intervals = this.scheduler.getIntervals();
  3. intervals.forEach(key => this.logger.log(`Interval: ${key}`));
  4. }

Dynamic timeouts

Obtain a reference to a timeout with the SchedulerRegistry.getTimeout() method. As above, inject SchedulerRegistry using standard constructor injection:

  1. constructor(private schedulerRegistry: SchedulerRegistry) {}

And use it as follows:

  1. const timeout = this.schedulerRegistry.getTimeout('notifications');
  2. clearTimeout(timeout);

Create a new timeout dynamically using the SchedulerRegistry.addTimeout() method, as follows:

  1. addTimeout(name: string, seconds: string) {
  2. const callback = () => {
  3. this.logger.warn(`Timeout ${name} executing after (${seconds})!`);
  4. });
  5. const timeout = setTimeout(callback, seconds);
  6. this.scheduler.addTimeout(name, timeout);
  7. }

In this code, we create a standard JavaScript timeout, then pass it to the ScheduleRegistry.addTimeout() method. That method takes two arguments: a name for the timeout, and the timeout itself.

Delete a named timeout using the SchedulerRegistry.deleteTimeout() method, as follows:

  1. deleteTimeout(name: string) {
  2. this.scheduler.deleteTimeout(name);
  3. this.logger.warn(`Timeout ${name} deleted!`);
  4. }

List all timeouts using the SchedulerRegistry.getTimeouts() method as follows:

  1. getTimeouts() {
  2. const timeouts = this.scheduler.getTimeouts();
  3. timeouts.forEach(key => this.logger.log(`Timeout: ${key}`));
  4. }

Example

A working example is available here.