Do not ignore preventOverrun in CronJob#202
Open
boristian wants to merge 1 commit into
Open
Conversation
Owner
|
@Hexagon Could you take a look at the failing tests? It looks like after the |
Author
|
Any updates on this? Is something wrong with the PR? |
Owner
|
@boristian yes, functionality appears to be broken on @Hexagon end |
|
Hello! Cannot find anything wrong with croner, tested both Tested using this code: import { Cron } from "npm:croner@6.0.4";
// Demo blocking function
const blockForAWhile = (ms) => new Promise(resolve => setTimeout(resolve, ms));
// (Optional) Callback to be triggered on a blocked call
const protectCallback = (job) => console.log(`Call at ${new Date().toISOString()} were blocked by call started at ${job.currentRun().toISOString()}`);
Cron(
"*/2 * * * * *",
{
protect: protectCallback // <- Using a callback for overrun protection, but `true` would work just as well
},
async (job) => {
console.log(`Call started at ${job.currentRun().toISOString()} started`);
await blockForAWhile(6000);
console.log(`Call started at ${job.currentRun().toISOString()} finished ${new Date().toISOString()}`);
}
);With this output @boristian @kibertoad Can you check the comments i made below? it('allows preventing CronJob execution overrun for async tasks', async () => {
let counter = 0
const scheduler = new ToadScheduler()
const task = new AsyncTask('simple task', () => { // <- 1. The callback isn't flagged with async, is this correct?
counter++
advanceTimersByTime(6000) // Do this really delay the return of this async function?
return Promise.resolve(undefined)
})
const job = new CronJob(
{
cronExpression: '*/2 * * * * *',
},
task,
{
preventOverrun: true,
}
)
scheduler.addCronJob(job)
expect(counter).toBe(0) // Counter is expected to be 0, yes
advanceTimersByTime(1000)
advanceTimersByTime(1000)
advanceTimersByTime(1000)
advanceTimersByTime(1000)
advanceTimersByTime(1000)
expect(counter).toBe(2) // 5 seconds elapsed, but advanceTimersByTime in callback is advanced 6 seconds... Counter should still be 1,
advanceTimersByTime(1000) // somewhere around now
advanceTimersByTime(1000) // or now - will the second callback call
advanceTimersByTime(1000)
advanceTimersByTime(1000)
advanceTimersByTime(1000)
expect(counter).toBe(5) // Counter should be around 2 here, not 5
advanceTimersByTime(1000)
advanceTimersByTime(1000)
advanceTimersByTime(1000)
advanceTimersByTime(1000)
advanceTimersByTime(1000)
expect(counter).toBe(7)
scheduler.stop()
})
``` |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #195