Skip to content

Commit 4ddc3d0

Browse files
committed
Update to using new async_batch approach
1 parent 0902256 commit 4ddc3d0

File tree

4 files changed

+30
-18
lines changed

4 files changed

+30
-18
lines changed

includes/class-migration.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public static function maybe_migrate() {
137137
self::migrate_from_0_16();
138138
}
139139
if ( \version_compare( $version_from_db, '1.0.0', '<' ) ) {
140-
\wp_schedule_single_event( \time(), 'activitypub_async_batch', array( array( self::class, 'migrate_from_0_17' ) ) );
140+
\wp_schedule_single_event( \time(), 'activitypub_migrate_from_0_17' );
141141
}
142142
if ( \version_compare( $version_from_db, '1.3.0', '<' ) ) {
143143
self::migrate_from_1_2_0();
@@ -158,7 +158,7 @@ public static function maybe_migrate() {
158158
self::migrate_to_4_1_0();
159159
}
160160
if ( \version_compare( $version_from_db, '4.5.0', '<' ) ) {
161-
\wp_schedule_single_event( \time() + MINUTE_IN_SECONDS, 'activitypub_async_batch', array( array( self::class, 'update_comment_counts' ) ) );
161+
\wp_schedule_single_event( \time() + MINUTE_IN_SECONDS, 'activitypub_update_comment_counts' );
162162
}
163163
if ( \version_compare( $version_from_db, '4.7.1', '<' ) ) {
164164
self::migrate_to_4_7_1();
@@ -171,8 +171,8 @@ public static function maybe_migrate() {
171171
}
172172
if ( \version_compare( $version_from_db, '5.0.0', '<' ) ) {
173173
Scheduler::register_schedules();
174-
\wp_schedule_single_event( \time(), 'activitypub_async_batch', array( array( self::class, 'create_post_outbox_items' ) ) );
175-
\wp_schedule_single_event( \time() + 15, 'activitypub_async_batch', array( array( self::class, 'create_comment_outbox_items' ) ) );
174+
\wp_schedule_single_event( \time(), 'activitypub_create_post_outbox_items' );
175+
\wp_schedule_single_event( \time() + 15, 'activitypub_create_comment_outbox_items' );
176176
add_action( 'init', 'flush_rewrite_rules', 20 );
177177
}
178178
if ( \version_compare( $version_from_db, '5.2.0', '<' ) ) {

includes/class-scheduler.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,12 @@ public static function init() {
3737
self::register_schedulers();
3838

3939
self::$batch_callbacks = array(
40-
'activitypub_send_activity' => array( Dispatcher::class, 'send_to_followers' ),
41-
'activitypub_retry_activity' => array( Dispatcher::class, 'retry_send_to_followers' ),
40+
'activitypub_send_activity' => array( Dispatcher::class, 'send_to_followers' ),
41+
'activitypub_retry_activity' => array( Dispatcher::class, 'retry_send_to_followers' ),
42+
'activitypub_migrate_from_0_17' => array( Migration::class, 'migrate_from_0_17' ),
43+
'activitypub_update_comment_counts' => array( Migration::class, 'update_comment_counts' ),
44+
'activitypub_create_post_outbox_items' => array( Migration::class, 'create_post_outbox_items' ),
45+
'activitypub_create_comment_outbox_items' => array( Migration::class, 'create_comment_outbox_items' ),
4246
);
4347

4448
// Follower Cleanups.
@@ -49,6 +53,10 @@ public static function init() {
4953
\add_action( 'activitypub_async_batch', array( self::class, 'async_batch' ), 10, 99 );
5054
\add_action( 'activitypub_send_activity', array( self::class, 'async_batch' ), 10, 3 );
5155
\add_action( 'activitypub_retry_activity', array( self::class, 'async_batch' ), 10, 3 );
56+
\add_action( 'activitypub_migrate_from_0_17', array( self::class, 'async_batch' ) );
57+
\add_action( 'activitypub_update_comment_counts', array( self::class, 'async_batch' ), 10, 2 );
58+
\add_action( 'activitypub_create_post_outbox_items', array( self::class, 'async_batch' ), 10, 2 );
59+
\add_action( 'activitypub_create_comment_outbox_items', array( self::class, 'async_batch' ), 10, 2 );
5260
\add_action( 'activitypub_reprocess_outbox', array( self::class, 'reprocess_outbox' ) );
5361
\add_action( 'activitypub_outbox_purge', array( self::class, 'purge_outbox' ) );
5462

@@ -326,7 +334,7 @@ public static function async_batch() {
326334
return;
327335
}
328336

329-
$key = \md5( \serialize( $args[0] ?? $args ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize
337+
$key = \md5( \serialize( $callback ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize
330338

331339
// Bail if the existing lock is still valid.
332340
if ( self::is_locked( $key ) ) {

tests/includes/class-test-migration.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,7 @@ public function test_migration_scheduling() {
177177

178178
Migration::maybe_migrate();
179179

180-
$schedule = \wp_next_scheduled( 'activitypub_migrate', array( '0.0.1' ) );
181-
$this->assertNotFalse( $schedule );
180+
$this->assertNotFalse( \wp_next_scheduled( 'activitypub_migrate_from_0_17' ) );
182181

183182
// Clean up.
184183
delete_option( 'activitypub_db_version' );

tests/includes/class-test-scheduler.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -361,15 +361,15 @@ public function test_update_comment_counts_with_existing_valid_lock() {
361361
// Set a lock.
362362
Scheduler::lock( $key );
363363

364-
Scheduler::async_batch( $callback, 10, 0 );
364+
\do_action( 'activitypub_update_comment_counts', 10, 0 );
365365

366366
// Verify a scheduled event was created.
367-
$next_scheduled = wp_next_scheduled( 'activitypub_async_batch', array( $callback, 10, 0 ) );
367+
$next_scheduled = wp_next_scheduled( 'activitypub_update_comment_counts', array( 10, 0 ) );
368368
$this->assertNotFalse( $next_scheduled );
369369

370370
// Clean up.
371371
delete_option( 'activitypub_migration_lock' );
372-
wp_clear_scheduled_hook( 'activitypub_async_batch', array( $callback, 10, 0 ) );
372+
wp_clear_scheduled_hook( 'activitypub_update_comment_counts', array( 10, 0 ) );
373373
}
374374

375375
/**
@@ -385,8 +385,10 @@ public function test_async_upgrade() {
385385

386386
// Test that lock prevents simultaneous upgrades.
387387
Scheduler::lock( $key );
388-
Scheduler::async_batch( $callback );
389-
$scheduled = \wp_next_scheduled( 'activitypub_async_batch', array( $callback ) );
388+
389+
\do_action( 'activitypub_create_post_outbox_items', 10, 0 );
390+
391+
$scheduled = \wp_next_scheduled( 'activitypub_create_post_outbox_items', array( 10, 0 ) );
390392
$this->assertNotFalse( $scheduled );
391393
Scheduler::unlock( $key );
392394

@@ -395,15 +397,18 @@ public function test_async_upgrade() {
395397
\add_action( 'transition_post_status', array( \Activitypub\Scheduler\Post::class, 'schedule_post_activity' ), 33, 3 );
396398

397399
// Test scheduling next batch when callback returns more work.
398-
Scheduler::async_batch( $callback, 1, 0 ); // Small batch size to force multiple batches.
399-
$scheduled = \wp_next_scheduled( 'activitypub_async_batch', array( $callback, 1, 1 ) );
400+
\do_action( 'activitypub_create_post_outbox_items', 1, 0 ); // Small batch size to force multiple batches.
401+
$scheduled = \wp_next_scheduled( 'activitypub_create_post_outbox_items', array( 1, 1 ) );
400402
$this->assertNotFalse( $scheduled );
401403

402404
// Test no scheduling when callback returns null (no more work).
403-
Scheduler::async_batch( $callback, 100, 1000 ); // Large offset to ensure no posts found.
405+
\do_action( 'activitypub_create_post_outbox_items', 100, 1000 ); // Large offset to ensure no posts found.
404406
$this->assertFalse(
405-
\wp_next_scheduled( 'activitypub_async_batch', array( $callback, 100, 1100 ) )
407+
\wp_next_scheduled( 'activitypub_create_post_outbox_items', array( 100, 1100 ) )
406408
);
409+
}
410+
411+
/**
407412
* Test async_batch method.
408413
*
409414
* @covers ::async_batch

0 commit comments

Comments
 (0)