Make WordPress Core

Changeset 8927


Ignore:
Timestamp:
09/18/2008 07:09:38 AM (18 years ago)
Author:
azaozz
Message:

Cron improvement, props hailin, fixes #7068

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-cron.php

    r7991 r8927  
    2424        exit;
    2525
    26 if ( get_option('doing_cron') > time() )
    27         exit;
    28 
    29 update_option('doing_cron', time() + 30);
     26$local_time = time();
    3027
    3128$crons = _get_cron_array();
    32 $keys = array_keys($crons);
    33 if (!is_array($crons) || $keys[0] > time())
     29$keys = array_keys( $crons );
     30
     31if (!is_array($crons) || $keys[0] > $local_time) {
     32        update_option('doing_cron', 0);
    3433        return;
     34}
    3535
    36 foreach ($crons as $timestamp => $cronhooks) {
    37         if ($timestamp > time()) break;
     36foreach ($crons as $timestamp  => $cronhooks) {
     37
     38        if ( $timestamp > $local_time )
     39                break;
     40
    3841        foreach ($cronhooks as $hook => $keys) {
    39                 foreach ($keys as $key => $args) {
    40                         $schedule = $args['schedule'];
     42
     43                foreach ($keys as $k => $v) {
     44
     45                        $schedule = $v['schedule'];
     46
    4147                        if ($schedule != false) {
    42                                 $new_args = array($timestamp, $schedule, $hook, $args['args']);
     48                                $new_args = array($timestamp, $schedule, $hook, $v['args']);
    4349                                call_user_func_array('wp_reschedule_event', $new_args);
    4450                        }
    45                         wp_unschedule_event($timestamp, $hook, $args['args']);
    46                         do_action_ref_array($hook, $args['args']);
     51
     52                        wp_unschedule_event($timestamp, $hook, $v['args']);
     53
     54                        do_action_ref_array($hook, $v['args']);
    4755                }
    4856        }
     
    5159update_option('doing_cron', 0);
    5260
     61die();
     62
    5363?>
  • trunk/wp-includes/cron.php

    r8806 r8927  
    153153 * @return null Cron could not be spawned, because it is not needed to run.
    154154 */
    155 function spawn_cron() {
    156         $crons = _get_cron_array();
    157 
     155function spawn_cron( $local_time ) {
     156        global $current_blog;
     157
     158        /*
     159         * do not even start the cron if local server timer has drifted
     160         * such as due to power failure, or misconfiguration
     161         */
     162        $timer_accurate = check_server_timer( $local_time );
     163        if ( !$timer_accurate )
     164                return;
     165
     166        //sanity check
     167        $crons = _get_cron_array();
    158168        if ( !is_array($crons) )
    159169                return;
    160170
    161171        $keys = array_keys( $crons );
    162         if ( array_shift( $keys ) > time() )
     172        $timestamp =  $keys[0];
     173        if ( $timestamp > $local_time )
    163174                return;
    164175
    165176        $cron_url = get_option( 'siteurl' ) . '/wp-cron.php?check=' . wp_hash('187425');
     177        /*
     178        * multiple processes on multiple web servers can run this code concurrently
     179        * try to make this as atomic as possible by setting doing_cron switch
     180        */
     181        $flag = get_option('doing_cron');
     182
     183        // clean up potential invalid value resulted from various system chaos
     184        if ( $flag != 0 ) {
     185                if ( $flag > $local_time + 10*60 || $flag < $local_time - 10*60 ) {
     186                        update_option('doing_cron', 0);
     187                        $flag = 0;
     188                }
     189        }
     190
     191         //don't run if another process is currently running it
     192        if ( $flag > $local_time )
     193                return;
     194
     195        update_option( 'doing_cron', $local_time + 30 );
    166196
    167197        wp_remote_post($cron_url, array('timeout' => 0.01, 'blocking' => false));
     
    176206 */
    177207function wp_cron() {
     208
    178209        // Prevent infinite loops caused by lack of wp-cron.php
    179210        if ( strpos($_SERVER['REQUEST_URI'], '/wp-cron.php') !== false )
     
    189220                return;
    190221
     222        $local_time = time();
    191223        $schedules = wp_get_schedules();
    192224        foreach ( $crons as $timestamp => $cronhooks ) {
    193                 if ( $timestamp > time() ) break;
     225                if ( $timestamp > $local_time ) break;
    194226                foreach ( (array) $cronhooks as $hook => $args ) {
    195227                        if ( isset($schedules[$hook]['callback']) && !call_user_func( $schedules[$hook]['callback'] ) )
    196228                                continue;
    197                         spawn_cron();
     229                        spawn_cron( $local_time );
    198230                        break 2;
    199231                }
     
    328360}
    329361
     362// stub for checking server timer accuracy, using outside standard time sources
     363function check_server_timer( $local_time ) {
     364        return true;
     365}
     366
    330367?>
Note: See TracChangeset for help on using the changeset viewer.