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

Sync command stop-on-error flag #3500

Merged
merged 16 commits into from
Aug 3, 2023
Merged

Conversation

oscarssanchez
Copy link
Contributor

@oscarssanchez oscarssanchez commented Jun 19, 2023

Description of the Change

This PR introduces a new flag stop-on-error to stop sync the moment we find an error. We accomplish this by returning an error logged by output() that later kills the sync.

Closes #2000

How to test the Change

Changelog Entry

Added - Flag --stop-on-error

Credits

Props @oscarssanchez

Checklist:

  • I agree to follow this project's Code of Conduct.
  • I have updated the documentation accordingly.
  • I have added tests to cover my change.
  • All new and existing tests pass.

@oscarssanchez oscarssanchez changed the title WIP: sync command stop-on-error flag Sync command stop-on-error flag Jun 22, 2023
@felipeelia
Copy link
Member

@oscarssanchez if I add a snippet like this in a mu-plugin and run the sync, it will only stop if --show-errors is also present:

add_filter(
	'ep_prepare_meta_data',
	function( $post_meta, $post ) {
		for ( $i = 0; $i < 100; $i++ ) {
			$post_meta[ "test_meta_{$i}_title_{$post->ID}" ] = 'Lorem';
			$post_meta[ "test_meta_{$i}_body_{$post->ID}" ]  = 'Ipsum';
		}
		return $post_meta;
	},
	10,
	2
);

Is that intended?

@felipeelia felipeelia added this to the 4.7.0 milestone Jul 4, 2023
Comment on lines 932 to 935
/**
* Test sync command with stop-on-error flag.
* Expect an error message that stops the sync instead of a warning.
*/
Copy link
Member

Choose a reason for hiding this comment

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

@oscarssanchez can we add a @since tag here please?

* Test sync command with stop-on-error flag.
* Expect an error message that stops the sync instead of a warning.
*/
public function testSyncStopOnError() {
Copy link
Member

Choose a reason for hiding this comment

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

Not a blocker but for newer tests we are using snake-case for test names. Do you mind renaming it to test_sync_stop_on_error()? We'll have a follow up issue to rename all test methods.

Comment on lines +937 to +964
add_filter(
'http_response',
function( $request ) {
$fake_request = json_decode( wp_remote_retrieve_body( $request ) );

if ( ! empty( $fake_request->items ) ) {
$fake_request->errors = true;

$fake_item = new \stdClass();
$fake_item->index = new \stdClass();
$fake_item->index->error = new \stdClass();
$fake_item->index->status = 400;
$fake_item->index->_id = 10;
$fake_item->index->type = '_doc';
$fake_item->index->_index = 'dummy-index';
$fake_item->index->error->reason = 'my dummy error reason';
$fake_item->index->error->type = 'my dummy error type';

$fake_request->items[0] = $fake_item;

$request['body'] = wp_json_encode( $fake_request );

return $request;
}

return $request;
}
);
Copy link
Member

Choose a reason for hiding this comment

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

Instead of faking the return via a filter, it would be faster to mock the remote_request response (similar to what I did here: https://github.com/10up/ElasticPress/blob/develop/tests/php/TestElasticsearch.php#L156) Do you mind making that change?

Comment on lines 966 to 968
Indexables::factory()->get( 'post' )->delete_index();

$this->ep_factory->post->create_many( 10 );
Copy link
Member

Choose a reason for hiding this comment

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

With the above, I think this can go away. If mocking the method return is not doable, can we avoid deleting the index at least?

Comment on lines 975 to 978
'setup' => true,
'show-errors' => true,
'stop-on-error' => true,
'yes' => true,
Copy link
Member

Choose a reason for hiding this comment

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

Just to make sure it already works with the minimal case, can we remove the setup, show-errors, and the yes flags?

@felipeelia felipeelia merged commit 87bd0dd into develop Aug 3, 2023
@felipeelia felipeelia deleted the feature/stop-on-errors-cli branch August 3, 2023 20:57
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.

Create a new --stop-on-errors parameter for the WP-CLI index command
2 participants