From 277ad98011e5da91d28891f751a8e8a154071f0c Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 15 Mar 2026 07:56:40 +0000 Subject: [PATCH 1/3] Initial plan From acf211062fca6eb9a123e97746f69af83f0764a2 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 15 Mar 2026 08:02:45 +0000 Subject: [PATCH 2/3] Respect doing_cron transient in wp cron event run --due-now Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> --- features/cron-event.feature | 32 ++++++++++++++++++++++++++++++++ src/Cron_Event_Command.php | 21 ++++++++++++++++++++- 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/features/cron-event.feature b/features/cron-event.feature index 19186500..629efaa0 100644 --- a/features/cron-event.feature +++ b/features/cron-event.feature @@ -244,3 +244,35 @@ Feature: Manage WP Cron events """ Debug: Beginning execution of cron event 'wp_version_check' """ + + Scenario: --due-now respects the doing_cron transient and skips when another run is in progress + When I run `wp cron event schedule wp_cli_test_event_lock now hourly` + Then STDOUT should contain: + """ + Success: Scheduled event with hook 'wp_cli_test_event_lock' + """ + + # Simulate an in-progress cron run by setting the doing_cron transient to now. + When I run `wp eval 'set_transient( "doing_cron", sprintf( "%.22F", microtime( true ) ) );'` + + When I run `wp cron event run --due-now` + Then STDERR should contain: + """ + Warning: A cron event run is already in progress; skipping. + """ + And STDOUT should not contain: + """ + wp_cli_test_event_lock + """ + + # After the transient is cleared, the run should proceed normally. + When I run `wp transient delete doing_cron` + And I run `wp cron event run --due-now` + Then STDOUT should contain: + """ + Executed the cron event 'wp_cli_test_event_lock' + """ + And STDOUT should contain: + """ + Executed a total of 1 cron event + """ diff --git a/src/Cron_Event_Command.php b/src/Cron_Event_Command.php index 263deb64..362d3f0e 100644 --- a/src/Cron_Event_Command.php +++ b/src/Cron_Event_Command.php @@ -226,7 +226,8 @@ public function schedule( $args, $assoc_args ) { * : One or more hooks to run. * * [--due-now] - * : Run all hooks due right now. + * : Run all hooks due right now. Respects the doing_cron transient to + * prevent overlapping runs. * * [--exclude=] * : Comma-separated list of hooks to exclude. @@ -243,10 +244,24 @@ public function schedule( $args, $assoc_args ) { * Success: Executed a total of 2 cron events. */ public function run( $args, $assoc_args ) { + $due_now = Utils\get_flag_value( $assoc_args, 'due-now' ); + + if ( $due_now ) { + $lock_timeout = defined( 'WP_CRON_LOCK_TIMEOUT' ) ? WP_CRON_LOCK_TIMEOUT : 60; + $doing_cron_transient = get_transient( 'doing_cron' ); + if ( is_string( $doing_cron_transient ) && (float) $doing_cron_transient > microtime( true ) - $lock_timeout ) { + WP_CLI::warning( 'A cron event run is already in progress; skipping.' ); + return; + } + set_transient( 'doing_cron', sprintf( '%.22F', microtime( true ) ) ); + } $events = self::get_selected_cron_events( $args, $assoc_args ); if ( is_wp_error( $events ) ) { + if ( $due_now ) { + delete_transient( 'doing_cron' ); + } WP_CLI::error( $events ); } @@ -263,6 +278,10 @@ public function run( $args, $assoc_args ) { } } + if ( $due_now ) { + delete_transient( 'doing_cron' ); + } + $message = ( 1 === $executed ) ? 'Executed a total of %d cron event.' : 'Executed a total of %d cron events.'; WP_CLI::success( sprintf( $message, $executed ) ); } From 56dda5082c96a2804e3530ccb7d85735f4eb8421 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Sun, 15 Mar 2026 09:30:10 +0100 Subject: [PATCH 3/3] Apply suggestions from code review Co-authored-by: Pascal Birchler --- features/cron-event.feature | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/features/cron-event.feature b/features/cron-event.feature index 629efaa0..fd580d4a 100644 --- a/features/cron-event.feature +++ b/features/cron-event.feature @@ -255,7 +255,7 @@ Feature: Manage WP Cron events # Simulate an in-progress cron run by setting the doing_cron transient to now. When I run `wp eval 'set_transient( "doing_cron", sprintf( "%.22F", microtime( true ) ) );'` - When I run `wp cron event run --due-now` + And I try `wp cron event run --due-now` Then STDERR should contain: """ Warning: A cron event run is already in progress; skipping. @@ -267,7 +267,7 @@ Feature: Manage WP Cron events # After the transient is cleared, the run should proceed normally. When I run `wp transient delete doing_cron` - And I run `wp cron event run --due-now` + And I try `wp cron event run --due-now` Then STDOUT should contain: """ Executed the cron event 'wp_cli_test_event_lock'