Skip to content

Commit 1d156d4

Browse files
Copilotswissspidy
andcommitted
Use wpcli_ prefix to distinguish WP-CLI cron lock from spawn_cron transient
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent 59b9a4e commit 1d156d4

2 files changed

Lines changed: 13 additions & 4 deletions

File tree

features/cron-event.feature

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,9 @@ Feature: Manage WP Cron events
252252
Success: Scheduled event with hook 'wp_cli_test_event_lock'
253253
"""
254254

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 ) ) );'`
255+
# Simulate an in-progress WP-CLI cron run by setting the doing_cron transient
256+
# with the 'wpcli_' prefix that WP-CLI uses.
257+
When I run `wp eval 'set_transient( "doing_cron", "wpcli_" . sprintf( "%.22F", microtime( true ) ) );'`
257258

258259
And I try `wp cron event run --due-now`
259260
Then STDERR should contain:

src/Cron_Event_Command.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,11 +249,19 @@ public function run( $args, $assoc_args ) {
249249
if ( $due_now ) {
250250
$lock_timeout = defined( 'WP_CRON_LOCK_TIMEOUT' ) ? WP_CRON_LOCK_TIMEOUT : 60;
251251
$doing_cron_transient = get_transient( 'doing_cron' );
252-
if ( is_string( $doing_cron_transient ) && (float) $doing_cron_transient > microtime( true ) - $lock_timeout ) {
252+
// Only treat the lock as belonging to another WP-CLI run when WP-CLI
253+
// itself set it (value starts with 'wpcli_'). WordPress's spawn_cron()
254+
// always stores a plain numeric microtime string, so we must not block
255+
// ourselves on a transient that was set by spawn_cron during bootstrap.
256+
if (
257+
is_string( $doing_cron_transient ) &&
258+
0 === strncmp( $doing_cron_transient, 'wpcli_', 6 ) &&
259+
(float) substr( $doing_cron_transient, 6 ) > microtime( true ) - $lock_timeout
260+
) {
253261
WP_CLI::warning( 'A cron event run is already in progress; skipping.' );
254262
return;
255263
}
256-
set_transient( 'doing_cron', sprintf( '%.22F', microtime( true ) ) );
264+
set_transient( 'doing_cron', 'wpcli_' . sprintf( '%.22F', microtime( true ) ) );
257265
}
258266

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

0 commit comments

Comments
 (0)