Skip to content

Commit

Permalink
fix: whenCurrentJobsFinished shouldn't initialize bclient
Browse files Browse the repository at this point in the history
  • Loading branch information
gabegorelick committed Jun 11, 2019
1 parent c0ee6be commit 3e082c0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/queue.js
Expand Up @@ -279,6 +279,12 @@ function redisClientGetter(queue, options, initCallback) {
return connections[type];
}
const client = (connections[type] = createClient(type, options.redis));

// Since connections are lazily initialized, we can't check queue.client
// without initializing a connection. So expose a boolean we can safely
// query.
queue[type + 'Initialized'] = true;

if (!options.createClient) {
queue.clients.push(client);
}
Expand Down Expand Up @@ -1139,6 +1145,11 @@ Queue.prototype.clean = function(grace, type, limit) {
*/
Queue.prototype.whenCurrentJobsFinished = function() {
return new Promise((resolve, reject) => {
if (!this.bclientInitialized) {
// bclient not yet initialized, so no jobs to wait for
return resolve();
}

//
// Force reconnection of blocking connection to abort blocking redis call immediately.
//
Expand Down
13 changes: 13 additions & 0 deletions test/test_pause.js
Expand Up @@ -6,6 +6,7 @@ const expect = require('chai').expect;
const redis = require('ioredis');
const utils = require('./utils');
const delay = require('delay');
const sinon = require('sinon');

describe('.pause', () => {
let client;
Expand All @@ -15,6 +16,7 @@ describe('.pause', () => {
});

afterEach(() => {
sinon.restore();
return client.quit();
});

Expand Down Expand Up @@ -284,6 +286,17 @@ describe('.pause', () => {
});
});

it('should not initialize blocking client if not already initialized', async () => {
const createClient = sinon.spy(() => client);
const queue = utils.buildQueue('pause-queue', { createClient });

await queue.pause(true);
const bClientCalls = createClient
.getCalls()
.filter(c => c.args[0] === 'bclient');
expect(bClientCalls).to.have.lengthOf(0);
});

it('pauses fast when queue is drained', function(done) {
this.timeout(10000);
const queue = new Queue('test');
Expand Down

0 comments on commit 3e082c0

Please sign in to comment.