This schedule module is forked from @nestjs/schedule.
And add some new features:
-
Distributed supports that by using
UseLocker()
decorator. -
Retryable Job.
-
Executing job immediately for
Interval
andTimeout
job.
$ npm i --save @nestcloud/schedule
import { Module } from '@nestjs/common';
import { ScheduleModule } from '@nestcloud/schedule';
@Module({
imports: [
ScheduleModule.forRoot(),
]
})
export class AppModule {
}
import { Injectable, Logger } from '@nestjs/common';
import { Cron, Timeout, Interval } from '@nestcloud/schedule';
@Injectable()
export class TasksService {
private readonly logger = new Logger(TasksService.name);
@Cron('45 * * * * *')
handleCron() {
this.logger.debug('Called when the current second is 45');
}
@Interval(5000)
handleInterval() {
this.logger.debug('Called every 5 seconds');
}
@Timeout(5000)
handleTimeout() {
this.logger.debug('Called after 5 seconds');
}
}
@See node-schedule#object-literal-syntax
import { Injectable, Logger } from '@nestjs/common';
import { Cron } from '@nestcloud/schedule';
@Injectable()
export class TasksService {
private readonly logger = new Logger(TasksService.name);
@Cron({
rule: '45 * * * * *',
start: new Date(Date.now() + 5000),
end: new Date(Date.now() + 10000),
tz: 'Asia/Shanghai'
})
handleCron() {
this.logger.debug('Called when the time is Sunday 14:30');
}
}
@See node-schedule#set-startTime-and-endTime
import { Injectable, Logger } from '@nestjs/common';
import { Cron } from '@nestcloud/schedule';
@Injectable()
export class TasksService {
private readonly logger = new Logger(TasksService.name);
@Cron({ hour: 14, minute: 30, dayOfWeek: 0, tz: 'Asia/Shanghai' })
handleCron() {
this.logger.debug('Called when the current second is 45');
}
}
import { Injectable, Logger, OnModuleInit } from '@nestjs/common';
import { InjectSchedule, Schedule } from '@nestcloud/schedule';
@Injectable()
export class TasksService implements OnModuleInit {
private readonly logger = new Logger(TasksService.name);
constructor(
@InjectSchedule() private readonly schedule: Schedule,
) {
}
execute() {
this.logger.debug('execute dynamic job');
}
onModuleInit() {
this.schedule.createIntervalJob(this.execute.bind(this), 3000, {name: 'test_job'});
this.schedule.deleteIntervalJob('test_job');
}
}
Dynamic job is not support distributed locker now.
- Implements
Locker
interface
import { Locker } from '@nestcloud/schedule';
import { Injectable } from '@nestjs/common';
@Injectable()
export class TaskLocker implements Locker {
private name: string;
init(name: string): void {
this.name = name;
}
release(): any {
}
async tryLock(): Promise<boolean> {
return true;
}
}
- Use your locker
import { Injectable, Logger } from '@nestjs/common';
import { Cron, UseLocker } from '@nestcloud/schedule';
import { TaskLocker } from './TaskLocker';
@Injectable()
export class TasksService {
private readonly logger = new Logger(TasksService.name);
@Cron('45 * * * * *')
@UseLocker(TaskLocker)
handleCron() {
this.logger.debug('Called when the current second is 45');
}
}
Import schedule module.
Dynamic create a timeout job.
field | type | required | description |
---|---|---|---|
methodRef | Function | true | job method |
timeout | number | true | milliseconds |
options | false | see decorators |
Dynamic create a interval job.
field | type | required | description |
---|---|---|---|
methodRef | Function | true | job method |
timeout | number | true | milliseconds |
options | false | see decorators |
createCronJob(rule: string | number | Date | CronObject | CronObjLiteral, methodRef, options?: CronOptions)
Dynamic create a cron job.
field | type | required | description |
---|---|---|---|
rule | Date string number CronObject CronObjLiteral | true | the cron rule |
methodRef | Function | true | job method |
options | false | see decorators |
Delete a timeout job
Delete a interval job
Delete a cron job
Get all timeout jobs
Get all interval jobs
Get all cron jobs
Cron(rule: string | number | Date | CronObject | CronObjLiteral, options?: CronOptions): MethodDecorator
Schedule a cron job.
field | type | required | description |
---|---|---|---|
rule | Date string number CronObject CronObjLiteral | true | The cron rule |
rule.dayOfWeek | number | true | Timezone |
options.name | string | false | The unique job key |
options.retries | number | false | the max retry count, default is -1 not retry |
options.retry | number | false | the retry interval, default is 5000 |
Schedule a interval job.
field | type | required | description |
---|---|---|---|
timeout | number | true | milliseconds |
options.retries | number | false | the max retry count, default is -1 not retry |
options.retry | number | false | the retry interval, default is 5000 |
options.immediate | boolean | false | executing job immediately |
Schedule a timeout job.
field | type | required | description |
---|---|---|---|
timeout | number | true | milliseconds |
options.retries | number | false | the max retry count, default is -1 not retry |
options.retry | number | false | the retry interval, default is 5000 |
options.immediate | boolean | false | executing job immediately |
Inject Schedule instance
Set a distributed locker for job.
- Author - NestCloud
NestCloud is MIT licensed.