Skip to content

Commit 413f797

Browse files
committed
fix: wait for start of test job
1 parent f3d6c56 commit 413f797

File tree

4 files changed

+10
-6
lines changed

4 files changed

+10
-6
lines changed

src/Job.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ export class Job<DATA = unknown | void> {
162162

163163
async isRunning(): Promise<boolean> {
164164
const definition = this.agenda.definitions[this.attrs.name];
165-
if (!definition) {
165+
if (!definition || !this.agenda.isActiveJobProcessor()) {
166166
// we have no job definition, therfore we are not the job processor, but a client call
167167
// so we get the real state from database
168168
await this.fetchStatus();
@@ -198,7 +198,7 @@ export class Job<DATA = unknown | void> {
198198

199199
async isDead(): Promise<boolean> {
200200
const definition = this.agenda.definitions[this.attrs.name];
201-
if (!definition) {
201+
if (!definition || !this.agenda.isActiveJobProcessor()) {
202202
// we have no job definition, therfore we are not the job processor, but a client call
203203
// so we get the real state from database
204204
await this.fetchStatus();

src/JobProcessor.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ export class JobProcessor {
500500
}
501501

502502
resolve(checkIfJobIsStillAlive());
503-
}, Math.max(this.processEvery, this.agenda.definitions[job.attrs.name].lockLifetime / 2))
503+
}, Math.max(this.processEvery / 2, this.agenda.definitions[job.attrs.name].lockLifetime / 2))
504504
);
505505
};
506506

src/index.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ export class Agenda extends EventEmitter {
6464

6565
readonly ready: Promise<void>;
6666

67+
isActiveJobProcessor(): boolean {
68+
return !!this.jobProcessor;
69+
}
70+
6771
async getRunningStats(fullDetails = false): Promise<IAgendaStatus> {
6872
if (!this.jobProcessor) {
6973
throw new Error('agenda not running!');
@@ -408,8 +412,6 @@ export class Agenda extends EventEmitter {
408412
this.attrs.processEvery
409413
);
410414

411-
await this.jobProcessor.process();
412-
413415
this.on('processJob', job => this.jobProcessor?.process(job));
414416
}
415417

test/jobprocessor.test.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ describe('JobProcessor', () => {
7777
agenda.now('test');
7878
await agenda.start();
7979

80+
await new Promise(resolve => agenda.on('start:test', resolve));
81+
8082
const status = await agenda.getRunningStats();
8183
expect(status).to.have.property('jobStatus');
8284
if (status.jobStatus) {
@@ -155,7 +157,7 @@ describe('JobProcessor', () => {
155157
'test long',
156158
async () => {
157159
jobStarted = true;
158-
await new Promise(resolve => setTimeout(resolve, 1000));
160+
await new Promise(resolve => setTimeout(resolve, 2500));
159161
},
160162
{ lockLifetime: 500 }
161163
);

0 commit comments

Comments
 (0)