Skip to content

Commit

Permalink
fix(worker): fix so that worker can be closed if Redis is down
Browse files Browse the repository at this point in the history
  • Loading branch information
manast committed Dec 22, 2023
1 parent fb0d8a7 commit 1f64edf
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
17 changes: 14 additions & 3 deletions src/classes/redis-connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ export class RedisConnection extends EventEmitter {
canDoubleTimeout: false,
};

status: 'initializing' | 'ready' | 'closing' | 'closed' = 'initializing';

protected _client: RedisClient;

private readonly opts: RedisOptions;
Expand Down Expand Up @@ -210,6 +212,7 @@ export class RedisConnection extends EventEmitter {
this._client.on('ready', this.handleClientReady);

await RedisConnection.waitUntilReady(this._client);

this.loadCommands();

this.version = await this.getRedisVersion();
Expand Down Expand Up @@ -239,6 +242,8 @@ export class RedisConnection extends EventEmitter {
canDoubleTimeout: !isRedisVersionLowerThan(this.version, '6.0.0'),
};

this.status = 'ready';

return this._client;
}

Expand Down Expand Up @@ -280,11 +285,16 @@ export class RedisConnection extends EventEmitter {

async close(): Promise<void> {
if (!this.closing) {
const status = this.status;
this.status = 'closing';
this.closing = true;
try {
await this.initializing;
if (!this.shared) {
await this._client.quit();
if (status === 'ready') {
// Not sure if we need to wait for this
await this.initializing;
if (!this.shared) {
await this._client.quit();
}
}
} catch (error) {
if (isNotConnectionError(error as Error)) {
Expand All @@ -298,6 +308,7 @@ export class RedisConnection extends EventEmitter {
decreaseMaxListeners(this._client, 3);

this.removeAllListeners();
this.status = 'closed';
}
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/classes/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -810,7 +810,10 @@ export class Worker<

this.abortDelayController?.abort();

const client = await this.blockingConnection.client;
const client =
this.blockingConnection.status == 'ready'
? await this.blockingConnection.client
: null;

this.resume();
await Promise.resolve()
Expand All @@ -832,7 +835,7 @@ export class Worker<
})
.finally(() => clearTimeout(this.extendLocksTimer))
.finally(() => clearTimeout(this.stalledCheckTimer))
.finally(() => client.disconnect())
.finally(() => client && client.disconnect())
.finally(() => this.connection.close())
.finally(() => this.emit('closed'));
this.closed = true;
Expand Down
10 changes: 10 additions & 0 deletions tests/test_connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,16 @@ describe('connection', () => {
});
});

it('should close worker even if redis is down', async () => {
const connection = new IORedis('badhost', { maxRetriesPerRequest: null });
connection.on('error', () => {});

const worker = new Worker('test', async () => {}, { connection, prefix });

worker.on('error', err => {});
await worker.close();
});

it('should recover from a connection loss', async () => {
let processor;

Expand Down

0 comments on commit 1f64edf

Please sign in to comment.