Skip to content

Commit a5bb965

Browse files
committed
fix: isRunning for non job processor calls
1 parent 413f797 commit a5bb965

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

src/Job.ts

+10-7
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,26 @@ export class Job<DATA = unknown | void> {
2929
args: Partial<IJobParameters<void>> & {
3030
name: string;
3131
type: 'normal' | 'single';
32-
}
32+
},
33+
byJobProcessor?
3334
);
3435
constructor(
3536
agenda: Agenda,
3637
args: Partial<IJobParameters<DATA>> & {
3738
name: string;
3839
type: 'normal' | 'single';
3940
data: DATA;
40-
}
41+
},
42+
byJobProcessor?
4143
);
4244
constructor(
4345
readonly agenda: Agenda,
4446
args: Partial<IJobParameters<DATA>> & {
4547
name: string;
4648
type: 'normal' | 'single';
4749
data: DATA;
48-
}
50+
},
51+
private readonly byJobProcessor = false
4952
) {
5053
// Set attrs to args
5154
this.attrs = {
@@ -161,8 +164,7 @@ export class Job<DATA = unknown | void> {
161164
}
162165

163166
async isRunning(): Promise<boolean> {
164-
const definition = this.agenda.definitions[this.attrs.name];
165-
if (!definition || !this.agenda.isActiveJobProcessor()) {
167+
if (!this.byJobProcessor) {
166168
// we have no job definition, therfore we are not the job processor, but a client call
167169
// so we get the real state from database
168170
await this.fetchStatus();
@@ -197,13 +199,14 @@ export class Job<DATA = unknown | void> {
197199
}
198200

199201
async isDead(): Promise<boolean> {
200-
const definition = this.agenda.definitions[this.attrs.name];
201-
if (!definition || !this.agenda.isActiveJobProcessor()) {
202+
if (!this.byJobProcessor) {
202203
// we have no job definition, therfore we are not the job processor, but a client call
203204
// so we get the real state from database
204205
await this.fetchStatus();
205206
}
206207

208+
const definition = this.agenda.definitions[this.attrs.name];
209+
207210
const lockDeadline = new Date(Date.now() - definition.lockLifetime);
208211

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

src/JobProcessor.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ export class JobProcessor {
239239
);
240240
}
241241

242-
const jobToEnqueue = new Job(this.agenda, resp) as JobWithId;
242+
const jobToEnqueue = new Job(this.agenda, resp, true) as JobWithId;
243243

244244
// Before en-queing job make sure we haven't exceed our lock limits
245245
if (!this.shouldLock(jobToEnqueue.attrs.name)) {
@@ -291,7 +291,7 @@ export class JobProcessor {
291291
'found a job available to lock, creating a new job on Agenda with id [%s]',
292292
result._id
293293
);
294-
return new Job(this.agenda, result) as JobWithId;
294+
return new Job(this.agenda, result, true) as JobWithId;
295295
}
296296

297297
return undefined;

test/job.test.ts

+13
Original file line numberDiff line numberDiff line change
@@ -1479,4 +1479,17 @@ describe('Job', () => {
14791479
});
14801480
});
14811481
});
1482+
1483+
it('checks database for running job on "client"', async () => {
1484+
agenda.define('test', async () => {
1485+
await new Promise(resolve => setTimeout(resolve, 30000));
1486+
});
1487+
1488+
const job = await agenda.now('test');
1489+
await agenda.start();
1490+
1491+
await new Promise(resolve => agenda.on('start:test', resolve));
1492+
1493+
expect(await job.isRunning()).to.be.equal(true);
1494+
});
14821495
});

0 commit comments

Comments
 (0)