[wp-hackers] wp_schedule_event fires three times

Peter Westwood peter.westwood at ftwr.co.uk
Thu Nov 11 16:10:00 UTC 2010


On 11 Nov 2010, at 15:26, Otto wrote:

> On Thu, Nov 11, 2010 at 4:38 AM, Javier Arques <j.arques at artvisual.net> wrote:
>> if (!wp_next_scheduled('generar_sitemap_foro_hook'))
>> wp_schedule_event(time() + 64800, 'daily',
>> 'generar_sitemap_foro_hook');
> 
> This is not really a safe way to schedule events. Basically, you have
> a race condition. What happens if I fire off three requests to your
> site in rapid succession? Before the first one has finished, another
> one hits? Websites are multithreaded. More than one copy of the site
> can run at a time, and you can't guarantee the code to run in a
> straight line.
> 
> Unfortunately, we don't have the equivalent of a mutex to work with.
> So you simply have to make your code smart enough to deal with the
> fact that it could run several times. Storing a time indicator in your
> function and updating it and then only running when it has elapsed
> might be a better way to go here.

A reliable way is to put the scheduling in your plugin activation hook - that tends to run in a less racey way :-)
-- 
Peter Westwood
http://blog.ftwr.co.uk | http://westi.wordpress.com
C53C F8FC 8796 8508 88D6 C950 54F4 5DCD A834 01C5



More information about the wp-hackers mailing list