Make WordPress Core


Ignore:
Timestamp:
02/07/2009 01:32:34 PM (16 years ago)
Author:
azaozz
Message:

Cron spawning improvement, see #9005

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/cron.php

    r10519 r10521  
    161161 * @return null Cron could not be spawned, because it is not needed to run.
    162162 */
    163 function spawn_cron( $local_time ) {
     163function spawn_cron( $local_time = 0 ) {
     164
     165    if ( !$local_time )
     166        $local_time = time();
     167
     168    if ( defined('DOING_CRON') || isset($_GET['doing_wp_cron']) )
     169        return;
    164170
    165171    /*
     
    171177        return;
    172178
    173     //sanity check
    174     $crons = _get_cron_array();
    175     if ( !is_array($crons) )
    176         return;
    177 
    178     $keys = array_keys( $crons );
    179     $timestamp =  $keys[0];
    180     if ( $timestamp > $local_time )
    181         return;
    182 
    183179    /*
    184180    * multiple processes on multiple web servers can run this code concurrently
     
    187183    $flag = get_transient('doing_cron');
    188184
    189     // clean up potential invalid value resulted from various system chaos
    190     if ( $flag != 0 ) {
    191         if ( $flag > $local_time + 10*60 || $flag < $local_time - 10*60 ) {
    192             set_transient('doing_cron', 0);
    193             $flag = 0;
    194         }
    195     }
    196 
    197     //don't run if another process is currently running it
    198     if ( $flag > $local_time )
    199         return;
    200 
    201     set_transient( 'doing_cron', $local_time + 30 );
    202 
    203     add_action('wp_head', 'spawn_cron_request');
     185    if ( $flag > $local_time + 10*60 )
     186        $flag = 0;
     187
     188    // don't run if another process is currently running it or more than once every 60 sec.
     189    if ( $flag + 60 > $local_time )
     190        return;
     191
     192    //sanity check
     193    $crons = _get_cron_array();
     194    if ( !is_array($crons) )
     195        return;
     196
     197    $keys = array_keys( $crons );
     198    if ( isset($keys[0]) && $keys[0] > $local_time )
     199        return;
     200
     201    if ( defined('ALTERNATE_WP_CRON') && ALTERNATE_WP_CRON ) {
     202        if ( !empty($_POST) || defined('DOING_AJAX') )
     203            return;
     204
     205        set_transient( 'doing_cron', $local_time );
     206
     207        ob_start();
     208        wp_redirect( add_query_arg('doing_wp_cron', '', stripslashes($_SERVER['REQUEST_URI'])) );
     209        echo ' ';
     210
     211        // flush any buffers and send the headers
     212        while ( @ob_end_flush() );
     213        flush();
     214
     215        @include_once(ABSPATH . 'wp-cron.php');
     216        return;
     217    }
     218
     219    set_transient( 'doing_cron', $local_time );
     220
     221    $cron_url = get_option( 'siteurl' ) . '/wp-cron.php?doing_wp_cron';
     222    wp_remote_post( $cron_url, array('timeout' => 0.01, 'blocking' => false) );
    204223}
    205224
     
    214233
    215234    // Prevent infinite loops caused by lack of wp-cron.php
    216     if ( strpos($_SERVER['REQUEST_URI'], '/wp-cron.php') !== false )
    217         return;
    218 
    219     $crons = _get_cron_array();
    220 
    221     if ( !is_array($crons) )
    222         return;
    223 
     235    if ( strpos($_SERVER['REQUEST_URI'], '/wp-cron.php') !== false || ( defined('DISABLE_WP_CRON') && DISABLE_WP_CRON ) )
     236        return;
     237
     238    if ( false === $crons = _get_cron_array() )
     239        return;
     240
     241    $local_time = time();
    224242    $keys = array_keys( $crons );
    225     if ( isset($keys[0]) && $keys[0] > time() )
    226         return;
    227 
    228     $local_time = time();
     243    if ( isset($keys[0]) && $keys[0] > $local_time )
     244        return;
     245
    229246    $schedules = wp_get_schedules();
    230247    foreach ( $crons as $timestamp => $cronhooks ) {
     
    371388}
    372389
    373 function spawn_cron_request() {
    374390?>
    375 <script type="text/javascript">
    376 /* <![CDATA[ */
    377 window.setTimeout(function(){var x;if(window.XMLHttpRequest){x=new XMLHttpRequest();}else{try{x=new ActiveXObject('Msxml2.XMLHTTP');}catch(e){try{x=new ActiveXObject('Microsoft.XMLHTTP');}catch(e){};}}if(x){x.open('GET','<?php echo get_option('siteurl'); ?>/wp-cron.php?'+(new Date()).getTime(), true);x.send('');}},10);
    378 /* ]]> */
    379 </script>
    380 <?php
    381 }
    382 
    383 ?>
Note: See TracChangeset for help on using the changeset viewer.