Skip to content

Commit 17b6afa

Browse files
committed
fix(worker): treat timeout=0 as immediate timeout
1 parent 428d91e commit 17b6afa

2 files changed

Lines changed: 50 additions & 1 deletion

File tree

src/worker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ export class Worker {
441441
context: JobContext,
442442
timeout?: number
443443
): Promise<void> {
444-
if (!timeout) {
444+
if (timeout === undefined) {
445445
instance.$hydrate(payload, context)
446446
return instance.execute()
447447
}

tests/worker.spec.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,55 @@ test.group('Worker', () => {
461461
assert.isBelow(elapsed, 150, 'Job should be killed before completing')
462462
})
463463

464+
test('should apply timeout when timeout is set to 0', async ({ assert, cleanup }) => {
465+
assert.plan(2)
466+
467+
class ZeroTimeoutJob extends Job {
468+
static options = { timeout: 0 }
469+
470+
async execute() {
471+
await setTimeout(200)
472+
}
473+
474+
async failed(error: Error) {
475+
assert.instanceOf(error, errors.E_JOB_TIMEOUT)
476+
}
477+
}
478+
479+
const sharedAdapter = memory()()
480+
481+
const localConfig = {
482+
default: 'memory',
483+
adapters: { memory: () => sharedAdapter },
484+
}
485+
486+
Locator.register('ZeroTimeoutJob', ZeroTimeoutJob)
487+
488+
const worker = new Worker(localConfig)
489+
490+
cleanup(async () => {
491+
Locator.clear()
492+
await worker.stop()
493+
})
494+
495+
await sharedAdapter.push({
496+
id: 'timeout-zero-job',
497+
name: 'ZeroTimeoutJob',
498+
payload: {},
499+
attempts: 0,
500+
priority: 0,
501+
})
502+
503+
const startTime = Date.now()
504+
505+
await worker.processCycle(['default']) // started
506+
await worker.processCycle(['default']) // completed (timeout)
507+
508+
const elapsed = Date.now() - startTime
509+
510+
assert.isBelow(elapsed, 150, 'Job should be killed before completing')
511+
})
512+
464513
test('should remove timeout abort listener when job completes before timeout', async ({
465514
assert,
466515
cleanup,

0 commit comments

Comments
 (0)