Skip to content

Latest commit

History

History
94 lines (68 loc) 路 2.88 KB

File metadata and controls

94 lines (68 loc) 路 2.88 KB
NestJS Redis Toolkit Logo

@nestjs-redis/schedule

Drop-in replacement for @nestjs/schedule with Redis-backed distributed cron execution

npm version npm downloads License: MIT TypeScript NestJS Redis


Features

  • Drop-in replacement - Same @Cron, @Interval, @Timeout decorators 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/client connections

Installation

npm install @nestjs-redis/schedule

Quick Start

// 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
  }
}

Migrating from @nestjs/schedule

  1. Swap the import:
-import { ScheduleModule, Cron } from '@nestjs/schedule';
+import { ScheduleModule, Cron } from '@nestjs-redis/schedule';
  1. 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: @Interval and @Timeout use native Node.js timers and run on every instance, identical to @nestjs/schedule. Only @Cron jobs are distributed via Redis.

Links

Contributing

Please see the root contributing guidelines.

License

MIT 漏 CSenshi