Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multisite context switch support to SyncManager #3689

Merged
merged 4 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions includes/classes/Indexable/Post/SyncManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -303,9 +303,7 @@ public function action_delete_post( $post_id ) {
* Make sure to remove this post from the sync queue in case an shutdown happens
* before a redirect when a redirect has already been triggered.
*/
if ( isset( $this->sync_queue[ $post_id ] ) ) {
unset( $this->sync_queue[ $post_id ] );
}
$this->remove_from_queue( $post_id );
}

/**
Expand Down Expand Up @@ -838,7 +836,7 @@ protected function should_reindex_post( $post_id, $taxonomy ) {
}

// If we have more items to update than the number set as Content Items per Index Cycle, skip it to avoid a timeout.
$single_ids_queued = array_unique( array_keys( $this->sync_queue ) );
$single_ids_queued = array_unique( array_keys( $this->get_sync_queue() ) );
$has_too_many_queued = count( $single_ids_queued ) > IndexHelper::factory()->get_index_default_per_page();

return ! $has_too_many_queued;
Expand Down
12 changes: 6 additions & 6 deletions includes/classes/Indexable/Term/SyncManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public function action_sync_on_update( $term_id ) {

do_action( 'ep_sync_term_on_transition', $term_id );

$this->sync_queue[ $term_id ] = true;
$this->add_to_queue( $term_id );

// Find all terms in the hierarchy so we resync those as well
$term = get_term( $term_id );
Expand All @@ -117,7 +117,7 @@ public function action_sync_on_update( $term_id ) {

do_action( 'ep_sync_term_on_transition', $hierarchy_term_id );

$this->sync_queue[ $hierarchy_term_id ] = true;
$this->add_to_queue( $hierarchy_term_id );
}
}

Expand Down Expand Up @@ -156,7 +156,7 @@ public function action_sync_on_object_update( $object_id, $terms ) {

do_action( 'ep_sync_term_on_transition', $term->term_id );

$this->sync_queue[ $term->term_id ] = true;
$this->add_to_queue( $term->term_id );

// Find all terms in the hierarchy so we resync those as well
$children = get_term_children( $term->term_id, $term->taxonomy );
Expand All @@ -174,7 +174,7 @@ public function action_sync_on_object_update( $object_id, $terms ) {

do_action( 'ep_sync_term_on_transition', $hierarchy_term_id );

$this->sync_queue[ $hierarchy_term_id ] = true;
$this->add_to_queue( $hierarchy_term_id );
}
}
}
Expand All @@ -191,7 +191,7 @@ public function action_queue_meta_sync( $meta_id, $term_id ) {
return;
}

$this->sync_queue[ $term_id ] = true;
$this->add_to_queue( $term_id );
}

/**
Expand Down Expand Up @@ -241,7 +241,7 @@ public function action_queue_children_sync( $term_id ) {

do_action( 'ep_sync_term_on_transition', $hierarchy_term_id );

$this->sync_queue[ $hierarchy_term_id ] = true;
$this->add_to_queue( $hierarchy_term_id );
}
}

Expand Down
121 changes: 88 additions & 33 deletions includes/classes/SyncManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,25 @@ public function __construct( $indexable_slug ) {
$this->setup();
}

/**
* Get sync queue.
*
* @since 5.0.0
* @param int $blog_id Blog ID to retrieve queue.
* @return array
*/
public function get_sync_queue( $blog_id = false ) {
if ( ! $blog_id ) {
$blog_id = get_current_blog_id();
}

if ( ! isset( $this->sync_queue[ $blog_id ] ) ) {
$this->sync_queue[ $blog_id ] = [];
}

return $this->sync_queue[ $blog_id ];
}

/**
* Add an object to the sync queue.
*
Expand All @@ -78,7 +97,13 @@ public function add_to_queue( $object_id ) {
if ( ! is_numeric( $object_id ) ) {
return false;
}
$this->sync_queue[ $object_id ] = true;

$current_blog_id = get_current_blog_id();
if ( ! isset( $this->sync_queue[ $current_blog_id ] ) ) {
$this->sync_queue[ $current_blog_id ] = [];
}

$this->sync_queue[ $current_blog_id ][ $object_id ] = true;

/**
* Fires after item is added to sync queue
Expand All @@ -88,7 +113,7 @@ public function add_to_queue( $object_id ) {
* @param {array} $sync_queue Current sync queue
* @since 3.1.2
*/
do_action( 'ep_after_add_to_queue', $object_id, $this->sync_queue );
do_action( 'ep_after_add_to_queue', $object_id, $this->get_sync_queue() );

