Skip to content

Commit 1cf1163

Browse files
Copilotswissspidy
andcommitted
Set TTL on doing_cron transient; only delete if lock value still matches
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent 4a8c806 commit 1cf1163

1 file changed

Lines changed: 6 additions & 3 deletions

File tree

src/Cron_Event_Command.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -246,20 +246,23 @@ public function schedule( $args, $assoc_args ) {
246246
public function run( $args, $assoc_args ) {
247247
$due_now = Utils\get_flag_value( $assoc_args, 'due-now' );
248248

249+
$doing_cron_value = null;
250+
249251
if ( $due_now ) {
250252
$lock_timeout = defined( 'WP_CRON_LOCK_TIMEOUT' ) ? WP_CRON_LOCK_TIMEOUT : 60;
251253
$doing_cron_transient = get_transient( 'doing_cron' );
252254
if ( is_numeric( $doing_cron_transient ) && (float) $doing_cron_transient > microtime( true ) - $lock_timeout ) {
253255
WP_CLI::warning( 'A cron event run is already in progress; skipping.' );
254256
return;
255257
}
256-
set_transient( 'doing_cron', sprintf( '%.22F', microtime( true ) ) );
258+
$doing_cron_value = sprintf( '%.22F', microtime( true ) );
259+
set_transient( 'doing_cron', $doing_cron_value, $lock_timeout );
257260
}
258261

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

261264
if ( is_wp_error( $events ) ) {
262-
if ( $due_now ) {
265+
if ( $due_now && get_transient( 'doing_cron' ) === $doing_cron_value ) {
263266
delete_transient( 'doing_cron' );
264267
}
265268
WP_CLI::error( $events );
@@ -278,7 +281,7 @@ public function run( $args, $assoc_args ) {
278281
}
279282
}
280283

281-
if ( $due_now ) {
284+
if ( $due_now && get_transient( 'doing_cron' ) === $doing_cron_value ) {
282285
delete_transient( 'doing_cron' );
283286
}
284287

0 commit comments

Comments
 (0)