Skip to content

feat: enhance RedisChannelConfig with BullJobOptions for job management #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/channel/redis.channel-config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ChannelConfig } from '@nestjstools/messaging';
import { KeepJobs } from 'bullmq';

export class RedisChannelConfig extends ChannelConfig {
public readonly connection: Connection;
Expand All @@ -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,
Expand All @@ -19,6 +21,7 @@ export class RedisChannelConfig extends ChannelConfig {
middlewares,
normalizer,
keyPrefix,
bullJobOptions,
}: RedisChannelConfig) {
super(
name,
Expand All @@ -30,6 +33,7 @@ export class RedisChannelConfig extends ChannelConfig {
this.connection = connection;
this.queue = queue;
this.keyPrefix = keyPrefix;
this.bullJobOptions = bullJobOptions;
}
}

Expand All @@ -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;
}
4 changes: 4 additions & 0 deletions src/channel/redis.channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ export class RedisChannel extends Channel<RedisChannelConfig> {
this.queue = new Queue(config.queue, {
connection: this.config.connection,
prefix: config.keyPrefix,
defaultJobOptions: {
removeOnComplete: config.bullJobOptions?.removeOnComplete,
removeOnFail: config.bullJobOptions?.removeOnFail,
},
});
}
}