[wp-hackers] Dealing with WP cron's raciness

Alex King lists at alexking.org
Wed Sep 18 18:24:37 UTC 2013


WP-CRON uses a "run at least once" model - you need to account for this in your code. In our Social plugin we implemented a semaphore to handle the queue of things we needed to do *only once*, but that could take longer than the PHP timeout (multiple API calls).

Feel free to learn from/borrow that code:

https://github.com/crowdfavorite/wp-social

Cheers,
--Alex

http://alexking.org | http://crowdfavorite.com




On Sep 18, 2013, at 5:06 AM, David Anderson <david at wordshell.net> wrote:

> Hi,
> 
> The WP cron system suffers from race conditions. Is this known by core developers, and how do they feel about it? i.e. Is it here to stay, or are there plans to improve it?
> 
> Easy way to reproduce (every time):
> 
> 1) Load up the machine with your MySQL server on so that it's fully loaded (so that there's some latency - this is the crucial bit):
> (e.g. run a few of these: while true; do wget -O - http://localhost/mydevwebsite; done )
> 
> 2) Create a simple mu-plugin:
> 
> add_action('crontest', 'testing_crontest_da');
> function testing_crontest_da() { error_log('cron test!'); }
> 
> 3) Create a few processes that are continually calling wp-cron.php (two or more):
> while true; do wget -O - http://localhost/mydevwebsite/wp-cron.php; done
> 
> 4) Schedule a single cron event:
> 
> <?php require('wp-load.php'); wp_schedule_single_event(time()+5, 'crontest'); ?>
> 
> The result will be that the 'listener' in 2) logs as many lines as you had tasks calling wp-cron.php in 3). i.e. the action gets called that many times.
> 
> This isn't a problem for scheduled tasks that are performing cleanup jobs (e.g. cache purges). But for something like a scheduled backup, it's annoying - you get two backups. For a task that sends out report emails, you get multiple reports. etc. Of course, each plugin that performs such tasks could write its own locking system... but these would be likely to each have their own ad hoc quirks. It'd be much better if WP's cron system was less racy.
> 
> Thoughts?
> 
> David
> 
> -- 
> WordShell - WordPress fast from the CLI - www.wordshell.net
> 
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers



More information about the wp-hackers mailing list