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

[BUG] Setting runOnInit executes onTick with false start #574

Closed
frbuceta opened this issue Jul 16, 2021 · 11 comments
Closed

[BUG] Setting runOnInit executes onTick with false start #574

frbuceta opened this issue Jul 16, 2021 · 11 comments

Comments

@frbuceta
Copy link

OnTick is running when runOnTick is true and start is false.

Any solution to this problem?

@ManuLpz4
Copy link

ManuLpz4 commented Mar 2, 2023

Same here, some workaround? 👀

@intcreator
Copy link
Collaborator

can you add a test case that illustrates this issue?

@ManuLpz4
Copy link

ManuLpz4 commented Mar 7, 2023

@intcreator

can you add a test case that illustrates this issue?

import { CronJob } from "cron";

const job = new CronJob({
    cronTime: "* * * * *",
    onTick: () => console.log("Tick"),
    runOnInit: true,
    startNow: false, // startNow is false by default anyway
});

It fires the tick every minute even if you never do job.start()

@intcreator
Copy link
Collaborator

intcreator commented Mar 10, 2023

I believe this is expected behavior for this option. there is an existing test that shows that the function is called as soon as the job is defined but before it is started. perhaps this could be clarified in the documentation

@ManuLpz4
Copy link

I believe this is expected behavior for this option. there is an existing test that shows that the function is called as soon as the job is defined but before it is started. perhaps this could be clarified in the documentation

I think it makes no sense since you can achieve it with the config but you cant achieve the other behavior :/

You can config runOnInit: true, startNow: true but you can't config runOnInit: true, startNow: false

@intcreator
Copy link
Collaborator

actually when I run the code example you provided it only runs once for me. what are you expecting runOnInit to do?

@ManuLpz4
Copy link

ManuLpz4 commented Mar 25, 2023

actually when I run the code example you provided it only runs once for me. what are you expecting runOnInit to do?

For "on init" we understand "on start" but if the cron didn't start yet I think the cron must not run.

How else can I achieve that behavior?

Btw: I really appreciate your work on it

@intcreator
Copy link
Collaborator

intcreator commented Mar 27, 2023

I do think it's kind of confusing but runOnInit runs the function when the cron job is initalized which is a different moment from when it is started.

import { CronJob } from "cron";

const job = new CronJob({
    cronTime: "* * * * *",
    onTick: () => console.log("Tick"),
    runOnInit: true,
    startNow: false,
});
// should see "Tick" once in the console even though job has not started

once you start the job you should see "Tick" in the console every second. if you are not starting the job but you are seeing "Tick" appear every second instead of just once, then that would be a bug. but you should be able to replicate that with a test case similar to the other test cases we have in the repo

@ManuLpz4
Copy link

ManuLpz4 commented Apr 5, 2023

I do think it's kind of confusing but runOnInit runs the function when the cron job is initalized which is a different moment from when it is started.

import { CronJob } from "cron";

const job = new CronJob({
    cronTime: "* * * * *",
    onTick: () => console.log("Tick"),
    runOnInit: true,
    startNow: false,
});
// should see "Tick" once in the console even though job has not started

once you start the job you should see "Tick" in the console every second. if you are not starting the job but you are seeing "Tick" appear every second instead of just once, then that would be a bug. but you should be able to replicate that with a test case similar to the other test cases we have in the repo

Oh ok, you are right! I understood. Maybe its a bit confusing

How can I run the job on start?

@intcreator
Copy link
Collaborator

I'm not sure there's a built in way to do that, but it should be easy enough to extract your job to a function and then call that function when you call job.start(). honestly I'm not sure runOnInit is the greatest feature but since we have it we might want to add a runOnStart as well. feel free to make a PR for that if you'd like that functionality

@intcreator
Copy link
Collaborator

@frbuceta is this the issue you were experiencing or was there a bug where runOnInit caused the job to start? I cannot reproduce that behavior in the latest version

I'm going to close this for now since it's an old issue that I can't duplicate and I think it's getting a bit off topic. I'm still open to adding a runOnStart option if someone wants to make a PR but technically that's a different issue than what this issue initially covered

@intcreator intcreator closed this as not planned Won't fix, can't repro, duplicate, stale Apr 6, 2023
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

No branches or pull requests

3 participants