Skip to content

Commit

Permalink
fix: remove the lazy client error handler on close (OptimalBits#1605)
Browse files Browse the repository at this point in the history
  • Loading branch information
lavarsicious authored and jtassin committed Jul 3, 2020
1 parent a11fd1a commit 10a7de8
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions lib/queue.js
Expand Up @@ -144,7 +144,9 @@ const Queue = function Queue(name, url, opts) {
this.clients = [];
const lazyClient = redisClientGetter(this, opts, (type, client) => {
// bubble up Redis error events
client.on('error', this.emit.bind(this, 'error'));
const handler = this.emit.bind(this, 'error');
client.on('error', handler);
this.once('close', () => client.removeListener('error', handler));

if (type === 'client') {
this._initializing = commands(client).then(
Expand Down Expand Up @@ -373,7 +375,7 @@ Queue.prototype._setupQueueEventListeners = function() {
const failedKey = this.keys.failed;
const drainedKey = this.keys.drained;

this.eclient.on('pmessage', (pattern, channel, message) => {
const pmessageHandler = (pattern, channel, message) => {
const keyAndToken = channel.split('@');
const key = keyAndToken[0];
const token = keyAndToken[1];
Expand All @@ -394,9 +396,9 @@ Queue.prototype._setupQueueEventListeners = function() {
this.emit('global:stalled', message);
break;
}
});
};

this.eclient.on('message', (channel, message) => {
const messageHandler = (channel, message) => {
const key = channel.split('@')[0];
switch (key) {
case progressKey: {
Expand Down Expand Up @@ -436,6 +438,14 @@ Queue.prototype._setupQueueEventListeners = function() {
this.emit('global:drained');
break;
}
};

this.eclient.on('pmessage', pmessageHandler);
this.eclient.on('message', messageHandler);

this.once('close', () => {
this.eclient.removeListener('pmessage', pmessageHandler);
this.eclient.removeListener('message', messageHandler);
});
};

Expand Down Expand Up @@ -561,6 +571,7 @@ Queue.prototype.close = function(doNotWaitJobs) {
.finally(() => {
this.childPool && this.childPool.clean();
this.closed = true;
this.emit('close');
}));
};

Expand Down

0 comments on commit 10a7de8

Please sign in to comment.