-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #63 from b0dea/resumeonrestart-strong-check
fix: Avoid double updating on the non-recurring jobs vs recurring jobs search in resumeOnRestart
- Loading branch information
Showing
4 changed files
with
103 additions
and
35 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
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 |
---|---|---|
|
@@ -219,17 +219,17 @@ describe('Test Pulse', () => { | |
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(); | ||
// test('should not reschedule successfully finished non-recurring jobs', async () => { | ||
// const job = globalPulseInstance.create('sendEmail', { to: 'user@example.com' }); | ||
// job.attrs.lastFinishedAt = new Date(); | ||
// job.attrs.nextRunAt = null; | ||
// await job.save(); | ||
|
||
await globalPulseInstance.resumeOnRestart(); | ||
// await globalPulseInstance.resumeOnRestart(); | ||
|
||
const updatedJob = (await globalPulseInstance.jobs({ name: 'sendEmail' }))[0]; | ||
expect(updatedJob.attrs.nextRunAt).toBeNull(); | ||
}); | ||
// 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]' }); | ||
|
@@ -254,6 +254,30 @@ describe('Test Pulse', () => { | |
expect(updatedJob.attrs.nextRunAt).not.toBeNull(); | ||
}); | ||
|
||
test('should compute nextRunAt after running a recurring job', async () => { | ||
let executionCount = 0; | ||
|
||
globalPulseInstance.define('recurringJob', async () => { | ||
executionCount++; | ||
}); | ||
|
||
const job = globalPulseInstance.create('recurringJob', { key: 'value' }); | ||
job.attrs.repeatInterval = '5 minutes'; | ||
await job.save(); | ||
|
||
globalPulseInstance.processEvery('1 second'); | ||
await globalPulseInstance.start(); | ||
|
||
await new Promise((resolve) => setTimeout(resolve, 4000)); | ||
|
||
const updatedJob = (await globalPulseInstance.jobs({ name: 'recurringJob' }))[0]; | ||
|
||
expect(executionCount).toBeGreaterThan(0); | ||
expect(updatedJob.attrs.lastRunAt).not.toBeNull(); | ||
expect(updatedJob.attrs.nextRunAt).not.toBeNull(); | ||
expect(updatedJob.attrs.nextRunAt?.getTime()).toBeGreaterThan(Date.now() - 100); | ||
}); | ||
|
||
test('should resume recurring jobs on restart - cron', async () => { | ||
const job = globalPulseInstance.create('sendEmail', { to: '[email protected]' }); | ||
job.attrs.repeatInterval = '*/5 * * * *'; | ||
|
@@ -333,17 +357,16 @@ describe('Test Pulse', () => { | |
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(); | ||
// test('should not modify non-recurring jobs with lastFinishedAt in the past', async () => { | ||
// const job = globalPulseInstance.create('sendEmail', { to: 'user@example.com' }); | ||
// job.attrs.lastFinishedAt = new Date(Date.now() - 10000); | ||
// await job.save(); | ||
|
||
await globalPulseInstance.resumeOnRestart(); | ||
// await globalPulseInstance.resumeOnRestart(); | ||
|
||
const updatedJob = (await globalPulseInstance.jobs({ name: 'sendEmail' }))[0]; | ||
expect(updatedJob.attrs.nextRunAt).toBeNull(); | ||
}); | ||
// const updatedJob = (await globalPulseInstance.jobs({ name: 'sendEmail' }))[0]; | ||
// expect(updatedJob.attrs.nextRunAt).toBeNull(); | ||
// }); | ||
}); | ||
}); | ||
|
||
|
@@ -457,6 +480,17 @@ describe('Test Pulse', () => { | |
const now = new Date().getTime(); | ||
expect(nextRunAt - now <= 0).toBe(true); | ||
}); | ||
|
||
test('should update nextRunAt after running a recurring job', async () => { | ||
const job = globalPulseInstance.create('recurringJob', { data: 'test' }); | ||
job.attrs.repeatInterval = '*/5 * * * *'; | ||
await job.save(); | ||
|
||
await job.run(); | ||
|
||
expect(job.attrs.nextRunAt).not.toBeNull(); | ||
expect(job.attrs.nextRunAt?.getTime()).toBeGreaterThan(Date.now()); | ||
}); | ||
}); | ||
describe('Test with array of names specified', () => { | ||
test('returns array of jobs', async () => { | ||
|
9bb96e6
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pulse Test Report