diff --git a/src/classes/redis-connection.ts b/src/classes/redis-connection.ts index 2111e8e514..3247b5fce1 100644 --- a/src/classes/redis-connection.ts +++ b/src/classes/redis-connection.ts @@ -288,13 +288,21 @@ export class RedisConnection extends EventEmitter { const status = this.status; this.status = 'closing'; this.closing = true; + try { if (status === 'ready') { // Not sure if we need to wait for this await this.initializing; - if (!this.shared) { + } + if (!this.shared) { + if (status == 'initializing') { + // If we have not still connected to Redis, we need to disconnect. + this._client.disconnect(); + } else { await this._client.quit(); } + // As IORedis does not update this status properly, we do it ourselves. + this._client['status'] = 'end'; } } catch (error) { if (isNotConnectionError(error as Error)) { diff --git a/tests/test_connection.ts b/tests/test_connection.ts index d54c3202b3..037fe8e7a0 100644 --- a/tests/test_connection.ts +++ b/tests/test_connection.ts @@ -144,6 +144,20 @@ describe('connection', () => { await worker.close(); }); + it('should close underlying redis connection when closing fast', async () => { + const queue = new Queue('CALLS_JOB_QUEUE_NAME', { + connection: { + host: 'localhost', + port: 6379, + }, + }); + + const client = queue['connection']['_client']; + await queue.close(); + + expect(client.status).to.be.eql('end'); + }); + it('should recover from a connection loss', async () => { let processor;