From a5bb965a57ffe28db8eae40311e0c102210509fa Mon Sep 17 00:00:00 2001 From: simon Date: Sun, 25 Oct 2020 19:35:13 +0100 Subject: [PATCH] fix: isRunning for non job processor calls --- src/Job.ts | 17 ++++++++++------- src/JobProcessor.ts | 4 ++-- test/job.test.ts | 13 +++++++++++++ 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/Job.ts b/src/Job.ts index 5b0ab38..bdbdbb5 100644 --- a/src/Job.ts +++ b/src/Job.ts @@ -29,7 +29,8 @@ export class Job { args: Partial> & { name: string; type: 'normal' | 'single'; - } + }, + byJobProcessor? ); constructor( agenda: Agenda, @@ -37,7 +38,8 @@ export class Job { name: string; type: 'normal' | 'single'; data: DATA; - } + }, + byJobProcessor? ); constructor( readonly agenda: Agenda, @@ -45,7 +47,8 @@ export class Job { name: string; type: 'normal' | 'single'; data: DATA; - } + }, + private readonly byJobProcessor = false ) { // Set attrs to args this.attrs = { @@ -161,8 +164,7 @@ export class Job { } async isRunning(): Promise { - const definition = this.agenda.definitions[this.attrs.name]; - if (!definition || !this.agenda.isActiveJobProcessor()) { + if (!this.byJobProcessor) { // we have no job definition, therfore we are not the job processor, but a client call // so we get the real state from database await this.fetchStatus(); @@ -197,13 +199,14 @@ export class Job { } async isDead(): Promise { - const definition = this.agenda.definitions[this.attrs.name]; - if (!definition || !this.agenda.isActiveJobProcessor()) { + if (!this.byJobProcessor) { // we have no job definition, therfore we are not the job processor, but a client call // so we get the real state from database await this.fetchStatus(); } + const definition = this.agenda.definitions[this.attrs.name]; + const lockDeadline = new Date(Date.now() - definition.lockLifetime); // This means a job has "expired", as in it has not been "touched" within the lockoutTime diff --git a/src/JobProcessor.ts b/src/JobProcessor.ts index 11c03e1..abf8cd4 100644 --- a/src/JobProcessor.ts +++ b/src/JobProcessor.ts @@ -239,7 +239,7 @@ export class JobProcessor { ); } - const jobToEnqueue = new Job(this.agenda, resp) as JobWithId; + const jobToEnqueue = new Job(this.agenda, resp, true) as JobWithId; // Before en-queing job make sure we haven't exceed our lock limits if (!this.shouldLock(jobToEnqueue.attrs.name)) { @@ -291,7 +291,7 @@ export class JobProcessor { 'found a job available to lock, creating a new job on Agenda with id [%s]', result._id ); - return new Job(this.agenda, result) as JobWithId; + return new Job(this.agenda, result, true) as JobWithId; } return undefined; diff --git a/test/job.test.ts b/test/job.test.ts index 6bc4f49..65215c8 100644 --- a/test/job.test.ts +++ b/test/job.test.ts @@ -1479,4 +1479,17 @@ describe('Job', () => { }); }); }); + + it('checks database for running job on "client"', async () => { + agenda.define('test', async () => { + await new Promise(resolve => setTimeout(resolve, 30000)); + }); + + const job = await agenda.now('test'); + await agenda.start(); + + await new Promise(resolve => agenda.on('start:test', resolve)); + + expect(await job.isRunning()).to.be.equal(true); + }); });