Nest Schedule
Description
This is a Nest module for using decorator schedule jobs.
Installation
$ npm i --save nest-schedule
Usage
;;
;; // Only support SINGLETON scope
Dynamic Schedule Job
;;
Distributed Support
1. Extend NestDistributedSchedule class
;;
2. Use UseLocker decorator
;; // If use NestCloud, it supports dependency injection.
;;;
API
class ScheduleModule
static register(config: IGlobalConfig): DynamicModule
Register schedule module.
field | type | required | description |
---|---|---|---|
config.enable | boolean | false | default is true, when false, the job will not execute |
config.maxRetry | number | false | the max retry count, default is -1 not retry |
config.retryInterval | number | false | the retry interval, default is 5000 |
config.waiting | boolean | false | the scheduler will not schedule job when this job is running, if waiting is true |
class Schedule
scheduleCronJob(key: string, cron: string, callback: JobCallback, config?: ICronJobConfig)
Schedule a cron job.
field | type | required | description |
---|---|---|---|
key | string | true | The unique job key |
cron | string | true | The cron expression |
callback | () => Promise<boolean> | boolean | If return true in callback function, the schedule will cancel this job immediately |
config.startTime | Date | false | The start time of this job |
config.endTime | Date | false | The end time of this job |
config.enable | boolean | false | default is true, when false, the job will not execute |
config.maxRetry | number | false | the max retry count, default is -1 not retry |
config.retryInterval | number | false | the retry interval, default is 5000 |
config.waiting | boolean | false | the scheduler will not schedule job when this job is running, if waiting is true |
config.immediate | boolean | false | running job immediately |
scheduleIntervalJob(key: string, interval: number, callback: JobCallback, config?: IJobConfig)
Schedule a interval job.
field | type | required | description |
---|---|---|---|
key | string | true | The unique job key |
interval | number | true | milliseconds |
callback | () => Promise<boolean> | boolean | If return true in callback function, the schedule will cancel this job immediately |
config.enable | boolean | false | default is true, when false, the job will not execute |
config.maxRetry | number | false | the max retry count, default is -1 not retry |
config.retryInterval | number | false | the retry interval, default is 5000 |
config.waiting | boolean | false | the scheduler will not schedule job when this job is running, if waiting is true |
config.immediate | boolean | false | running job immediately |
scheduleTimeoutJob(key: string, timeout: number, callback: JobCallback, config?: IJobConfig)
Schedule a timeout job.
field | type | required | description |
---|---|---|---|
key | string | true | The unique job key |
timeout | number | true | milliseconds |
callback | () => Promise<boolean> | boolean | If return true in callback function, the schedule will cancel this job immediately |
config.enable | boolean | false | default is true, when false, the job will not execute |
config.maxRetry | number | false | the max retry count, default is -1 not retry |
config.retryInterval | number | false | the retry interval, default is 5000 |
config.immediate | boolean | false | running job immediately |
cancelJob(key: string)
Cancel job.
Decorators
Cron(expression: string, config?: ICronJobConfig): MethodDecorator
Schedule a cron job.
field | type | required | description |
---|---|---|---|
expression | string | true | the cron expression |
config.key | string | false | The unique job key |
config.startTime | Date | false | the job's start time |
config.endTime | Date | false | the job's end time |
config.enable | boolean | false | default is true, when false, the job will not execute |
config.maxRetry | number | false | the max retry count, default is -1 not retry |
config.retryInterval | number | false | the retry interval, default is 5000 |
config.waiting | boolean | false | the scheduler will not schedule job when this job is running, if waiting is true |
config.immediate | boolean | false | running job immediately |
Interval(milliseconds: number, config?: IJobConfig): MethodDecorator
Schedule a interval job.
field | type | required | description |
---|---|---|---|
milliseconds | number | true | milliseconds |
config.key | string | false | The unique job key |
config.enable | boolean | false | default is true, when false, the job will not execute |
config.maxRetry | number | false | the max retry count, default is -1 not retry |
config.retryInterval | number | false | the retry interval, default is 5000 |
config.waiting | boolean | false | the scheduler will not schedule job when this job is running, if waiting is true |
config.immediate | boolean | false | running job immediately |
Timeout(milliseconds: number, config?: IJobConfig): MethodDecorator
Schedule a timeout job.
field | type | required | description |
---|---|---|---|
milliseconds | number | true | milliseconds |
config.key | string | false | The unique job key |
config.enable | boolean | false | default is true, when false, the job will not execute |
config.maxRetry | number | false | the max retry count, default is -1 not retry |
config.retryInterval | number | false | the retry interval, default is 5000 |
config.immediate | boolean | false | running job immediately |
InjectSchedule(): PropertyDecorator
Inject Schedule instance
UseLocker(locker: ILocker | Function): MethodDecorator
Make your job support distribution.
If you use NestCloud, the Locker will support dependency injection, or not use injection please.
Stay in touch
- Author - miaowing
License
- NestSchedule is MIT licensed.