Given a site running on server A and server B with a load balancer, shared database/etc, and a regular wp-cron task, how would I make sure that wp-cron only runs on server A, never on server B?
Leave a Reply
You must be logged in to post a comment.
Given a site running on server A and server B with a load balancer, shared database/etc, and a regular wp-cron task, how would I make sure that wp-cron only runs on server A, never on server B?
You must be logged in to post a comment.
I would create a constant in the
wp_config.php
defining which Server you are on. So far so good, not really magic.The tricky part, as you mentioned is to get the cron running for sure, and not aborting if it’s accidentally the wrong server. Try the function like this:
Now, you have a transient set when the cron would run on the other server, but is not executed. The Function
do_the_cron_action
contains your script.The last step is to hook a function to the
init
, checking if the server is ServerA and if the transient is set.That should do the trick.
I’m currently testing this in wp-config.php:
Where wp4 is the name of the second server in the setup