over the last week I have been working on some cron jobs for a real-estate website that has hundreds of updates/deletes and new posts added each day. I am very near the end now and looking to schedule three WordPress events to take care of the process.

The code I have linked below runs and send a email to me with the output (with the second and third events commented out). The problem comes when I remove the comments from jobs 2 and 3. The plugin activates fine but when the jobs start running I get a lot of duplicate emails.

NOTE** This is a high traffic website that may get hit many times around the time the event is scheduled. Some of the reading I was doing mention that the wp-cron has had some known issues dealing with long scripts and can overlap.

That being said I am wondering if my cron jobs are too big for the wp-cron functionality to handle. Does that force me to DISABLE_WP_CRON in my wp-config.php and go with the cron scheduler provided by my host? Or is my problem in how I have created my plugin?

Thanks in advance!

register_activation_hook(__FILE__,'cron_activation');
register_deactivation_hook(__FILE__,'cron_deactivation');

function cron_activation(){  
  wp_schedule_event(time()+60, 'daily', 'first_hook');
  //wp_schedule_event(time()+420, 'hourly', 'second_hook');
  //wp_schedule_event(time()+840, 'hourly', 'third_hook');
}

function cron_deactivation(){  
  wp_clear_scheduled_hook('first_hook');
  //wp_clear_scheduled_hook('second_hook');
  //wp_clear_scheduled_hook('third_hook');
}


add_action( 'first_hook', 'first_hook_function' );
//add_action( 'second_hook', 'second_hook_function' );
//add_action( 'third_hook', 'third_hook_function' );

function first_hook_function(){ 
  //code to run takes 2 minutes.
  //sends output as email to me for inspection
}

function second_hook_function(){
  //code to run takes 3 minutes.
  //sends output as email to me for inspection
}

function third_hook_function(){
  //code to run takes 4 minutes.
  //sends output as email to me for inspection
} 

P.S. I hope I followed all the correct format, this is my first post!

Edit: To clarify my questions, is it possible to run these (2 to 4 minuet) functions all from the same plugin using the wp-cron method?

2 Answers
2

Maybe the problem happens because you don’t check if your hook is already scheduled.

if ( !wp_next_scheduled( 'first_hook' ) ) {
    wp_schedule_event(time()+60, 'daily', 'first_hook');
}

Leave a Reply

Your email address will not be published. Required fields are marked *