3

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?

0

In general using the Alternative Cron is more reliable. I tried to make a demo using your example code as the basis in order to replicate what was being seen:

<?php
/*
* Plugin Name: Overlapping Cron Test
* Author: futuernorn
*/

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

function cron_activation(){
  wp_schedule_event(time()+30, 'daily', 'first_hook');
  wp_schedule_event(time()+60, 'hourly', 'second_hook');
  wp_schedule_event(time()+90, '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
  sleep(30);
  $crontest_data = get_option( '_crontest_data' );
  $crontest_data[] = 'first_hook_completed: '.date('Y-m-d H:i:s');
  update_option('_crontest_data', $crontest_data);
}

function second_hook_function(){
  //code to run takes 3 minutes.
  //sends output as email to me for inspection\
  sleep(60);
  $crontest_data = get_option( '_crontest_data' );
  $crontest_data[] = 'second_hook_completed: '.date('Y-m-d H:i:s');
  update_option('_crontest_data', $crontest_data);
}

function third_hook_function(){
  //code to run takes 4 minutes.
  //sends output as email to me for inspection
  sleep(90);
  $crontest_data = get_option( '_crontest_data' );
  $crontest_data[] = 'third_hook_completed: '.date('Y-m-d H:i:s');
  update_option('_crontest_data', $crontest_data);
}

With the normal wp-cron functionality in place, and a contrived set of concurrent traffic, we see just one entry per cron even however:

testsite$ wp plugin activate crontest
Success: Plugin 'crontest' activated.
testsite$ for i in {1..10}; do curl -b -s $WEBSITE_URL 1>/dev/null; done
testsite$ for i in {1..10}; do curl -s $WEBSITE_URL 1>/dev/null; done
testsite$ wp option get _crontest_data
array (
  0 => 'first_hook_completed: 2015-11-09 19:49:25',
  1 => 'second_hook_completed: 2015-11-09 19:50:25',
  2 => 'third_hook_completed: 2015-11-09 19:51:55',
)

Since I can not quite replicate what you're seeing, it may help to have a bit more information here.

1

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');
}
New contributor
Kirill is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.