Skip to content

ES: added 'await' method #47

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

Merged
merged 1 commit into from
Oct 27, 2024
Merged
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
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;
Loading