-
Notifications
You must be signed in to change notification settings - Fork 25
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
Do not ignore preventOverrun in CronJob #202
base: main
Are you sure you want to change the base?
Conversation
@Hexagon Could you take a look at the failing tests? It looks like after the |
Any updates on this? Is something wrong with the PR? |
@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:[email protected]";
// 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()
})
``` |
Fixes #195