Fix: Reschedule Event Error for Action Scheduler

[ad_1]

TL;DR: If you’re using WordPress 6.1+ and aren’t able to schedule any type of job with Action Scheduler, this article explains the problem and a potential fix.

Specifically, this seeks to address the following message appearing in WordPress 6.1+:

Cron reschedule event error for hook: action_scheduler_run_queue, Error code: invalid_schedule, Error message: Event schedule does not exist., Data: {"schedule":"every_minute","args":["WP Cron"],"interval":60}

Resolve the Reschedule Event Error

Introducing More Logging

Starting in WordPress 6.1, additional logging was added to WordPress core. Specifically, the patch responsible for this includes the following description:

Rarely and randomly some of my custom cron events have disappeared. From searching around, I’m not the only one with this issue, and no one else was able to figure out why. I also was unable to debug the issue since wp-cron.php doesn’t log any errors nor have any hooks to try handling them. This patch adds those in.

trac

Once this patch was introduced, it also resulted in others experiencing issues with cron and cron-related libraries starting from this ticket and then taking place in a specific forum post.

When you’re trying to create a schedule but it keeps blowing up.

And sure, these tickets are helpful as are the comments and the rest of the discussion in the forum. But there are times when we’re working on a specific task with a specific set of dependencies and need a specific solution.

Action Scheduler and Cron Jobs

My problem was this: I was trying to to register a job using Action Scheduler and the library wasn’t able to register the schedule because of the aforementioned problem.

So the fix was to add this function in my code:

/**
 * Additional logging in WordPress 6.1+ that generates the following
 * message regarding cron schedules:
 *
 * Cron reschedule event error for hook:
 * action_scheduler_run_queue,
 * Error code: invalid_schedule,
 * Error message: Event schedule does not exist.,
 * Data: {"schedule":"every_minute","args":["WP Cron"],"interval":60}
 *
 * This function needs to fire prior to loading Action Scheduler as its a
 * pre-requisite for it to schedule our tasks.
 *
 * This filter seeks to manually add the schedule to the list of schedules to
 * address this bug.
 */
add_filter('cron_schedules', function ($schedules) {
    $schedules['every_minute'] = [
        'interval' => 60,
        'display'  => 'Every Minute',
    ];
    return $schedules;
});

A few things about this code:

  • I don’t recommend using this as a permanent fix for every case. It’s a specific solution for a generic warning.
  • If your codebase is going to be distributed to wide audience, avoid anonymous functions. If you have control over the environment in which it will return, it may be fine.

On the other hand, if you’re using Action Scheduler, WordPress 6.1+, and are trying to register your own jobs and are seeing this message, this will ensure schedules Action Scheduler uses are available.

[ad_2]

Source link