From 2ea1921d613803aabbf2edd6ca976a784e477c02 Mon Sep 17 00:00:00 2001 From: Bodo Hugo Barwich Date: Thu, 17 Feb 2022 23:36:05 +0000 Subject: [PATCH] ElasticSearch Methods _get_indices_info() and _get_aliases_info() --- lib/MetaCPAN/Role/Script.pm | 64 +++++++++++++++++++++---------------- 1 file changed, 36 insertions(+), 28 deletions(-) diff --git a/lib/MetaCPAN/Role/Script.pm b/lib/MetaCPAN/Role/Script.pm index ba27bec00..102f80174 100644 --- a/lib/MetaCPAN/Role/Script.pm +++ b/lib/MetaCPAN/Role/Script.pm @@ -246,6 +246,37 @@ before run => sub { #Dlog_debug {"Connected to $_"} $self->remote; }; +sub _get_indices_info { + my ( $self, $irefresh ) = @_; + + if ( $irefresh || scalar( keys %{ $self->indices_info } ) == 0 ) { + my $sinfo_rs = $self->es->cat->indices( h => [ 'index', 'health' ] ); + my $sindices_parsing = qr/^([^[:space:]]+) +([^[:space:]]+)/m; + + $self->indices_info( {} ); + + while ( $sinfo_rs =~ /$sindices_parsing/g ) { + $self->indices_info->{$1} + = { 'index_name' => $1, 'health' => $2 }; + } + } +} + +sub _get_aliases_info { + my ( $self, $irefresh ) = @_; + + if ( $irefresh || scalar( keys %{ $self->aliases_info } ) == 0 ) { + my $sinfo_rs = $self->es->cat->aliases( h => [ 'alias', 'index' ] ); + my $saliases_parsing = qr/^([^[:space:]]+) +([^[:space:]]+)/m; + + $self->aliases_info( {} ); + + while ( $sinfo_rs =~ /$saliases_parsing/g ) { + $self->aliases_info->{$1} = { 'alias_name' => $1, 'index' => $2 }; + } + } +} + sub check_health { my ( $self, $irefresh ) = @_; my $ihealth = 0; @@ -255,39 +286,16 @@ sub check_health { $ihealth = $self->await; if ($ihealth) { - if ( $irefresh || scalar( keys %{ $self->indices_info } ) == 0 ) { - my $sinfo_rs - = $self->es->cat->indices( h => [ 'index', 'health' ] ); + $self->_get_indices_info($irefresh); - $self->indices_info( {} ); - - while ( $sinfo_rs =~ /^([^[:space:]]+) +([^[:space:]]+)/gm ) { - $self->indices_info->{$1} - = { 'index_name' => $1, 'health' => $2 }; - - $ihealth = 0 if ( $2 eq 'red' ); - } - } - else { - foreach ( keys %{ $self->indices_info } ) { - $ihealth = 0 - if ( $self->indices_info->{$_}->{'health'} eq 'red' ); - } + foreach ( keys %{ $self->indices_info } ) { + $ihealth = 0 + if ( $self->indices_info->{$_}->{'health'} eq 'red' ); } } if ($ihealth) { - if ( $irefresh || scalar( keys %{ $self->aliases_info } ) == 0 ) { - my $sinfo_rs - = $self->es->cat->aliases( h => [ 'alias', 'index' ] ); - - $self->aliases_info( {} ); - - while ( $sinfo_rs =~ /^([^[:space:]]+) +([^[:space:]]+)/gm ) { - $self->aliases_info->{$1} - = { 'alias_name' => $1, 'index' => $2 }; - } - } + $self->_get_aliases_info($irefresh); $ihealth = 0 if ( scalar( keys %{ $self->aliases_info } ) == 0 ); }