From 81004781fc8760f9417887cbbc63c6a3fe44cad0 Mon Sep 17 00:00:00 2001 From: Mickey Nasriachi Date: Sun, 27 Oct 2024 08:50:49 +0100 Subject: [PATCH] ES: added 'await' method --- lib/MetaCPAN/ES.pm | 46 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/lib/MetaCPAN/ES.pm b/lib/MetaCPAN/ES.pm index fbd22ae..4fc8952 100644 --- a/lib/MetaCPAN/ES.pm +++ b/lib/MetaCPAN/ES.pm @@ -7,7 +7,7 @@ use v5.36; use MetaCPAN::Logger qw< :log :dlog >; use Search::Elasticsearch; -use MetaCPAN::Ingest qw< config >; +use MetaCPAN::Ingest qw< config handle_error >; sub new ( $class, %args ) { my $node = $args{node}; @@ -142,4 +142,48 @@ sub clear_type ( $self ) { $self->delete_ids(@$ids); } +sub await ( $self ) { + my $timeout = 15; + my $iready = 0; + my $cluster_info; + my $es = $self->{es}; + + if ( scalar( keys %$cluster_info ) == 0 ) { + my $iseconds = 0; + + log_info {"Awaiting Elasticsearch ..."}; + + do { + eval { + $iready = $es->ping; + + if ($iready) { + log_info { sprintf("Awaiting %d / %d : ready", $iseconds, $timeout) }; + $cluster_info = \%{ $es->info }; + } + }; + + if ($@) { + if ( $iseconds < $timeout ) { + log_info { sprintf("Awaiting %d / %d : unavailable - sleeping ...", $iseconds, $timeout) }; + sleep(1); + $iseconds++; + } + else { + log_info { sprintf("Awaiting %d / %d : unavailable - timeout!", $iseconds, $timeout) }; + + #Set System Error: 112 - EHOSTDOWN - Host is down + handle_error( 112, $@, 1 ); + } + } + } while ( !$iready && $iseconds <= $timeout ); + } + else { + #ElasticSearch Service is available + $iready = 1; + } + + return $iready; +} + 1;