app.module.js
· 1.7 KiB · JavaScript
Raw
// app.module.ts
import { BullModule } from '@nestjs/bull';
@Module({
imports: [
BullModule.forRoot({
redis: {
host: process.env.REDIS_HOST,
port: parseInt(process.env.REDIS_PORT),
password: process.env.REDIS_PASSWORD,
},
// ── Applied to ALL queues globally ──────────────────────────────
defaultJobOptions: {
removeOnComplete: {
age: 3600, // remove completed jobs older than 1 hour (seconds)
count: 100, // but always keep the last 100 regardless of age
},
removeOnFail: {
age: 86400, // keep failed jobs for 24 hours for debugging
count: 200,
},
attempts: 3,
backoff: {
type: 'exponential',
delay: 5000,
},
},
}),
// Individual queues — no need to repeat defaultJobOptions
BullModule.registerQueue({ name: 'helpers' }),
BullModule.registerQueue({ name: 'notification' }),
BullModule.registerQueue({ name: 'log' }),
BullModule.registerQueue({ name: 'optimize-image' }),
],
})
export class AppModule {}
// If you use forRootAsync (with ConfigService)
BullModule.forRootAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: (config: ConfigService) => ({
connection: {
host: config.get('REDIS_HOST'),
port: config.get<number>('REDIS_PORT'),
password: config.get('REDIS_PASSWORD'),
db: config.get<number>('REDIS_DB', 0),
},
defaultJobOptions: {
removeOnComplete: { age: 3600, count: 100 },
removeOnFail: { age: 86400, count: 200 },
attempts: 3,
backoff: { type: 'exponential', delay: 5000 },
},
}),
}),
| 1 | // app.module.ts |
| 2 | import { 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 | }) |
| 37 | export class AppModule {} |
| 38 | |
| 39 | |
| 40 | // If you use forRootAsync (with ConfigService) |
| 41 | BullModule.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 | }), |