I have a user using my plugin that has multiple AWS instances hosting WP and pointing to the same database. One of the jobs of my plugins is to run a large number of small jobs.
I was thinking of using wp_schedule_single_event to schedule the jobs but I’m not sure how that will work with multiple instances. Each job will have unique parameters so there won’t be any duplication.
Question:
Will each instance try to run the same job or will WordPress be able to resolve this so that each job is only run once?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Question: Will each instance try to run the same job
Yes! If you have 5 machines running the same site, they’ll all try to run the cron job. It’ll be inconsistent, but if machine 1 triggers WP Cron and there’s an event ready to run, and machine 2 triggers WP Cron, then machine 2 will also see that event if the timing is right and attempt to process it.
or will WordPress be able to resolve this so that each job is only run once?
Nope, it can’t. You can try adding a lock of sorts, but it can’t fix the problem, only prevent it in some scenarios
What you really need is to disable WP Cron in your config and run it via a system level cron job on a single machine. Or better yet, install a cron service such as cavalcade
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0