Make WordPress Core

Ticket #51716: 51716.diff

File 51716.diff, 3.1 KB (added by Takahashi_Fumiki, 5 years ago)

Separate scheduling and execution on WP-Cron task.

  • wp-cron.php

     
    11<?php
    22/**
    3  * A pseudo-cron daemon for scheduling WordPress tasks.
     3 * A pseudo-CRON daemon for scheduling WordPress tasks
    44 *
    5  * WP-Cron is triggered when the site receives a visit. In the scenario
     5 * WP Cron is triggered when the site receives a visit. In the scenario
    66 * where a site may not receive enough visits to execute scheduled tasks
    77 * in a timely manner, this file can be called directly or via a server
    8  * cron daemon for X number of times.
     8 * CRON daemon for X number of times.
    99 *
    1010 * Defining DISABLE_WP_CRON as true and calling this file directly are
    1111 * mutually exclusive and the latter does not rely on the former to work.
    1212 *
    1313 * The HTTP request to this file will not slow down the visitor who happens to
    14  * visit when a scheduled cron event runs.
     14 * visit when the cron job is needed to run.
    1515 *
    1616 * @package WordPress
    1717 */
     
    3333}
    3434
    3535/**
    36  * Tell WordPress we are doing the cron task.
     36 * Tell WordPress we are doing the CRON task.
    3737 *
    3838 * @var bool
    3939 */
     
    109109        return;
    110110}
    111111
     112/*
     113 * Store cron job to be executed.
     114 */
     115$cron_jobs = [];
     116
     117// Schedule and reschedule cron jobs.
    112118foreach ( $crons as $timestamp => $cronhooks ) {
    113119        if ( $timestamp > $gmt_time ) {
    114120                break;
     
    126132
    127133                        wp_unschedule_event( $timestamp, $hook, $v['args'] );
    128134
    129                         /**
    130                          * Fires scheduled events.
    131                          *
    132                          * @ignore
    133                          * @since 2.1.0
    134                          *
    135                          * @param string $hook Name of the hook that was scheduled to be fired.
    136                          * @param array  $args The arguments to be passed to the hook.
    137                          */
    138                         do_action_ref_array( $hook, $v['args'] );
     135                        // Register jobs to be executed.
     136                        $cron_jobs[] = [
     137                                'timestamp' => $timestamp,
     138                                'hook'      => $hook,
     139                                'args'      => isset( $v['args'] ) ? (array) $v['args'] : [],
     140                                'schedule'  => $schedule,
     141                        ];
    139142
    140143                        // If the hook ran too long and another cron process stole the lock, quit.
    141144                        if ( _get_cron_lock() !== $doing_wp_cron ) {
     145                                do_action( 'wp_cron_schedule_aborted' );
    142146                                return;
    143147                        }
    144148                }
     
    149153        delete_transient( 'doing_cron' );
    150154}
    151155
     156// Let's do the cron jobs.
     157foreach ( $cron_jobs as $job ) {
     158        /**
     159         * Get cron executor.
     160         *
     161         * By default, `do_action_ref_array` will called.
     162         *
     163         * @since 5.6.0
     164         *
     165         * @param array $jobs Cron task to be executed.
     166         */
     167        $callback = apply_filters( 'wp_cron_job_callback', 'do_action_ref_array', $job );
     168        try {
     169                do_action( 'wp_cron_before_execution', $job );
     170                /**
     171                 * Fires scheduled events by callback.
     172                 *
     173                 * By default, `do_action_ref_array` will called.
     174                 *
     175                 * @ignore
     176                 * @since 2.1.0
     177                 * @since 5.6.0 This hook runs by default, but can be overridden.
     178                 *
     179                 * @param string $hook Name of the hook that was scheduled to be fired.
     180                 * @param array  $args The arguments to be passed to the hook.
     181                 */
     182                call_user_func_array( $callback, [ $job['hook'], $job['args'] ] );
     183                do_action( 'wp_cron_after_execution', $job );
     184        } catch ( Exception $e ) {
     185                do_action( 'wp_cron_execution_error', $e );
     186        }
     187}
     188
    152189die();