My environment:
wordpress –> myplugin
in myplugin I use wp-loaded action to trigger my cronfunc to run
my cronfunc pseudo-code:
add_action('wp_loaded','cronfunc');
function cronfunc()
{
if(defined('IN_MY_CRON_FUNC'))
return;
define('IN_MY_CRON_FUNC',true);
if nowtime - get_option('last_do_work_time') > 3600:
update_option('last_do_work_time',nowtime);
run cronjob
}
The problem is sometimes cronfunc will run twice!
Somebody tell me why?
I can’t seem to get it to load twice on a manually set plugin. Here is the code:
Saved as test.php and put into a folder in plugins and activated it. Does it happen a lot or just once in a while? It’s possible that they are getting past IF before being updated again. If that is the case, you may need to lock the option and wait or skip if it’s already being run. You might need to use semaphores or create something like it using the database… maybe you could use wordpress options to define a kind of mutex or something. DEFINE() isn’t going to help, because if it’s run again side by side, then they won’t affect each other.
here is probably a bad way to do it, but it might work for now:
EDIT: put the lock around the option and returned if can’t get… a little sloppy, but just for the idea