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

When using go-cron to work, multiple services are started at the same time, and multiple cron tasks will be executed at the same time? What can be done to ensure that the cron task is only executed once? #476

Open
Jayleonc opened this issue Dec 29, 2022 · 2 comments

Comments

@Jayleonc
Copy link

Hi! When using go-cron to work, multiple services are started at the same time, and multiple cron tasks will be executed at the same time? What can be done to ensure that the cron task is only executed once?

@andrei-dascalu
Copy link

best way would be to use some sort of shared cache (like redis) to store the state for a period of time. For example, cron A from service 1 will set a key "cron_a_running" with a certain lifetime. The cron function should successfully finish (and remove the key) OR fail (and the key will live until expiration or until manually remove). The same cron A running from service 2 will check said key in redis and skip an execution if the key is there.

not a perfect solution - but this is just a simple cron manager and while I'm not part of the project, a system of clustering feels out of scope.

@G2G2G2G
Copy link

G2G2G2G commented Apr 2, 2023

Just have a global variable and check if it's "true" when the function starts and set it to true immediately faster

You're running multiple programs with the same cron jobs? that isn't what cron is for lol

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