🦅 NestJS Intermediate

What is the @nestjs/schedule module?

Answer

The @nestjs/schedule module enables cron jobs and scheduled tasks within a NestJS application. Import ScheduleModule.forRoot(). Decorate methods in a service with scheduling decorators: @Cron(CronExpression.EVERY_DAY_AT_MIDNIGHT) runs a method every midnight. @Cron('0 30 11 * * 1-5') uses a custom cron expression. @Interval(10000) runs every 10 seconds. @Timeout(5000) runs once after a 5-second delay. Jobs are registered at startup and managed by the scheduler. Access and control jobs programmatically with the SchedulerRegistry. In distributed environments (multiple instances), only one instance should run scheduled jobs — use a lock mechanism (e.g., Redis) to prevent duplicate execution.