return true;
}
Expand All @@ -106,7 +131,12 @@ public function remove_from_queue( $object_id ) {
return false;
}

unset( $this->sync_queue[ $object_id ] );
$current_blog_id = get_current_blog_id();
if ( ! isset( $this->sync_queue[ $current_blog_id ] ) ) {
$this->sync_queue[ $current_blog_id ] = [];
}

unset( $this->sync_queue[ $current_blog_id ][ $object_id ] );

/**
* Fires after item is removed from sync queue
Expand All @@ -116,11 +146,25 @@ public function remove_from_queue( $object_id ) {
* @param {array} $sync_queue Current sync queue
* @since 3.5
*/
do_action( 'ep_after_remove_from_queue', $object_id, $this->sync_queue );
do_action( 'ep_after_remove_from_queue', $object_id, $this->get_sync_queue() );

return true;
}

/**
* Reset the sync queue.
*
* @since 5.0.0
* @param int $blog_id Blog ID to reset queue
*/
public function reset_sync_queue( $blog_id = false ) {
if ( ! $blog_id ) {
$blog_id = get_current_blog_id();
}

$this->sync_queue[ $blog_id ] = [];
}

/**
* Sync queued objects if the EP_SYNC_CHUNK_LIMIT is reached.
*
Expand All @@ -129,7 +173,7 @@ public function remove_from_queue( $object_id ) {
*/
public function index_sync_on_chunk_limit() {
if ( defined( 'EP_SYNC_CHUNK_LIMIT' ) && is_numeric( EP_SYNC_CHUNK_LIMIT ) &&
is_array( $this->sync_queue ) && count( $this->sync_queue ) > EP_SYNC_CHUNK_LIMIT ) {
is_array( $this->get_sync_queue() ) && count( $this->get_sync_queue() ) > EP_SYNC_CHUNK_LIMIT ) {
$this->index_sync_queue();
}
return true;
Expand Down Expand Up @@ -159,40 +203,51 @@ public function index_sync_queue() {
return;
}

/**
* Allow other code to intercept the sync process
*
* @hook pre_ep_index_sync_queue
* @param {boolean} $bail True to skip the rest of index_sync_queue(), false to continue normally
* @param {SyncManager} $sync_manager SyncManager instance for the indexable
* @param {string} $indexable_slug Slug of the indexable being synced
* @since 3.5
*/
if ( apply_filters( 'pre_ep_index_sync_queue', false, $this, $this->indexable_slug ) ) {
return;
}
$current_blog_id = get_current_blog_id();
foreach ( $this->sync_queue as $blog_id => $sync_queue ) {
if ( $current_blog_id !== $blog_id ) {
switch_to_blog( $blog_id );
}

/**
* Backwards compat for pre-3.0
*/
foreach ( $this->sync_queue as $object_id => $value ) {
/**
* Fires when object in queue are synced
* Allow other code to intercept the sync process
*
* @hook ep_sync_on_meta_update_queue
* @param {int} $object_id ID of object
* @hook pre_ep_index_sync_queue
* @param {boolean} $bail True to skip the rest of index_sync_queue(), false to continue normally
* @param {SyncManager} $sync_manager SyncManager instance for the indexable
* @param {string} $indexable_slug Slug of the indexable being synced
* @since 3.5
*/
do_action( 'ep_sync_on_meta_update', $object_id );
}
if ( apply_filters( 'pre_ep_index_sync_queue', false, $this, $this->indexable_slug ) ) {
return;
}

// Bulk sync them all.
Indexables::factory()->get( $this->indexable_slug )->bulk_index_dynamically( array_keys( $this->sync_queue ) );
/**
* Backwards compat for pre-3.0
*/
foreach ( $sync_queue as $object_id => $value ) {
/**
* Fires when object in queue are synced
*
* @hook ep_sync_on_meta_update_queue
* @param {int} $object_id ID of object
*/
do_action( 'ep_sync_on_meta_update', $object_id );
}

// Bulk sync them all.
Indexables::factory()->get( $this->indexable_slug )->bulk_index_dynamically( array_keys( $this->get_sync_queue( $blog_id ) ) );

/**
* Make sure to reset sync queue in case an shutdown happens before a redirect
* when a redirect has already been triggered.
*/
$this->sync_queue = [];
/**
* Make sure to reset sync queue in case an shutdown happens before a redirect
* when a redirect has already been triggered.
*/
$this->reset_sync_queue( $blog_id );

if ( $current_blog_id !== $blog_id ) {
restore_current_blog();
}
}
}

