web-dev-qa-db-ja.com

プラグインの複数のwp_schedule_event cronジョブにより、複数の実行が発生します

先週、私は毎日何百もの更新/削除と新しい投稿が追加される不動産ウェブサイトのいくつかのcronジョブに取り組んできました。私は今、終わりに近づいており、3つのWordPressイベントをスケジュールしてプロセスを処理しようとしています。

以下にリンクしたコードが実行され、出力(2番目と3番目のイベントはコメント化されています)を含む電子メールが送信されます。問題は、ジョブ2および3からコメントを削除するときに発生します。プラグインは正常にアクティブになりますが、ジョブの実行を開始すると、大量の重複した電子メールを受け取ります。

注**これは、イベントがスケジュールされている時間帯に何度もヒットする可能性のあるトラフィックの多いWebサイトです。私が行っていた読み物のいくつかは、wp-cronが長いスクリプトを扱ういくつかの既知の問題を抱えており、重複する可能性があることを述べました。

そうは言っても、私のcronジョブはwp-cron機能で処理するには大きすぎるのではないかと思っています。それは私のwp-config.phpでDISABLE_WP_CRONを強制し、ホストが提供するcronスケジューラーを使用しますか?または、プラグインの作成方法に問題がありますか?

前もって感謝します!

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
} 

追伸すべての正しいフォーマットに従ったことを願っています、これが私の最初の投稿です!

編集:私の質問を明確にするために、wp-cronメソッドを使用して同じプラグインからこれらの(2〜4メヌエット)関数を実行することは可能ですか?

3
Entrit

フックがすでにスケジュールされているかどうかを確認しないために問題が発生する可能性があります。

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

一般的に Alternative Cronisを使用すると、より信頼性が高くなります。見たものを再現するために、サンプルコードをベースとしてデモを作成しようとしました。

<?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ごとに1つのエントリのみが表示されます。

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',
)

私はあなたが見ているものを完全に再現することはできないので、ここでもう少し情報を入手すると役立つかもしれません。

0
futuernorn