Skip to content

Commit acf2110

Browse files
Copilotswissspidy
andcommitted
Respect doing_cron transient in wp cron event run --due-now
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent 277ad98 commit acf2110

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

features/cron-event.feature

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,3 +244,35 @@ Feature: Manage WP Cron events
244244
"""
245245
Debug: Beginning execution of cron event 'wp_version_check'
246246
"""
247+
248+
Scenario: --due-now respects the doing_cron transient and skips when another run is in progress
249+
When I run `wp cron event schedule wp_cli_test_event_lock now hourly`
250+
Then STDOUT should contain:
251+
"""
252+
Success: Scheduled event with hook 'wp_cli_test_event_lock'
253+
"""
254+
255+
# Simulate an in-progress cron run by setting the doing_cron transient to now.
256+
When I run `wp eval 'set_transient( "doing_cron", sprintf( "%.22F", microtime( true ) ) );'`
257+
258+
When I run `wp cron event run --due-now`
259+
Then STDERR should contain:
260+
"""
261+
Warning: A cron event run is already in progress; skipping.
262+
"""
263+
And STDOUT should not contain:
264+
"""
265+
wp_cli_test_event_lock
266+
"""
267+
268+
# After the transient is cleared, the run should proceed normally.
269+
When I run `wp transient delete doing_cron`
270+
And I run `wp cron event run --due-now`
271+
Then STDOUT should contain:
272+
"""
273+
Executed the cron event 'wp_cli_test_event_lock'
274+
"""
275+
And STDOUT should contain:
276+
"""
277+
Executed a total of 1 cron event
278+
"""

src/Cron_Event_Command.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,8 @@ public function schedule( $args, $assoc_args ) {
226226
* : One or more hooks to run.
227227
*
228228
* [--due-now]
229-
* : Run all hooks due right now.
229+
* : Run all hooks due right now. Respects the doing_cron transient to
230+
* prevent overlapping runs.
230231
*
231232
* [--exclude=<hooks>]
232233
* : Comma-separated list of hooks to exclude.
@@ -243,10 +244,24 @@ public function schedule( $args, $assoc_args ) {
243244
* Success: Executed a total of 2 cron events.
244245
*/
245246
public function run( $args, $assoc_args ) {
247+
$due_now = Utils\get_flag_value( $assoc_args, 'due-now' );
248+
249+
if ( $due_now ) {
250+
$lock_timeout = defined( 'WP_CRON_LOCK_TIMEOUT' ) ? WP_CRON_LOCK_TIMEOUT : 60;
251+
$doing_cron_transient = get_transient( 'doing_cron' );
252+
if ( is_string( $doing_cron_transient ) && (float) $doing_cron_transient > microtime( true ) - $lock_timeout ) {
253+
WP_CLI::warning( 'A cron event run is already in progress; skipping.' );
254+
return;
255+
}
256+
set_transient( 'doing_cron', sprintf( '%.22F', microtime( true ) ) );
257+
}
246258

247259
$events = self::get_selected_cron_events( $args, $assoc_args );
248260

249261
if ( is_wp_error( $events ) ) {
262+
if ( $due_now ) {
263+
delete_transient( 'doing_cron' );
264+
}
250265
WP_CLI::error( $events );
251266
}
252267

@@ -263,6 +278,10 @@ public function run( $args, $assoc_args ) {
263278
}
264279
}
265280

281+
if ( $due_now ) {
282+
delete_transient( 'doing_cron' );
283+
}
284+
266285
$message = ( 1 === $executed ) ? 'Executed a total of %d cron event.' : 'Executed a total of %d cron events.';
267286
WP_CLI::success( sprintf( $message, $executed ) );
268287
}

0 commit comments

Comments
 (0)