Make WordPress Core


Ignore:
Timestamp:
09/20/2022 03:15:55 PM (4 years ago)
Author:
audrasjb
Message:

Cron API: Add error logging and hooks to wp-cron.php.

This changeset adds error logging on wp_reschedule_event() and wp_unschedule_event in wp-cron.php. This allows proper error logging when random errors appear. It also introduces cron_reschedule_event_error and cron_unschedule_event_error hooks which can be used to trigger additional behaviors when an error occurs.

Props Enchiridion, johnbillion, costdev.
Fixes #56048.

File:
1 edited

Legend:

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

    r53653 r54258  
    124124
    125125                        if ( $schedule ) {
    126                                 wp_reschedule_event( $timestamp, $schedule, $hook, $v['args'] );
     126                                $result = wp_reschedule_event( $timestamp, $schedule, $hook, $v['args'], true );
     127
     128                                if ( is_wp_error( $result ) ) {
     129                                        error_log(
     130                                                sprintf(
     131                                                        /* translators: 1: Hook name, 2: Error code, 3: Error message, 4: Event data. */
     132                                                        __( 'Cron reschedule event error for hook: %1$s, Error code: %2$s, Error message: %3$s, Data: %4$s' ),
     133                                                        $hook,
     134                                                        $result->get_error_code(),
     135                                                        $result->get_error_message(),
     136                                                        wp_json_encode( $v )
     137                                                )
     138                                        );
     139
     140                                        /**
     141                                         * Fires when an error happens rescheduling a cron event.
     142                                         *
     143                                         * @since 6.1.0
     144                                         *
     145                                         * @param WP_Error $result The WP_Error object.
     146                                         * @param string   $hook   Action hook to execute when the event is run.
     147                                         * @param array    $v      Event data.
     148                                         */
     149                                        do_action( 'cron_reschedule_event_error', $result, $hook, $v );
     150                                }
    127151                        }
    128152
    129                         wp_unschedule_event( $timestamp, $hook, $v['args'] );
     153                        $result = wp_unschedule_event( $timestamp, $hook, $v['args'], true );
     154
     155                        if ( is_wp_error( $result ) ) {
     156                                error_log(
     157                                        sprintf(
     158                                                /* translators: 1: Hook name, 2: Error code, 3: Error message, 4: Event data. */
     159                                                __( 'Cron unschedule event error for hook: %1$s, Error code: %2$s, Error message: %3$s, Data: %4$s' ),
     160                                                $hook,
     161                                                $result->get_error_code(),
     162                                                $result->get_error_message(),
     163                                                wp_json_encode( $v )
     164                                        )
     165                                );
     166
     167                                /**
     168                                 * Fires when an error happens unscheduling a cron event.
     169                                 *
     170                                 * @since 6.1.0
     171                                 *
     172                                 * @param WP_Error $result The WP_Error object.
     173                                 * @param string   $hook   Action hook to execute when the event is run.
     174                                 * @param array    $v      Event data.
     175                                 */
     176                                do_action( 'cron_unschedule_event_error', $result, $hook, $v );
     177                        }
    130178
    131179                        /**
Note: See TracChangeset for help on using the changeset viewer.