From 1aaaa61f0a009563a84cb81036427c187076f190 Mon Sep 17 00:00:00 2001 From: simon Date: Sun, 25 Oct 2020 15:06:12 +0100 Subject: [PATCH] feat: isRunning querys database again if called by client --- src/Job.ts | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Job.ts b/src/Job.ts index 9c7aa77..a33fafc 100644 --- a/src/Job.ts +++ b/src/Job.ts @@ -152,7 +152,16 @@ export class Job { return this; } - isRunning(): boolean { + async isRunning(): Promise { + const definition = this.agenda.definitions[this.attrs.name]; + if (!definition) { + // we have no job definition, therfore we are not the job processor, but a client call + // so we get the real state from database + const dbJob = await this.agenda.db.getJobs({ _id: this.attrs._id }); + this.attrs.lastRunAt = dbJob[0].lastRunAt; + this.attrs.lastFinishedAt = dbJob[0].lastFinishedAt; + } + if (!this.attrs.lastRunAt) { return false; } @@ -183,6 +192,10 @@ export class Job { isDead(): boolean { const definition = this.agenda.definitions[this.attrs.name]; + if (!definition) { + console.warn('this method is only callable from an agenda job processor'); + return false; + } const lockDeadline = new Date(Date.now() - definition.lockLifetime); // This means a job has "expired", as in it has not been "touched" within the lockoutTime