Skip to content

Commit d795834

Browse files
Copilotswissspidy
andcommitted
Use wp_cli_doing_cron transient instead of value prefix trick
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent 1d156d4 commit d795834

2 files changed

Lines changed: 8 additions & 17 deletions

File tree

features/cron-event.feature

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

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

259258
And I try `wp cron event run --due-now`
260259
Then STDERR should contain:
@@ -267,7 +266,7 @@ Feature: Manage WP Cron events
267266
"""
268267

269268
# After the transient is cleared, the run should proceed normally.
270-
When I run `wp eval 'delete_transient( "doing_cron" );'`
269+
When I run `wp eval 'delete_transient( "wp_cli_doing_cron" );'`
271270
And I try `wp cron event run --due-now`
272271
Then STDOUT should contain:
273272
"""

src/Cron_Event_Command.php

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -248,27 +248,19 @@ public function run( $args, $assoc_args ) {
248248

249249
if ( $due_now ) {
250250
$lock_timeout = defined( 'WP_CRON_LOCK_TIMEOUT' ) ? WP_CRON_LOCK_TIMEOUT : 60;
251-
$doing_cron_transient = get_transient( 'doing_cron' );
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-
) {
251+
$doing_cron_transient = get_transient( 'wp_cli_doing_cron' );
252+
if ( is_numeric( $doing_cron_transient ) && (float) $doing_cron_transient > microtime( true ) - $lock_timeout ) {
261253
WP_CLI::warning( 'A cron event run is already in progress; skipping.' );
262254
return;
263255
}
264-
set_transient( 'doing_cron', 'wpcli_' . sprintf( '%.22F', microtime( true ) ) );
256+
set_transient( 'wp_cli_doing_cron', sprintf( '%.22F', microtime( true ) ) );
265257
}
266258

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

269261
if ( is_wp_error( $events ) ) {
270262
if ( $due_now ) {
271-
delete_transient( 'doing_cron' );
263+
delete_transient( 'wp_cli_doing_cron' );
272264
}
273265
WP_CLI::error( $events );
274266
}
@@ -287,7 +279,7 @@ public function run( $args, $assoc_args ) {
287279
}
288280

289281
if ( $due_now ) {
290-
delete_transient( 'doing_cron' );
282+
delete_transient( 'wp_cli_doing_cron' );
291283
}
292284

293285
$message = ( 1 === $executed ) ? 'Executed a total of %d cron event.' : 'Executed a total of %d cron events.';

0 commit comments

Comments
 (0)