Skip to content

Commit

Permalink
Merge pull request #47 from metacpan/mickey/es_await
Browse files Browse the repository at this point in the history
ES: added 'await' method
  • Loading branch information
mickeyn authored Oct 27, 2024
2 parents ba5f06f + 8100478 commit 2dcf324
Showing 1 changed file with 45 additions and 1 deletion.
46 changes: 45 additions & 1 deletion lib/MetaCPAN/ES.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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;

0 comments on commit 2dcf324

Please sign in to comment.