Skip to content

Commit 2dcf324

Browse files
authored
Merge pull request #47 from metacpan/mickey/es_await
ES: added 'await' method
2 parents ba5f06f + 8100478 commit 2dcf324

File tree

1 file changed

+45
-1
lines changed

1 file changed

+45
-1
lines changed

lib/MetaCPAN/ES.pm

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use v5.36;
77
use MetaCPAN::Logger qw< :log :dlog >;
88
use Search::Elasticsearch;
99

10-
use MetaCPAN::Ingest qw< config >;
10+
use MetaCPAN::Ingest qw< config handle_error >;
1111

1212
sub new ( $class, %args ) {
1313
my $node = $args{node};
@@ -142,4 +142,48 @@ sub clear_type ($self) {
142142
$self->delete_ids(@$ids);
143143
}
144144

145+
sub await ( $self ) {
146+
my $timeout = 15;
147+
my $iready = 0;
148+
my $cluster_info;
149+
my $es = $self->{es};
150+
151+
if ( scalar( keys %$cluster_info ) == 0 ) {
152+
my $iseconds = 0;
153+
154+
log_info {"Awaiting Elasticsearch ..."};
155+
156+
do {
157+
eval {
158+
$iready = $es->ping;
159+
160+
if ($iready) {
161+
log_info { sprintf("Awaiting %d / %d : ready", $iseconds, $timeout) };
162+
$cluster_info = \%{ $es->info };
163+
}
164+
};
165+
166+
if ($@) {
167+
if ( $iseconds < $timeout ) {
168+
log_info { sprintf("Awaiting %d / %d : unavailable - sleeping ...", $iseconds, $timeout) };
169+
sleep(1);
170+
$iseconds++;
171+
}
172+
else {
173+
log_info { sprintf("Awaiting %d / %d : unavailable - timeout!", $iseconds, $timeout) };
174+
175+
#Set System Error: 112 - EHOSTDOWN - Host is down
176+
handle_error( 112, $@, 1 );
177+
}
178+
}
179+
} while ( !$iready && $iseconds <= $timeout );
180+
}
181+
else {
182+
#ElasticSearch Service is available
183+
$iready = 1;
184+
}
185+
186+
return $iready;
187+
}
188+
145189
1;

0 commit comments

Comments
 (0)