A progressive Node.js framework for building efficient and scalable server-side applications, heavily inspired by Angular.
Description
This is a Nest module for using decorator schedule a job.
Installation
$ npm i --save nest-schedule
Quick Start
import { Injectable } from '@nestjs/common';
import { Cron, Interval, Timeout, NestSchedule } from 'nest-schedule';
@Injectable()
export class ScheduleService extends NestSchedule {
constructor() {
super();
}
@Cron('0 0 2 * *', {
startTime: new Date(),
endTime: new Date(new Date().getTime() + 24 * 60 * 60 * 1000),
tz: 'Asia/Shanghai',
})
async syncData() {
console.log('syncing data ...');
}
@Cron('0 0 4 * *')
async clear() {
console.log('clear data ...');
await doClear();
}
@Timeout(5000)
onceJob() {
console.log('once job');
}
@Interval(2000)
intervalJob() {
console.log('interval job');
// if you want to cancel the job, you should return true;
return true;
}
}
Distributed Support
import { Injectable } from '@nestjs/common';
import { Cron, NestDistributedSchedule } from 'nest-schedule';
@Injectable()
export class ScheduleService extends NestDistributedSchedule {
constructor() {
super();
}
async tryLock(method: string) {
// If try lock fail, you should throw an error.
throw new Error('try lock fail');
return () => {
// Release here.
}
}
@Cron('0 0 4 * *')
async clear() {
console.log('clear data ...');
}
}
Stay in touch
- Author - Miaowing
License
Nest is MIT licensed.