Skip to content

Commit

Permalink
Merge pull request #2243 from 10up/fix/issue-2240
Browse files Browse the repository at this point in the history
Output of output_index_errors()
  • Loading branch information
Rahmon authored Jun 29, 2021
2 parents a01271b + 88749a4 commit 8852865
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions includes/classes/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -932,12 +932,13 @@ function ( $prefix ) use ( $args ) {
// If we have hit the trigger, initiate the bulk request.
if ( ! empty( $objects ) && ( count( $objects ) + $killed_object_count ) >= absint( count( $query['objects'] ) ) ) {
$index_objects = $objects;

$this->reset_transient( (int) ( count( $query['objects'] ) + $query_args['offset'] ), (int) $query['total_objects'], $indexable->slug );

for ( $attempts = 1; $attempts <= 3; $attempts++ ) {
$response = $indexable->bulk_index( array_keys( $index_objects ) );

$es_response_items = [];

/**
* Fires after bulk indexing in CLI
*
Expand All @@ -956,6 +957,14 @@ function ( $prefix ) use ( $args ) {
}
}

// The entire batch failed for the same reason, so apply the same error message for all IDs.
foreach ( $index_objects as $object_id => $value ) {
$es_response_items[ $object_id ] = [
'type' => esc_html__( 'Request Error', 'elasticpress' ),
'reason' => $response->get_error_message(),
];
}

WP_CLI::warning( implode( "\n", $response->get_error_messages() ) );
continue;
}
Expand All @@ -964,6 +973,8 @@ function ( $prefix ) use ( $args ) {
foreach ( $response['items'] as $item ) {
if ( empty( $item['index']['error'] ) ) {
unset( $index_objects[ $item['index']['_id'] ] );
} else {
$es_response_items[ $item['index']['_id'] ] = (array) $item['index']['error'];
}
}
} else {
Expand All @@ -976,7 +987,7 @@ function ( $prefix ) use ( $args ) {
$synced += count( $objects ) - count( $index_objects );

foreach ( $index_objects as $object_id => $value ) {
$failed_objects[ $object_id ] = (array) $item['index']['error'];
$failed_objects[ $object_id ] = ( ! empty( $es_response_items[ $object_id ] ) ) ? $es_response_items[ $object_id ] : [];
}

// reset killed count.
Expand Down Expand Up @@ -1048,15 +1059,20 @@ private function output_index_errors( $errors, Indexable $indexable, $output = t

foreach ( $errors as $object_id => $error ) {

$error_type = ( ! empty( $error['type'] ) ) ? $error['type'] : '';
$error_reason = ( ! empty( $error['reason'] ) ) ? $error['reason'] : '';

$error_array[ $object_id ] = array(
$indexable->labels['singular'],
$error['type'],
$error['reason'],
$error_type,
$error_reason,
);

$error_text .= '- ' . $object_id . ' (' . $indexable->labels['singular'] . '): ' . "\r\n";

$error_text .= '[' . $error['type'] . '] ' . $error['reason'] . "\r\n";
if ( ! empty( $error_type ) || ! empty( $error_reason ) ) {
$error_text .= '[' . $error_type . '] ' . $error_reason . "\r\n";
}
}

if ( $output ) {
Expand Down

0 comments on commit 8852865

Please sign in to comment.