Last active 1 month ago

Remove completed jobs older than 1 hour

app.module.js Raw
1// app.module.ts
2import { BullModule } from '@nestjs/bull';
3
4@Module({
5 imports: [
6 BullModule.forRoot({
7 redis: {
8 host: process.env.REDIS_HOST,
9 port: parseInt(process.env.REDIS_PORT),
10 password: process.env.REDIS_PASSWORD,
11 },
12 // ── Applied to ALL queues globally ──────────────────────────────
13 defaultJobOptions: {
14 removeOnComplete: {
15 age: 3600, // remove completed jobs older than 1 hour (seconds)
16 count: 100, // but always keep the last 100 regardless of age
17 },
18 removeOnFail: {
19 age: 86400, // keep failed jobs for 24 hours for debugging
20 count: 200,
21 },
22 attempts: 3,
23 backoff: {
24 type: 'exponential',
25 delay: 5000,
26 },
27 },
28 }),
29
30 // Individual queues — no need to repeat defaultJobOptions
31 BullModule.registerQueue({ name: 'helpers' }),
32 BullModule.registerQueue({ name: 'notification' }),
33 BullModule.registerQueue({ name: 'log' }),
34 BullModule.registerQueue({ name: 'optimize-image' }),
35 ],
36})
37export class AppModule {}
38
39
40// If you use forRootAsync (with ConfigService)
41BullModule.forRootAsync({
42 imports: [ConfigModule],
43 inject: [ConfigService],
44 useFactory: (config: ConfigService) => ({
45 connection: {
46 host: config.get('REDIS_HOST'),
47 port: config.get<number>('REDIS_PORT'),
48 password: config.get('REDIS_PASSWORD'),
49 db: config.get<number>('REDIS_DB', 0),
50 },
51 defaultJobOptions: {
52 removeOnComplete: { age: 3600, count: 100 },
53 removeOnFail: { age: 86400, count: 200 },
54 attempts: 3,
55 backoff: { type: 'exponential', delay: 5000 },
56 },
57 }),
58}),