Skip to content

[Performance] Use the WordPress caching layer for the option lock component.#1342

Merged
kalessil merged 2 commits into
trunkfrom
performance/option-lock-enhancements
Jul 10, 2026
Merged

[Performance] Use the WordPress caching layer for the option lock component.#1342
kalessil merged 2 commits into
trunkfrom
performance/option-lock-enhancements

Conversation

@kalessil

@kalessil kalessil commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Introduces the following changes to the \ActionScheduler_OptionLock component:

  • Capture current timestamps once, where applicable, rather than using in-place calls (reliability).
  • Use the WordPress cache for active locks to reduce SQL queries and decrease asynchronous dispatch latency during shutdown (micro-optimization).

Manual testing:

  • Build the plugin and install it on a testing site as a standalone plugin
  • Add the snippet below (AI-slop, but works), e.g., as a mu-plugin or to another place where it'll be executed
  • Navigate to the admin area (with and without persistent object cache being active)
  • Refresh the page to monitor and verify the lock acquisition
add_action( 'admin_notices', function () {                                                                                                                                                                   
        $lock      = ActionScheduler::lock();                                                                                                                                                                  
        $lock_type = 'async-request-runner';                                                                                                                                                                   
        $is_locked  = $lock->is_locked( $lock_type );                                                                                                                                                          
        $expiration = $lock->get_expiration( $lock_type );
                                                                                                                                                                                                               
        $status = $is_locked                                                                                                                                                                                 
                ? sprintf(                                                                                                                                                                                     
                        '<strong>LOCKED</strong> — expires %s (in %ds)',                                                                                                                                     
                        esc_html( date( 'H:i:s', $expiration ) ),                                                                                                                                              
                        max( 0, $expiration - time() )                                                                                                                                                         
                )                                                                                                                                                                                              
                : '<strong>NOT LOCKED</strong>';                                                                                                                                                               
                                                                                                                                                                                                             
        printf(                                                                                                                                                                                                
                '<div class="notice notice-%s"><p>ActionScheduler lock <code>%s</code>: %s</p></div>',
                $is_locked ? 'warning' : 'info',                                                                                                                                                               
                esc_html( $lock_type ),                                                                                                                                                                      
                $status                                                                                                                                                                                        
        );                                                                                                                                                                                                     
  } );

@kalessil kalessil self-assigned this Jul 8, 2026

@barryhughes barryhughes left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a nice approach to me, and I think will add a small improvement even when there is no persistent caching layer (because ActionScheduler_QueueRunner::maybe_dispatch_async_request() effectively reads the lock twice in the same request).

I realize it's a draft, so this is likely planned, but definitely worth additional tests.

Comment thread classes/ActionScheduler_OptionLock.php Outdated
@kalessil
kalessil force-pushed the performance/option-lock-enhancements branch from 6b4656f to cb1bfa4 Compare July 9, 2026 08:30
@kalessil kalessil changed the title [WIP] [Performance] Option lock enhancements [Performance] Use the WordPress caching layer for the option lock component. Jul 9, 2026
@kalessil
kalessil requested a review from barryhughes July 9, 2026 08:51
@kalessil
kalessil marked this pull request as ready for review July 9, 2026 08:51

@barryhughes barryhughes left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

Shows admin notices generated by Action Scheduler and by the testing instructions snippet

I left a non-blocking comment but this looks good and tests well. I supported the testing instructions with a further snippet to create a small stream of past-due actions:

add_action( 'action_scheduler_init', function () {
	rand( 0, 1 ) ? as_enqueue_async_action( 'test-pr-1342' ) : 0;
} );

These continued to be vacuumed up at the expected cadence (albeit there wasn't much contention in my local env, I didn't see cases where lock acquisition was notably delayed).

Comment thread classes/ActionScheduler_OptionLock.php Outdated
@kalessil
kalessil merged commit 96e062f into trunk Jul 10, 2026
105 checks passed
@kalessil
kalessil deleted the performance/option-lock-enhancements branch July 10, 2026 05:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants