From 3e082c0c7ed81eb4429237365ad10e1aff8286d1 Mon Sep 17 00:00:00 2001 From: Gabe Gorelick Date: Tue, 11 Jun 2019 15:28:21 -0500 Subject: [PATCH] fix: whenCurrentJobsFinished shouldn't initialize bclient Fixes #1346 --- lib/queue.js | 11 +++++++++++ test/test_pause.js | 13 +++++++++++++ 2 files changed, 24 insertions(+) diff --git a/lib/queue.js b/lib/queue.js index db61360aa..b642a8a38 100755 --- a/lib/queue.js +++ b/lib/queue.js @@ -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); } @@ -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. // diff --git a/test/test_pause.js b/test/test_pause.js index 832610b52..ec05068d9 100644 --- a/test/test_pause.js +++ b/test/test_pause.js @@ -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; @@ -15,6 +16,7 @@ describe('.pause', () => { }); afterEach(() => { + sinon.restore(); return client.quit(); }); @@ -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');