Skip to content

Commit

Permalink
feat: pass clientName to createClient function
Browse files Browse the repository at this point in the history
Overrides the options.redis settings for clientName for creating a new
client
  • Loading branch information
tebriel committed Jul 15, 2021
1 parent 6171f1a commit 2a29569
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
5 changes: 3 additions & 2 deletions PATTERNS.md
Expand Up @@ -82,14 +82,15 @@ const client = new Redis(REDIS_URL);
const subscriber = new Redis(REDIS_URL);

const opts = {
createClient: function (type) {
// redisOpts here will contain at least a property of connectionName which will identify the queue based on its name
createClient: function (type, redisOpts) {
switch (type) {
case 'client':
return client;
case 'subscriber':
return subscriber;
case 'bclient':
return new Redis(REDIS_URL);
return new Redis(REDIS_URL, redisOpts);
default:
throw new Error('Unexpected connection type: ', type);
}
Expand Down
4 changes: 3 additions & 1 deletion lib/queue.js
Expand Up @@ -277,7 +277,9 @@ function redisClientGetter(queue, options, initCallback) {
if (connections[type] != null) {
return connections[type];
}
const client = (connections[type] = createClient(type, options.redis));
const clientOptions = _.assign({}, options.redis);
clientOptions.connectionName = this.clientName();
const client = (connections[type] = createClient(type, clientOptions));

// Since connections are lazily initialized, we can't check queue.client
// without initializing a connection. So expose a boolean we can safely
Expand Down
5 changes: 5 additions & 0 deletions test/test_queue.js
Expand Up @@ -310,6 +310,11 @@ describe('Queue', () => {
expect(job.id).to.be.ok;
expect(job.data.foo).to.be.eql('bar');
})
.then(
queueFoo.bclient.client('GETNAME').then(name => {
expect(name).to.be.eql(queueFoo.clientName());
})
)
.then(() => {
return queueQux.add({ qux: 'baz' }).then(job => {
expect(job.id).to.be.ok;
Expand Down

0 comments on commit 2a29569

Please sign in to comment.