/**
Expand Down
6 changes: 3 additions & 3 deletions tests/php/TestAdminNotices.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function() {
ElasticPress\Elasticsearch::factory()->delete_all_indices();
ElasticPress\Indexables::factory()->get( 'post' )->put_mapping();

ElasticPress\Indexables::factory()->get( 'post' )->sync_manager->sync_queue = [];
ElasticPress\Indexables::factory()->get( 'post' )->sync_manager->reset_sync_queue();

$this->setup_test_post_type();

Expand Down Expand Up @@ -490,7 +490,7 @@ function() use ( $es_version ) {

ElasticPress\Elasticsearch::factory()->delete_all_indices();
ElasticPress\Indexables::factory()->get( 'post' )->put_mapping();
ElasticPress\Indexables::factory()->get( 'post' )->sync_manager->sync_queue = [];
ElasticPress\Indexables::factory()->get( 'post' )->sync_manager->reset_sync_queue();

ElasticPress\Screen::factory()->set_current_screen( null );

Expand Down Expand Up @@ -534,7 +534,7 @@ function() use ( $es_version ) {

ElasticPress\Elasticsearch::factory()->delete_all_indices();
ElasticPress\Indexables::factory()->get( 'post' )->put_mapping();
ElasticPress\Indexables::factory()->get( 'post' )->sync_manager->sync_queue = [];
ElasticPress\Indexables::factory()->get( 'post' )->sync_manager->reset_sync_queue();

$mapping = function() {
return 'idonotmatch';
Expand Down
2 changes: 1 addition & 1 deletion tests/php/TestFeatureActivation.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function set_up() {
ElasticPress\Elasticsearch::factory()->delete_all_indices();
ElasticPress\Indexables::factory()->get( 'post' )->put_mapping();

ElasticPress\Indexables::factory()->get( 'post' )->sync_manager->sync_queue = [];
ElasticPress\Indexables::factory()->get( 'post' )->sync_manager->reset_sync_queue();

$this->setup_test_post_type();
}
Expand Down
2 changes: 1 addition & 1 deletion tests/php/TestInstaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function set_up() {
ElasticPress\Elasticsearch::factory()->delete_all_indices();
ElasticPress\Indexables::factory()->get( 'post' )->put_mapping();

ElasticPress\Indexables::factory()->get( 'post' )->sync_manager->sync_queue = [];
ElasticPress\Indexables::factory()->get( 'post' )->sync_manager->reset_sync_queue();

$this->setup_test_post_type();

Expand Down
2 changes: 1 addition & 1 deletion tests/php/TestScreen.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function set_up() {
ElasticPress\Elasticsearch::factory()->delete_all_indices();
ElasticPress\Indexables::factory()->get( 'post' )->put_mapping();

ElasticPress\Indexables::factory()->get( 'post' )->sync_manager->sync_queue = [];
ElasticPress\Indexables::factory()->get( 'post' )->sync_manager->reset_sync_queue();

$this->setup_test_post_type();

Expand Down
2 changes: 1 addition & 1 deletion tests/php/TestStats.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function set_up() {
ElasticPress\Elasticsearch::factory()->delete_all_indices();
ElasticPress\Indexables::factory()->get( 'post' )->put_mapping();

ElasticPress\Indexables::factory()->get( 'post' )->sync_manager->sync_queue = [];
ElasticPress\Indexables::factory()->get( 'post' )->sync_manager->reset_sync_queue();

$this->setup_test_post_type();

Expand Down
2 changes: 1 addition & 1 deletion tests/php/TestUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function set_up() {
ElasticPress\Elasticsearch::factory()->delete_all_indices();
ElasticPress\Indexables::factory()->get( 'post' )->put_mapping();

ElasticPress\Indexables::factory()->get( 'post' )->sync_manager->sync_queue = [];
ElasticPress\Indexables::factory()->get( 'post' )->sync_manager->reset_sync_queue();

$this->setup_test_post_type();

Expand Down
2 changes: 1 addition & 1 deletion tests/php/features/TestAutosuggest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function set_up() {
ElasticPress\Elasticsearch::factory()->delete_all_indices();
ElasticPress\Indexables::factory()->get( 'post' )->put_mapping();

ElasticPress\Indexables::factory()->get( 'post' )->sync_manager->sync_queue = [];
ElasticPress\Indexables::factory()->get( 'post' )->sync_manager->reset_sync_queue();

$this->setup_test_post_type();

Expand Down
2 changes: 1 addition & 1 deletion tests/php/features/TestComments.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function set_up() {
ElasticPress\Elasticsearch::factory()->delete_all_indices();
ElasticPress\Indexables::factory()->get( 'post' )->put_mapping();

ElasticPress\Indexables::factory()->get( 'post' )->sync_manager->sync_queue = [];
ElasticPress\Indexables::factory()->get( 'post' )->sync_manager->reset_sync_queue();

$this->setup_test_post_type();

Expand Down
2 changes: 1 addition & 1 deletion tests/php/features/TestDocuments.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function set_up() {
ElasticPress\Elasticsearch::factory()->delete_all_indices();
ElasticPress\Indexables::factory()->get( 'post' )->put_mapping();

ElasticPress\Indexables::factory()->get( 'post' )->sync_manager->sync_queue = [];
ElasticPress\Indexables::factory()->get( 'post' )->sync_manager->reset_sync_queue();

$this->setup_test_post_type();

Expand Down
4 changes: 2 additions & 2 deletions tests/php/features/TestProtectedContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function set_up() {
ElasticPress\Elasticsearch::factory()->delete_all_indices();
ElasticPress\Indexables::factory()->get( 'post' )->put_mapping();

ElasticPress\Indexables::factory()->get( 'post' )->sync_manager->sync_queue = [];
ElasticPress\Indexables::factory()->get( 'post' )->sync_manager->reset_sync_queue();

$this->setup_test_post_type();
}
Expand Down Expand Up @@ -414,7 +414,7 @@ public function testAdminCommentQuery() {
ElasticPress\Features::factory()->setup_features();

ElasticPress\Indexables::factory()->get( 'comment' )->put_mapping();
ElasticPress\Indexables::factory()->get( 'comment' )->sync_manager->sync_queue = [];
ElasticPress\Indexables::factory()->get( 'comment' )->sync_manager->reset_sync_queue();

// Need to call this since it's hooked to init.
ElasticPress\Features::factory()->get_registered_feature( 'comments' )->search_setup();
Expand Down
2 changes: 1 addition & 1 deletion tests/php/features/TestRelatedPosts.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function set_up() {
ElasticPress\Elasticsearch::factory()->delete_all_indices();
ElasticPress\Indexables::factory()->get( 'post' )->put_mapping();

ElasticPress\Indexables::factory()->get( 'post' )->sync_manager->sync_queue = [];
ElasticPress\Indexables::factory()->get( 'post' )->sync_manager->reset_sync_queue();

$this->setup_test_post_type();
}
Expand Down
2 changes: 1 addition & 1 deletion tests/php/features/TestSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function set_up() {
ElasticPress\Elasticsearch::factory()->delete_all_indices();
ElasticPress\Indexables::factory()->get( 'post' )->put_mapping();

ElasticPress\Indexables::factory()->get( 'post' )->sync_manager->sync_queue = [];
ElasticPress\Indexables::factory()->get( 'post' )->sync_manager->reset_sync_queue();

$this->setup_test_post_type();
}
Expand Down
2 changes: 1 addition & 1 deletion tests/php/features/TestSearchOrdering.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function set_up() {
ElasticPress\Elasticsearch::factory()->delete_all_indices();
ElasticPress\Indexables::factory()->get( 'post' )->put_mapping();

ElasticPress\Indexables::factory()->get( 'post' )->sync_manager->sync_queue = [];
ElasticPress\Indexables::factory()->get( 'post' )->sync_manager->reset_sync_queue();

$this->setup_test_post_type();

Expand Down
Loading