Skip to content

Commit a62241f

Browse files
authored
fix(queue): forward error from repeat or job-scheduler instances (#3775) fixes #3774
1 parent c17cc22 commit a62241f

File tree

3 files changed

+71
-4
lines changed

3 files changed

+71
-4
lines changed

src/classes/queue.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ export class Queue<
247247
...this.opts,
248248
connection: await this.client,
249249
});
250-
this._repeat.on('error', e => this.emit.bind(this, e));
250+
this._repeat.on('error', this.emit.bind(this, 'error'));
251251
}
252252
resolve(this._repeat);
253253
});
@@ -260,7 +260,7 @@ export class Queue<
260260
...this.opts,
261261
connection: await this.client,
262262
});
263-
this._jobScheduler.on('error', e => this.emit.bind(this, e));
263+
this._jobScheduler.on('error', this.emit.bind(this, 'error'));
264264
}
265265
resolve(this._jobScheduler);
266266
});

src/classes/worker.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ export class Worker<
507507
...this.opts,
508508
connection,
509509
});
510-
this._repeat.on('error', e => this.emit.bind(this, e));
510+
this._repeat.on('error', this.emit.bind(this, 'error'));
511511
}
512512
resolve(this._repeat);
513513
});
@@ -521,7 +521,7 @@ export class Worker<
521521
...this.opts,
522522
connection,
523523
});
524-
this._jobScheduler.on('error', e => this.emit.bind(this, e));
524+
this._jobScheduler.on('error', this.emit.bind(this, 'error'));
525525
}
526526
resolve(this._jobScheduler);
527527
});

tests/error-forwarding.test.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { default as IORedis } from 'ioredis';
2+
import { v4 } from 'uuid';
3+
import { describe, it, expect, vi, beforeAll, afterAll } from 'vitest';
4+
import { Queue } from '../src';
5+
import { removeAllQueueData } from '../src/utils';
6+
7+
describe('Repeat/JobScheduler error forwarding', () => {
8+
const redisHost = process.env.REDIS_HOST || 'localhost';
9+
const prefix = process.env.BULLMQ_TEST_PREFIX || 'bull';
10+
let connection: IORedis;
11+
12+
beforeAll(async () => {
13+
connection = new IORedis(redisHost, { maxRetriesPerRequest: null });
14+
});
15+
16+
afterAll(async () => {
17+
await connection.quit();
18+
});
19+
20+
it('Queue should forward Repeat "error" to queue.on("error")', async () => {
21+
const queueName = `test-forward-repeat-${v4()}`;
22+
const queue = new Queue(queueName, { connection, prefix });
23+
24+
// Force repeat instance creation so the buggy listener is attached
25+
const repeat = await queue.repeat;
26+
await repeat.waitUntilReady();
27+
28+
const spy = vi.fn();
29+
queue.on('error', spy);
30+
31+
const err = new Error('repeat boom');
32+
33+
// Trigger the child error event directly
34+
(queue as any)._repeat.emit('error', err);
35+
36+
// This EXPECTATION FAILS on current buggy code (spy called 0 times)
37+
expect(spy).toHaveBeenCalledTimes(1);
38+
expect(spy).toHaveBeenCalledWith(err);
39+
40+
await queue.close();
41+
await removeAllQueueData(new IORedis(redisHost), queueName);
42+
});
43+
44+
it('Queue should forward JobScheduler "error" to queue.on("error")', async () => {
45+
const queueName = `test-forward-scheduler-${v4()}`;
46+
const queue = new Queue(queueName, { connection, prefix });
47+
48+
// Force jobScheduler instance creation so the buggy listener is attached
49+
const jobScheduler = await queue.jobScheduler;
50+
await jobScheduler.waitUntilReady();
51+
52+
const spy = vi.fn();
53+
queue.on('error', spy);
54+
55+
const err = new Error('scheduler boom');
56+
57+
// Trigger the child error event directly
58+
(queue as any)._jobScheduler.emit('error', err);
59+
60+
// This EXPECTATION FAILS on current buggy code (spy called 0 times)
61+
expect(spy).toHaveBeenCalledTimes(1);
62+
expect(spy).toHaveBeenCalledWith(err);
63+
64+
await queue.close();
65+
await removeAllQueueData(new IORedis(redisHost), queueName);
66+
});
67+
});

0 commit comments

Comments
 (0)