1.0.11 • Published 5 years ago
nestjs-rmq-test v1.0.11
NestJS cron Module
NestJS cron package allows you easily setup cron for your controllers or services.
npm i nestjs-cron
Then register module in your root app.module
import { CronModule } from 'nestjs-cron';
@Module({
imports: [
// ...
CronModule.forRoot(),
],
})
export class AppModule {}
To use cron, decorate your class with @Scheduled()
and method with @Cron()
import { Cron, Scheduled } from 'nestjs-cron';
@Scheduled()
export class MyClass {
@Cron('* * * * * *')
async myMethod() {
//...
}
}
'* * * * * *'
- is a standart cron notation. In this example it will be triggered every second.
Additionaly you can use options:
@Cron('* * * * * *', {
launchOnInit: true,
sync: true,
})
- launchOnInit - Launch job one time right after start
- sync - Wait for method to finish before launching next tick if your function takes more time then cron.