Skip to content
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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

boristian
Copy link

Fixes #195

@kibertoad
Copy link
Owner

kibertoad commented Sep 3, 2023

@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
Author

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

@kibertoad
Copy link
Owner

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

@Hexagon
Copy link

Hexagon commented Nov 13, 2023

Hello! Cannot find anything wrong with croner, tested both [email protected] used by toad-scheduler, and [email protected] which is the latest iteration.

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

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