Skip to content
This repository was archived by the owner on Jun 18, 2025. It is now read-only.

Fix recurring queued jobs execution skip issue on starting new instance #72

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 0 additions & 13 deletions src/job/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,19 +85,6 @@ export const run: RunMethod = async function (this: Job) {
throw new JobError('Undefined job');
}

// on restart, skip the job if it's not time to run
if (
!this.pulse._resumeOnRestart &&
previousRunAt &&
this.pulse._readyAt >= previousRunAt &&
this.attrs.nextRunAt
) {
debug('[%s:%s] job resumeOnRestart skipped', this.attrs.name, this.attrs._id);
resumeOnRestartSkipped = true;
await jobCallback(undefined, 'skipped');
return;
}

this.attrs.runCount = (this.attrs.runCount || 0) + 1;

if (definition.fn.length === 2) {
Expand Down
22 changes: 22 additions & 0 deletions test/unit/pulse.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,28 @@ describe('Test Pulse', () => {
// const updatedJob = (await globalPulseInstance.jobs({ name: 'sendEmail' }))[0];
// expect(updatedJob.attrs.nextRunAt).toBeNull();
// });

test('should not skip queued recurring jobs while starting a new pulse instance in case of resumeOnRestart is disabled', async () => {
await globalPulseInstance.stop();

// Create a recurring job that is in queue and wasn't locked by any pulse instance
const job = globalPulseInstance.create('processData', { data: 'sample' });
job.attrs.repeatInterval = '10 minutes';
job.attrs.lockedAt = null;
job.attrs.nextRunAt = new Date(Date.now() - 10000);
await job.save();

// Starting a new pulse instance
const newPulseInstance = new Pulse({ mongo: mongoDb, resumeOnRestart: false });
newPulseInstance.define('processData', jobProcessor);
await newPulseInstance.start();

await delay(1000);
const updatedJob = (await newPulseInstance.jobs({ name: 'processData' }))[0];
expect(updatedJob.attrs.lastFinishedAt).toBeDefined();

await newPulseInstance.stop();
});
});
});

Expand Down