Skip to main content

Schedule actions

There are many cases in which you want to trigger some action periodically. For example, you may want to send a reminder email to a user every day at 10:00 AM. For this, you can use scheduled commands.

Scheduled command

Commands represent an action. Therefore, the way to trigger an action periodically is by scheduling a command. Scheduled commands are the way to add automated tasks to your application, like cron jobs in other frameworks. Booster scheduled commands are TypeScript classes decorated with @ScheduledCommand. Unlike conventional commands, the handle function doesn't have any parameters.

In Booster, a scheduled command looks like this:

@ScheduledCommand({
minute: '0/5', // runs every 5 minutes
})
export class CheckCartCount {
public static async handle(): Promise<void> {
/* YOUR CODE HERE */
}
}

You can pass the following parameters to the @ScheduledCommand decorator:

  • minute: A cron expression to specify the minute(s) in which the command will be triggered. For example, 0/5 means "every 5 minutes". You can also use a comma-separated list of values, like 1,5,10,15,20,25,30,35,40,45,50,55 to specify a list of minutes.
  • hour: A cron expression to specify the hour(s) in which the command will be triggered. For example, 0/2 means "every 2 hours". You can also use a comma-separated list of values, like 1,3,5,7,9,11,13,15,17,19,21,23 to specify a list of hours.
  • day: A cron expression to specify the day(s) in which the command will be triggered. For example, 1/2 means "every 2 days". You can also use a comma-separated list of values, like 1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31 to specify a list of days.
  • month: A cron expression to specify the month(s) in which the command will be triggered. For example, 1/2 means "every 2 months". You can also use a comma-separated list of values, like 1,3,5,7,9,11 to specify a list of months.
  • weekDay: A cron expression to specify the day(s) of the week in which the command will be triggered. For example, 1/2 means "every 2 days of the week". You can also use a comma-separated list of values, like 1,3,5 to specify a list of days of the week.
  • year: A cron expression to specify the year(s) in which the command will be triggered. For example, 2020/2 means "every 2 years". You can also use a comma-separated list of values, like 2020,2022,2024,2026,2028,2030 to specify a list of years.

By default, if no paramaters are passed, the scheduled command will not be triggered.

Creating a scheduled command

The preferred way to create a scheduled command is by using the generator, e.g.

boost new:scheduled-command CheckCartCount