[wp-hackers] wp_schedule_event fires three times

Otto otto at ottodestruct.com
Thu Nov 11 15:26:48 UTC 2010


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.

-Otto


More information about the wp-hackers mailing list