Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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 1 commit
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
Next Next commit
Remove logic of skipping recurring jobs and let them run based on nex…
…tRunAt date if resumeOnRestart is disabled
  • Loading branch information
het-delwadiya committed Jan 22, 2025
commit a66fdcb8d21a3cf1780f23cadd40883dde394eab
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 () => {
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