通常使用Alternative Cron 更可靠。我试图以您的示例代码为基础制作一个演示,以复制所看到的内容:
<?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);
}
有了正常的wp cron功能和一组人为的并发流量,我们可以看到每个cron只有一个条目,尽管如此:
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\',
)
由于我无法完全复制您所看到的内容,因此这里提供更多信息可能会有所帮助。