From 7247b3bb9741c5eb18ce4027ea14349cbc8504c5 Mon Sep 17 00:00:00 2001 From: manoj kumar Date: Sun, 31 Oct 2021 16:39:11 +0530 Subject: [PATCH] feat: emit event on job lock extend failure --- REFERENCE.md | 6 ++++++ lib/queue.js | 4 ++-- test/test_events.js | 21 +++++++++++++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/REFERENCE.md b/REFERENCE.md index 6858ae722..fb9b1d012 100644 --- a/REFERENCE.md +++ b/REFERENCE.md @@ -955,6 +955,12 @@ A queue emits also some useful events: // workers that crash or pause the event loop. }) +.on('lock-extension-failed', function (job, err) { + // A job failed to extend lock. This will be useful to debug redis + // connection issues and jobs getting restarted because workers + // are not able to extend locks. +}); + .on('progress', function (job, progress) { // A job's progress was updated! }) diff --git a/lib/queue.js b/lib/queue.js index ba02a8d66..2c21b15cb 100755 --- a/lib/queue.js +++ b/lib/queue.js @@ -1078,8 +1078,8 @@ Queue.prototype.processJob = function(job, notFetch = false) { lockExtender(); } }) - .catch((/*err*/) => { - // Somehow tell the worker this job should stop processing... + .catch(err => { + this.emit('lock-extension-failed', job, err); }); } ); diff --git a/test/test_events.js b/test/test_events.js index e1bf9d9ff..c72a2a531 100644 --- a/test/test_events.js +++ b/test/test_events.js @@ -211,6 +211,27 @@ describe('events', () => { }); }); + it('should emit an event if a job fails to extend lock', done => { + const LOCK_RENEW_TIME = 1; + queue = utils.buildQueue('queue fails to extend lock', { + settings: { + lockRenewTime: LOCK_RENEW_TIME + } + }); + queue.once('lock-extension-failed', (lockingFailedJob, error) => { + expect(lockingFailedJob.data.foo).to.be.equal('lockingFailedJobFoo'); + expect(error.message).to.be.equal('Connection is closed.'); + queue.close().then(done); + }); + queue.isReady().then(() => { + queue.process(() => { + utils.simulateDisconnect(queue); + return delay(LOCK_RENEW_TIME + 0.25); + }); + queue.add({ foo: 'lockingFailedJobFoo' }); + }); + }); + it('should listen to global events', done => { const queue1 = utils.buildQueue(); const queue2 = utils.buildQueue();