-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Fix the resumeOnRestart function
Fix the resumeOnRestart function to handle correctl the recurring jobs + do not run finished non-recurrent jobs on restart + add unit tests
- Loading branch information
Showing
3 changed files
with
183 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -218,6 +218,132 @@ describe('Test Pulse', () => { | |
test('returns itself', () => { | ||
expect(globalPulseInstance.resumeOnRestart(false)).toEqual(globalPulseInstance); | ||
}); | ||
|
||
test('should not reschedule successfully finished non-recurring jobs', async () => { | ||
const job = globalPulseInstance.create('sendEmail', { to: '[email protected]' }); | ||
job.attrs.lastFinishedAt = new Date(); | ||
job.attrs.nextRunAt = null; | ||
await job.save(); | ||
|
||
await globalPulseInstance.resumeOnRestart(); | ||
|
||
const updatedJob = (await globalPulseInstance.jobs({ name: 'sendEmail' }))[0]; | ||
expect(updatedJob.attrs.nextRunAt).toBeNull(); | ||
}); | ||
|
||
test('should resume non-recurring jobs on restart', async () => { | ||
const job = globalPulseInstance.create('sendEmail', { to: '[email protected]' }); | ||
job.attrs.nextRunAt = new Date(Date.now() - 1000); | ||
await job.save(); | ||
|
||
await globalPulseInstance.resumeOnRestart(); | ||
|
||
const updatedJob = (await globalPulseInstance.jobs({ name: 'sendEmail' }))[0]; | ||
expect(updatedJob.attrs.nextRunAt?.getTime()).toBeGreaterThan(Date.now() - 100); | ||
}); | ||
|
||
test('should resume recurring jobs on restart - interval', async () => { | ||
const job = globalPulseInstance.create('sendEmail', { to: '[email protected]' }); | ||
job.attrs.repeatInterval = '5 minutes'; | ||
job.attrs.nextRunAt = null; | ||
await job.save(); | ||
|
||
await globalPulseInstance.resumeOnRestart(); | ||
|
||
const updatedJob = (await globalPulseInstance.jobs({ name: 'sendEmail' }))[0]; | ||
expect(updatedJob.attrs.nextRunAt).not.toBeNull(); | ||
}); | ||
|
||
test('should resume recurring jobs on restart - cron', async () => { | ||
const job = globalPulseInstance.create('sendEmail', { to: '[email protected]' }); | ||
job.attrs.repeatInterval = '*/5 * * * *'; | ||
job.attrs.nextRunAt = null; | ||
await job.save(); | ||
|
||
await globalPulseInstance.resumeOnRestart(); | ||
|
||
const updatedJob = (await globalPulseInstance.jobs({ name: 'sendEmail' }))[0]; | ||
expect(updatedJob.attrs.nextRunAt).not.toBeNull(); | ||
}); | ||
|
||
test('should resume recurring jobs on restart - repeatAt', async () => { | ||
const job = globalPulseInstance.create('sendEmail', { to: '[email protected]' }); | ||
job.attrs.repeatAt = '1:00 am'; | ||
job.attrs.nextRunAt = null; | ||
await job.save(); | ||
|
||
await globalPulseInstance.resumeOnRestart(); | ||
|
||
const updatedJob = (await globalPulseInstance.jobs({ name: 'sendEmail' }))[0]; | ||
expect(updatedJob.attrs.nextRunAt).not.toBeNull(); | ||
}); | ||
|
||
test('should not modify jobs with existing nextRunAt', async () => { | ||
const futureDate = new Date(Date.now() + 60 * 60 * 1000); | ||
const job = globalPulseInstance.create('sendEmail', { to: '[email protected]' }); | ||
job.attrs.nextRunAt = futureDate; | ||
await job.save(); | ||
|
||
await globalPulseInstance.resumeOnRestart(); | ||
|
||
const updatedJob = (await globalPulseInstance.jobs({ name: 'sendEmail' }))[0]; | ||
expect(updatedJob.attrs.nextRunAt?.getTime()).toEqual(futureDate.getTime()); | ||
}); | ||
|
||
test('should handle jobs that started but have not finished (non-recurring)', async () => { | ||
const job = globalPulseInstance.create('processData', { data: 'sample' }); | ||
job.attrs.nextRunAt = null; | ||
job.attrs.lockedAt = new Date(); | ||
await job.save(); | ||
|
||
await globalPulseInstance.resumeOnRestart(); | ||
|
||
const updatedJob = (await globalPulseInstance.jobs({ name: 'processData' }))[0]; | ||
|
||
const now = Date.now(); | ||
expect(updatedJob.attrs.nextRunAt).not.toBeNull(); | ||
expect(updatedJob.attrs.nextRunAt?.getTime()).toBeGreaterThan(now - 100); | ||
}); | ||
|
||
test('should handle recurring jobs that started but have not finished', async () => { | ||
const job = globalPulseInstance.create('processData', { data: 'sample' }); | ||
job.attrs.repeatInterval = '10 minutes'; | ||
job.attrs.lockedAt = new Date(); | ||
job.attrs.nextRunAt = new Date(Date.now() + 10000); | ||
await job.save(); | ||
|
||
await globalPulseInstance.resumeOnRestart(); | ||
|
||
const updatedJob = (await globalPulseInstance.jobs({ name: 'processData' }))[0]; | ||
expect(updatedJob.attrs.lockedAt).not.toBeNull(); | ||
expect(updatedJob.attrs.nextRunAt).not.toBeNull(); | ||
}); | ||
|
||
test('should handle interrupted recurring jobs after server recovery', async () => { | ||
const job = globalPulseInstance.create('processData', { data: 'sample' }); | ||
job.attrs.repeatInterval = '5 minutes'; | ||
job.attrs.lastModifiedBy = 'server_crash'; | ||
job.attrs.nextRunAt = null; | ||
await job.save(); | ||
|
||
await globalPulseInstance.resumeOnRestart(); | ||
|
||
const updatedJob = (await globalPulseInstance.jobs({ name: 'processData' }))[0]; | ||
expect(updatedJob.attrs.nextRunAt).not.toBeNull(); | ||
expect(updatedJob.attrs.lastModifiedBy).not.toEqual('server_crash'); | ||
}); | ||
|
||
test('should not modify non-recurring jobs with lastFinishedAt in the past', async () => { | ||
const job = globalPulseInstance.create('sendEmail', { to: '[email protected]' }); | ||
job.attrs.lastFinishedAt = new Date(Date.now() - 10000); | ||
job.attrs.nextRunAt = null; | ||
await job.save(); | ||
|
||
await globalPulseInstance.resumeOnRestart(); | ||
|
||
const updatedJob = (await globalPulseInstance.jobs({ name: 'sendEmail' }))[0]; | ||
expect(updatedJob.attrs.nextRunAt).toBeNull(); | ||
}); | ||
}); | ||
}); | ||
|
||
|