Skip to content

Do not ignore preventOverrun in CronJob#202

Open
boristian wants to merge 1 commit into
kibertoad:mainfrom
boristian:FIX_CronJob-preventOverrun
Open

Do not ignore preventOverrun in CronJob#202
boristian wants to merge 1 commit into
kibertoad:mainfrom
boristian:FIX_CronJob-preventOverrun

Conversation

@boristian

Copy link
Copy Markdown

Fixes #195

@kibertoad

kibertoad commented Sep 3, 2023

Copy link
Copy Markdown
Owner

@Hexagon Could you take a look at the failing tests? It looks like after the protect flag is enabled with the true value, it never gets released.

@boristian

Copy link
Copy Markdown
Author

Any updates on this? Is something wrong with the PR?

@kibertoad

Copy link
Copy Markdown
Owner

@boristian yes, functionality appears to be broken on @Hexagon end

@Hexagon

Hexagon commented Nov 13, 2023

Copy link
Copy Markdown

Hello! Cannot find anything wrong with croner, tested both croner@6.0.4 used by toad-scheduler, and croner@7.0.5 which is the latest iteration.

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

codespace@codespaces-56134a:/workspaces/croner master [?]> deno run test.ts
Call started at 2023-11-13T19:21:44.003Z started
  Call at 2023-11-13T19:21:46.002Z were blocked by call started at 2023-11-13T19:21:44.003Z
  Call at 2023-11-13T19:21:48.001Z were blocked by call started at 2023-11-13T19:21:44.003Z
  Call at 2023-11-13T19:21:50.002Z were blocked by call started at 2023-11-13T19:21:44.003Z
Call started at 2023-11-13T19:21:44.003Z finished 2023-11-13T19:21:50.005Z
Call started at 2023-11-13T19:21:52.001Z started
  Call at 2023-11-13T19:21:54.006Z were blocked by call started at 2023-11-13T19:21:52.001Z
  Call at 2023-11-13T19:21:56.007Z were blocked by call started at 2023-11-13T19:21:52.001Z
Call started at 2023-11-13T19:21:52.001Z finished 2023-11-13T19:21:58.002Z // <- Callback runs before actually unblocking, that's why this call is out of order
  Call at 2023-11-13T19:21:58.007Z were blocked by call started at 2023-11-13T19:21:52.001Z
Call started at 2023-11-13T19:22:00.002Z started
  Call at 2023-11-13T19:22:02.007Z were blocked by call started at 2023-11-13T19:22:00.002Z
  Call at 2023-11-13T19:22:04.006Z were blocked by call started at 2023-11-13T19:22:00.002Z
Call started at 2023-11-13T19:22:00.002Z finished 2023-11-13T19:22:06.004Z

@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()
    })
    ```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

preventOverrun maybe not working for CronJob

3 participants