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
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
13 changes: 12 additions & 1 deletion includes/classes/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,9 @@ public function delete_transient_on_int( $signal_no ) {
* [--show-nobulk-errors]
* : Display the error message returned from Elasticsearch when a post fails to index while not using the /_bulk endpoint
*
* [--stop-on-error]
* : Stop indexing if an error is encountered and display the error.
*
* [--offset=<offset_number>]
* : Skip the first n posts (don't forget to remove the `--setup` flag when resuming or the index will be emptied before starting again).
*
Expand Down Expand Up @@ -800,7 +803,14 @@ public function sync( $args, $assoc_args ) {
'static_bulk' => $static_bulk,
];

if ( isset( $assoc_args['show-errors'] ) || ( isset( $assoc_args['show-bulk-errors'] ) && ! $no_bulk ) || ( isset( $assoc_args['show-nobulk-errors'] ) && $no_bulk ) ) {
$index_args['stop_on_error'] = WP_CLI\Utils\get_flag_value( $assoc_args, 'stop-on-error', false );

$show_errors = $index_args['stop_on_error'] ||
WP_CLI\Utils\get_flag_value( $assoc_args, 'show-errors', false ) ||
( WP_CLI\Utils\get_flag_value( $assoc_args, 'show-bulk-errors', false ) && ! $no_bulk ) ||
( WP_CLI\Utils\get_flag_value( $assoc_args, 'show-nobulk-errors', false ) && $no_bulk );

if ( $show_errors ) {
$index_args['show_errors'] = true;
}

Expand Down Expand Up @@ -1310,6 +1320,7 @@ public function index_output( $message, $args, $index_meta, $context ) {
if ( empty( $args['show_errors'] ) ) {
return;
}

WP_CLI::warning( $message['message'] );
break;

Expand Down
3 changes: 2 additions & 1 deletion includes/classes/IndexHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -716,8 +716,9 @@ function( $item ) {
);

$this->index_meta['current_sync_item']['failed'] += count( $failed_objects );
$error_type = ! empty( $this->args['stop_on_error'] ) ? 'error' : 'warning';

$this->queue_message( $errors_output, 'warning' );
$this->queue_message( $errors_output, $error_type );
} else {
$this->index_meta['current_sync_item']['synced'] += count( $queued_items );
}
Expand Down
49 changes: 49 additions & 0 deletions tests/php/TestCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -929,6 +929,55 @@ public function testRequest() {
$this->assertStringContainsString( 'Response:', $output );
}

/**
* Test sync command with stop-on-error flag.
* Expect an error message that stops the sync instead of a warning.
*
* @since 4.7.0
*/
public function test_sync_stop_on_error() {
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;
}
);
Comment on lines +939 to +966
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?

Indexables::factory()->get( 'post' )->delete_index();

$this->ep_factory->post->create();

$this->expectExceptionMessage( '10 (Post): [my dummy error type] my dummy error reason' );

$this->command->sync(
[],
[
'stop-on-error' => true,
]
);
}

/**
* Test request command throws an error if request fails.
*/
Expand Down