nest js cron job
xxxxxxxxxx
javascript
Copy code
import { Injectable } from '@nestjs/common';
import * as cron from 'node-cron';
@Injectable()
export class MyService {
constructor() {
cron.schedule('*/60 * * * *', () => {
this.myMethod();
});
}
async myMethod() {
// your code here
}
}
xxxxxxxxxx
* * * * * *
| | | | | |
| | | | | day of week
| | | | months
| | | day of month
| | hours
| minutes
seconds (optional)
//Some sample cron patterns are:
* * * * * * every second
45 * * * * * every minute, on the 45th second
0 10 * * * * every hour, at the start of the 10th minute
0 */30 9-17 * * * every 30 minutes between 9am and 5pm
0 30 11 * * 1-5 Monday to Friday at 11:30am
xxxxxxxxxx
javascript
Copy code
import { Injectable } from '@nestjs/common';
import * as cron from 'node-cron';
@Injectable()
export class MyService {
constructor() {
cron.schedule('*/60 * * * *', () => {
this.myMethod();
});
}
async myMethod() {
// your code here
}
}