Creating a Job
In the Schedule Plugin, you can create jobs by 2 different ways. The first way is via decorators (@Cron, @Interval, @Date, @Timeout), and the other way is dynamically vía SchedulerService. Below are examples of both methods.
Decorator-based Job Creation
First, if you are going to use decorators, you will need to decorate your class with the @Scheduler() decorator. This decorator is used to mark a class as a scheduler, allowing it to contain job methods that can be scheduled and executed by the Schedule Plugin.
import { Scheduler, Cron, Interval, Date, Timeout } from '@axispark/schedule';
@Scheduler()
export class SchedulerExample {
/** Creates a cron job that runs every 5 seconds */
@Cron({ value: '*/5 * * * * *', name: 'every5SecondsJob', disabled: true })
handleCronJob() {...}
/** Creates an interval job that runs every 10 seconds */
@Interval(10000)
handleIntervalJob() {...}
/** Creates a date job that runs at a specific date and time */
@Date(new Date('2023-01-01T00:00:00Z'))
handleDateJob() {...}
/** Creates a timeout job that runs after 5 seconds from start */
@Timeout(5000)
handleTimeoutJob() {...
}
You can also provide additional options to the decorators, such as
nameanddisabled. Thenameoption allows you to specify a unique name for the job, which can be useful for identifying and managing jobs. Thedisabledoption allows you to disable a job, preventing it from being executed until it is enabled again.
When no name is provided for a job, the Schedule Plugin will automatically generate a unique name for the job based on the method name and the class name. This ensures that each job has a unique identifier, even if you don't explicitly provide a name. Example:
schedulerexample.handlecronjobwill be the generated name for thehandleCronJobmethod in theSchedulerExampleclass.
Dynamic Job Creation
Sometimes, you may want to create jobs dynamically at runtime. The Schedule Plugin provides the SchedulerService for this purpose. You can use the startJob method of the SchedulerService to create and start jobs dynamically. There are 4 different job definitions that you can use to create jobs dynamically: CronJobDefinition, IntervalJobDefinition, DateJobDefinition, and TimeoutJobDefinition. Each job definition requires a unique name, a schedule (cron expression, interval duration, date, or timeout duration), and a callback function that contains the logic to be executed when the job runs.
import { Injectable } from '@nestjs/di';
import { SchedulerService } from '@axispark/schedule';
@Injectable()
export class DynamicJobExample {
constructor(private readonly schedulerService: SchedulerService) {}
/** Creates a cron job that runs every 5 seconds */
createCronJob() {
this.schedulerService.startJob(
new CronJobDefinition('everyFiveSecondsCronJob', '*/5 * * * * *', async () => {
...job logic here...
})
);
}
/** Creates an interval job that runs every 10 seconds */
createIntervalJob() {
this.schedulerService.startJob(
new IntervalJobDefinition('everyTenSecondsIntervalJob', 10000, async () => {
...job logic here...
})
);
}
/** Creates a date job that runs at a specific date and time */
createDateJob() {
this.schedulerService.startJob(
new DateJobDefinition('specificDateJob', new Date('2023-01-01T00:00:00Z'), async () => {
...job logic here...
})
);
}
/** Creates a timeout job that runs after 5 seconds from start */
createTimeoutJob() {
this.schedulerService.startJob(
new TimeoutJobDefinition('fiveSecondsTimeoutJob', 5000, async () => {
...job logic here...
})
);
}
}