diff --git a/src/Queue.ts b/src/Queue.ts index 164fe7c..75903f3 100644 --- a/src/Queue.ts +++ b/src/Queue.ts @@ -327,7 +327,6 @@ export class Queue { this.jobStore.removeJob(rawJob); } catch (error) { - worker.triggerFailure(job, error); const { attempts } = rawJob; // tslint:disable-next-line: prefer-const let { errors, failedAttempts } = JSON.parse(rawJob.metaData); @@ -337,6 +336,7 @@ export class Queue { failed = new Date().toISOString(); } const metaData = JSON.stringify({ errors: [...errors, error], failedAttempts }); + worker.triggerFailure({ ...job, metaData, failed }, error); this.jobStore.updateJob({ ...rawJob, ...{ active: FALSE, metaData, failed } }); } finally { delete this.runningJobPromises[job.id] diff --git a/src/__tests__/Queue.test.ts b/src/__tests__/Queue.test.ts index b1c83d4..91de432 100644 --- a/src/__tests__/Queue.test.ts +++ b/src/__tests__/Queue.test.ts @@ -145,9 +145,23 @@ describe('Queue Basics', () => { }) ); queue.configure({ onQueueFinish: onQueueFinish }); - queue.addWorker( - new Worker('testWorker', executer, { concurrency: 1, onFailure: onError }) - ); + queue.addWorker(new Worker('testWorker', executer, { concurrency: 1, onFailure: onError })); + queue.addJob('testWorker', { test: '1' }, { attempts: 0, timeout: 5, priority: 0 }, false); + queue.start(); + }); + it('trigger onFailure', (done) => { + const executer = async () => { + throw new Error('This is an error'); + }; + const onFailure = (job: Job, _: Error) => { + try { + expect(job.failed).not.toEqual(''); + done(); + } catch (error) { + done(error); + } + }; + queue.addWorker(new Worker('testWorker', executer, { concurrency: 1, onFailure })); queue.addJob('testWorker', { test: '1' }, { attempts: 0, timeout: 5, priority: 0 }, false); queue.start(); });