- Drop-in replacement - Same
@Cron,@Interval,@Timeoutdecorators and other APIs as@nestjs/schedule - Distributed cron execution - Redis locking guarantees a job fires on exactly one instance per tick
- Redis persistence for cron jobs - schedules survive process restarts
- Works with existing
@nestjs-redis/clientconnections
npm install @nestjs-redis/schedule// app.module.ts
@Module({
imports: [
RedisModule.forRoot({ options: { url: 'redis://localhost:6379' } }),
ScheduleModule.forRootAsync({
inject: [RedisToken()],
useFactory: (client) => ({ client }),
}),
],
})
export class AppModule {}// tasks.service.ts
@Injectable()
export class TasksService {
@Cron(CronExpression.EVERY_MINUTE)
handleCron() {
// runs on exactly one instance per tick, even with many replicas
}
}- Swap the import:
-import { ScheduleModule, Cron } from '@nestjs/schedule';
+import { ScheduleModule, Cron } from '@nestjs-redis/schedule';- Pass a Redis client:
-ScheduleModule.forRoot()
+ScheduleModule.forRootAsync({
+ inject: [RedisToken()],
+ useFactory: (client) => ({ client }),
+})For full API documentation refer to the official @nestjs/schedule docs.
Note:
@Intervaland@Timeoutuse native Node.js timers and run on every instance, identical to@nestjs/schedule. Only@Cronjobs are distributed via Redis.
- Root repo: CSenshi/nestjs-redis
- Issues: GitHub Issues
- Discussions: GitHub Discussions
Please see the root contributing guidelines.
MIT 漏 CSenshi
