Skip to content

Commit 1aaaa61

Browse files
committed
feat: isRunning querys database again if called by client
1 parent 92992cb commit 1aaaa61

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

src/Job.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,16 @@ export class Job<DATA = unknown | void> {
152152
return this;
153153
}
154154

155-
isRunning(): boolean {
155+
async isRunning(): Promise<boolean> {
156+
const definition = this.agenda.definitions[this.attrs.name];
157+
if (!definition) {
158+
// we have no job definition, therfore we are not the job processor, but a client call
159+
// so we get the real state from database
160+
const dbJob = await this.agenda.db.getJobs({ _id: this.attrs._id });
161+
this.attrs.lastRunAt = dbJob[0].lastRunAt;
162+
this.attrs.lastFinishedAt = dbJob[0].lastFinishedAt;
163+
}
164+
156165
if (!this.attrs.lastRunAt) {
157166
return false;
158167
}
@@ -183,6 +192,10 @@ export class Job<DATA = unknown | void> {
183192

184193
isDead(): boolean {
185194
const definition = this.agenda.definitions[this.attrs.name];
195+
if (!definition) {
196+
console.warn('this method is only callable from an agenda job processor');
197+
return false;
198+
}
186199
const lockDeadline = new Date(Date.now() - definition.lockLifetime);
187200

188201
// This means a job has "expired", as in it has not been "touched" within the lockoutTime

0 commit comments

Comments
 (0)