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

Cron Job executing twice on a single scheduled job #489

Closed
Tracked by #674
izAL17x9iZmxoo423gvY opened this issue Apr 24, 2020 · 22 comments
Closed
Tracked by #674

Cron Job executing twice on a single scheduled job #489

izAL17x9iZmxoo423gvY opened this issue Apr 24, 2020 · 22 comments
Labels
type:bug Bug reports and bug fixes

Comments

@izAL17x9iZmxoo423gvY
Copy link

Hey so, every single time after the system runs for like 3 days, it will run into an issue whereby the cron job will simply execute twice on a single scheduled job.

@ncb000gt
Copy link
Member

Can you share aa base code sample that replicates what you're seeing (without any extra custom code)?

@izAL17x9iZmxoo423gvY
Copy link
Author

Yes, sure!

var job = new CronJob('0 0 * * *', function() { // do something }, null, true, 'America/New_York'); job.start();

@ncb000gt
Copy link
Member

Does the execution seem to always happen twice for every execution of the onTick after 3 days? Is it always 3 days?

I'm asking because I have an instance of this running myself and it doesn't perform this way and until somewhat recently I had it running for a couple months without the behavior your describing.

Is there something else that may be causing this? Have you reviewed your processes to make sure that only the one is running? Are you using a process manager to watch your processes and keep them alive (maybe there is something going on with that)?

@izAL17x9iZmxoo423gvY
Copy link
Author

Yes, it usually starts to run twice after 3 days. And I am able to fix it by changing the time to 1 minute earlier, however, 3 days later, the same issue will happen again. I have also used other npm cron such as the cron-scheduler however, the same issue still occurs. And no, I was not using any process manager.

@jeffminsungkim

This comment was marked as off-topic.

@MattCSmith
Copy link

I am also noticing this problem.
I have a digital ocean droplet, using PM2.
I have two Crons (00 59 23 * * * & 00 59 23 * * Wed)

Usually speaking they both start of fine for a couple of days and then they would start running an additional time every couple days. I would usually just restart the service and it would be good for a couple days.

However this time it started straight away,
I am currently in 16 days of uptime and this is how the 00 59 23 * * * cron has fired:

25th - 1 Time
26th - 2 Times
27th - 2 Times
28th - 3 Times
29th - 3 Times
30th - 4 Times
31st - 4 Times
1st - 4 Times
2nd - 4 Times
3rd - 5 Times
4th - 5 Times
5th - 5 Times
6th - 6 Times
7th - 6 Times
8th - 6 Times
9th - 7 Times
10th - 7 Times

@vikram1992
Copy link

Did anyone found a solution, mine is executing at-most 3 times.

@vikram1992
Copy link

Solved by running cron in fork mode rather than in cluster mode. But how can I distribute the cron task without executing the same task again in the next thread?

@tibfox
Copy link

tibfox commented Feb 11, 2022

I transfer funds with this cron job and I have to find a solution for it or I have to change my execution strategy to something wilder than this package. @vikram1992 how can I run it in "fork mode"?

@AbdlrahmanSaberAbdo

This comment was marked as off-topic.

@intcreator
Copy link
Collaborator

hey thanks for looking into it. we'll probably rewrite the _getNextDateFrom function as part of our roadmap for the next steps on this project. if you want you can join the Discord server and we can discuss how it works a little more informally

@sheerlox

This comment was marked as off-topic.

@sheerlox

This comment was marked as off-topic.

@sheerlox
Copy link
Collaborator

sheerlox commented Aug 15, 2023

These comments not being related to the original issue, I'm hiding them to reduce noise.

@sheerlox sheerlox added the type:bug Bug reports and bug fixes label Aug 15, 2023
@AbdlrahmanSaberAbdo

This comment was marked as off-topic.

@sheerlox

This comment was marked as off-topic.

@AbdlrahmanSaberAbdo

This comment was marked as off-topic.

@sheerlox

This comment was marked as off-topic.

@ianfirdaus31
Copy link

Try running the scheduler using fork mode instead of cluster mode if using pm2.

@sheerlox
Copy link
Collaborator

sheerlox commented Nov 4, 2023

Try running the scheduler using fork mode instead of cluster mode if using pm2.

Hey @ianfirdaus31 👋 did you encounter this issue recently?

@ianfirdaus31
Copy link

Try running the scheduler using fork mode instead of cluster mode if using pm2.

Hey @ianfirdaus31 👋 did you encounter this issue recently?

Nope, it was fixed by the way i explained above

@sheerlox
Copy link
Collaborator

Closing this since no one reported encountering this bug in more than a year.

Using cron in a clustered Node.js application

This is out of the scope of this package and is directly related to your design and implementation choices.
As an example, I've had to tackle that issue recently to balance the jobs load on an application and decided to go with only two instances so that I could use cluster.isPrimary & cluster.isWorker as the value for the start argument, effectively distributing my jobs between the primary and the worker.
More complex use cases with more than two instances will require a lot more thinking, and we'd be glad to discuss any approaches you might come up with to tackle that, and maybe come up with a standard solution we could add to the documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:bug Bug reports and bug fixes
Projects
None yet
Development

No branches or pull requests

10 participants