Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(worker): fix so that worker can be closed if Redis is down #2350

Merged
merged 1 commit into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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