diff --git a/src/channel/redis.channel-config.ts b/src/channel/redis.channel-config.ts index 7d93c7b..6f6325f 100644 --- a/src/channel/redis.channel-config.ts +++ b/src/channel/redis.channel-config.ts @@ -1,4 +1,5 @@ import { ChannelConfig } from '@nestjstools/messaging'; +import { KeepJobs } from 'bullmq'; export class RedisChannelConfig extends ChannelConfig { public readonly connection: Connection; @@ -9,6 +10,7 @@ export class RedisChannelConfig extends ChannelConfig { * Read more: https://github.com/taskforcesh/bullmq/issues/1219#issuecomment-1113903785 */ public readonly keyPrefix?: string; + public readonly bullJobOptions?: BullJobOptions; constructor({ name, @@ -19,6 +21,7 @@ export class RedisChannelConfig extends ChannelConfig { middlewares, normalizer, keyPrefix, + bullJobOptions, }: RedisChannelConfig) { super( name, @@ -30,6 +33,7 @@ export class RedisChannelConfig extends ChannelConfig { this.connection = connection; this.queue = queue; this.keyPrefix = keyPrefix; + this.bullJobOptions = bullJobOptions; } } @@ -39,3 +43,22 @@ interface Connection { password?: string; db?: number; } + +interface BullJobOptions { + /** + * If true, removes the job when it successfully completes + * When given a number, it specifies the maximum amount of + * jobs to keep, or you can provide an object specifying max + * age and/or count to keep. It overrides whatever setting is used in the worker. + * Default behavior is to keep the job in the completed set. + */ + removeOnComplete?: number | boolean | KeepJobs; + /** + * If true, removes the job when it fails after all attempts. + * When given a number, it specifies the maximum amount of + * jobs to keep, or you can provide an object specifying max + * age and/or count to keep. It overrides whatever setting is used in the worker. + * Default behavior is to keep the job in the failed set. + */ + removeOnFail?: number | boolean | KeepJobs; +} diff --git a/src/channel/redis.channel.ts b/src/channel/redis.channel.ts index 5187edc..2dcb26f 100644 --- a/src/channel/redis.channel.ts +++ b/src/channel/redis.channel.ts @@ -11,6 +11,10 @@ export class RedisChannel extends Channel { this.queue = new Queue(config.queue, { connection: this.config.connection, prefix: config.keyPrefix, + defaultJobOptions: { + removeOnComplete: config.bullJobOptions?.removeOnComplete, + removeOnFail: config.bullJobOptions?.removeOnFail, + }, }); } }