I’m looking for PHP solutions for running cronjobs over multiple servers and guaranteeing that only a single server runs these cronjobs automatically. We have cronjobs that we need to run only once, like daily digest emails or weekly reports.
Right now, we have a “master” server which has the crontab installed, and multiple “normal” servers which only have apache installed on them. The issue is that if the “master” server fails, nobody will run the cronjob anymore. It also means we need to keep track on which server is the master and it’s creating some scaling issues for us.
Are there any ready-made php solutions for running unique tasks by multiple servers?
I have looked at gearman (http://www.slideshare.net/felixdv/high-gear-php-with-gearman), but it’s a little too complex . I just need to guarantee that only one server out of the farm runs the cronjobs.
I build my concurrency and ‘running’ checks in the scripts themselves.
Update a ‘lock’ in the database or in memcached that states the last execution time.
If that lock is present, bail in the other copies of the script… unless the lock is too old.
If the lock has been sitting around > max_execution, the script failed or ran too long and never unlocked. Email yourself on that condition.
Remember to unset the lock at script close.