Skip to content

Use wp_schedule_single_event() for Run Now to fix Cavalcade compatibility#276

Open
Gilmoursa wants to merge 1 commit into
johnbillion:developfrom
Gilmoursa:fix/cavalcade-run-now
Open

Use wp_schedule_single_event() for Run Now to fix Cavalcade compatibility#276
Gilmoursa wants to merge 1 commit into
johnbillion:developfrom
Gilmoursa:fix/cavalcade-run-now

Conversation

@Gilmoursa
Copy link
Copy Markdown

Summary

Fixes #56.

Clicking "Run Now" on an event produced a false "Failed to schedule the cron event" error for sites running Cavalcade (and likely other alternative cron runners). The event was actually created in Cavalcade's database, but WP Crontrol reported failure.

Root cause

force_schedule_single_event() bypassed wp_schedule_single_event() and called _set_cron_array() directly. This meant Cavalcade's pre_schedule_event filter hook never fired. Cavalcade's handling of the raw _set_cron_array() call returned a falsy value, triggering the false === $result check that produced the error.

Why force_schedule_single_event() existed

It was introduced to bypass WordPress's duplicate-event check, which (in older WP versions) would reject scheduling a new event when an identical one was already pending within 10 minutes of now.

Why it's no longer needed

Since WP 6.4 (this plugin's minimum), the duplicate check compares timestamps:

abs( $existing_timestamp - $new_timestamp ) <= 10 * MINUTE_IN_SECONDS

"Run Now" schedules at timestamp 1 (Unix epoch, 1970). The distance between epoch and any real future cron event is always orders of magnitude larger than 10 minutes, so the duplicate check never triggers.

Fix

Replace force_schedule_single_event() with wp_schedule_single_event( 1, $hookname, $event->args, true ). This:

  • Fires pre_schedule_event so Cavalcade (and others) can intercept correctly
  • Returns WP_Error on failure (WP 5.7+ $wp_error = true param)
  • Handles the duplicate_event error code defensively (treat as already-queued, not a failure)

force_schedule_single_event() is removed entirely as it's no longer used and was an internal implementation detail.

Test plan

  • Standard WP (no Cavalcade): "Run Now" works as before, event runs immediately
  • With Cavalcade: "Run Now" no longer shows a false error, event runs via Cavalcade
  • Clicking "Run Now" on an event due in the next few minutes works correctly

@johnbillion johnbillion force-pushed the fix/cavalcade-run-now branch from 1b23851 to 4cb41a1 Compare May 19, 2026 21:09
…lity

force_schedule_single_event() bypassed wp_schedule_single_event() to
avoid the duplicate-event check, but this meant third-party cron runners
like Cavalcade never saw the pre_schedule_event filter fire. The result
was a false "Failed to schedule" error even though the event was created
in Cavalcade's database.

Since the plugin requires WP 6.4+, the modern duplicate check uses
abs($existing - $new_timestamp) <= 10 minutes. Scheduling at timestamp 1
(epoch) means this distance is always enormous for any real event, so
the duplicate check never triggers. wp_schedule_single_event() is now
safe to use directly, which allows Cavalcade and other cron runners to
intercept the scheduling via their hooks.

The force_schedule_single_event() function is removed as it's no longer
used and was always an internal implementation detail.

Fixes johnbillion#56
@johnbillion johnbillion force-pushed the fix/cavalcade-run-now branch from 4cb41a1 to c4fe938 Compare May 19, 2026 21:14
@johnbillion
Copy link
Copy Markdown
Owner

Excellent spot, thanks. I rebased this PR to remove the commit with the capability changes. I'll do some testing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

"Run Now" doesn't work with Cavalcade

2 participants