robfig/cron

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?

Jayleonc opened this issue · 2 comments

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?

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.

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