Skip to content

Commit

Permalink
Account for an arbitrary number of args to be passed
Browse files Browse the repository at this point in the history
  • Loading branch information
obenland committed Jan 24, 2025
1 parent 0cf8b91 commit 1e877c3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 32 deletions.
19 changes: 13 additions & 6 deletions includes/class-migration.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Migration {
*/
public static function init() {
\add_action( 'activitypub_migrate', array( self::class, 'async_migration' ) );
\add_action( 'activitypub_upgrade', array( self::class, 'async_upgrade' ) );
\add_action( 'activitypub_upgrade', array( self::class, 'async_upgrade' ), 10, 99 );
\add_action( 'activitypub_update_comment_counts', array( self::class, 'update_comment_counts' ), 10, 2 );

self::maybe_migrate();
Expand Down Expand Up @@ -221,22 +221,29 @@ public static function async_migration( $version_from_db ) {
* @param mixed ...$args The arguments to pass to the callback.
*/
public static function async_upgrade( $callback, ...$args ) {
$args = \func_get_args();

// Bail if the existing lock is still valid.
if ( self::is_locked() ) {
\wp_schedule_single_event( time() + MINUTE_IN_SECONDS, 'activitypub_upgrade', \array_merge( array( $callback ), $args ) );
\wp_schedule_single_event( time() + MINUTE_IN_SECONDS, 'activitypub_upgrade', $args );
return;
}

self::lock();

$next = \call_user_func_array( array( self::class, $callback ), $args );
$callback = array_shift( $args ); // Remove $callback from arguments.
$next = \call_user_func_array( array( self::class, $callback ), $args );

self::unlock();

if ( ! empty( $next ) ) {
// Schedule the next run, adding the result to the arguments.
\wp_schedule_single_event( \time() + 30, 'activitypub_upgrade', \array_merge( array( $callback ), $next ) );
\wp_schedule_single_event(
\time() + 30,
'activitypub_upgrade',
\array_merge( array( $callback ), \array_values( $next ) )
);
}

self::unlock();
}

/**
Expand Down
29 changes: 3 additions & 26 deletions tests/includes/class-test-migration.php
Original file line number Diff line number Diff line change
Expand Up @@ -534,27 +534,13 @@ public function test_async_upgrade() {

// Test scheduling next batch when callback returns more work.
Migration::async_upgrade( 'create_post_outbox_items', 1, 0 ); // Small batch size to force multiple batches.
$scheduled = \wp_next_scheduled(
'activitypub_upgrade',
array(
'create_post_outbox_items',
'batch_size' => 1,
'offset' => 1,
)
);
$scheduled = \wp_next_scheduled( 'activitypub_upgrade', array( 'create_post_outbox_items', 1, 1 ) );
$this->assertNotFalse( $scheduled );

// Test no scheduling when callback returns null (no more work).
Migration::async_upgrade( 'create_post_outbox_items', 100, 1000 ); // Large offset to ensure no posts found.
$this->assertFalse(
\wp_next_scheduled(
'activitypub_upgrade',
array(
'create_post_outbox_items',
'batch_size' => 100,
'offset' => 1100,
)
)
\wp_next_scheduled( 'activitypub_upgrade', array( 'create_post_outbox_items', 100, 1100 ) )
);
}

Expand All @@ -566,16 +552,7 @@ public function test_async_upgrade() {
public function test_async_upgrade_multiple_args() {
// Test that multiple arguments are passed correctly.
Migration::async_upgrade( 'update_comment_counts', 50, 100 );
$scheduled = \wp_next_scheduled(
'activitypub_upgrade',
array(
'update_comment_counts',
array(
'batch_size' => 50,
'offset' => 150,
),
)
);
$scheduled = \wp_next_scheduled( 'activitypub_upgrade', array( 'update_comment_counts', 50, 150 ) );
$this->assertFalse( $scheduled, 'Should not schedule next batch when no comments found' );
}

Expand Down

0 comments on commit 1e877c3

Please sign in to comment.