Skip to content

Commit c75cf24

Browse files
committed
move code out of script role that is used in one script
Many parts of the Script role were only used by the mapping script or the purge script. Move them to the scripts themselves.
1 parent 146843f commit c75cf24

File tree

3 files changed

+205
-209
lines changed

3 files changed

+205
-209
lines changed

lib/MetaCPAN/Role/Script.pm

Lines changed: 0 additions & 208 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use Moose::Role;
44

55
use Carp ();
66
use MooseX::Types::ElasticSearch qw( ES );
7-
use File::Path ();
87
use IO::Prompt::Tiny qw( prompt );
98
use Log::Contextual qw( :log :dlog );
109
use MetaCPAN::Model ();
@@ -48,15 +47,6 @@ has exit_code => (
4847
documentation => 'Exit Code to be returned on termination',
4948
);
5049

51-
has arg_await_timeout => (
52-
init_arg => 'await',
53-
is => 'ro',
54-
isa => Int,
55-
default => 15,
56-
documentation =>
57-
'seconds before connection is considered failed with timeout',
58-
);
59-
6050
has ua => (
6151
is => 'ro',
6252
lazy => 1,
@@ -86,29 +76,6 @@ has model => (
8676
traits => ['NoGetopt'],
8777
);
8878

89-
has cluster_info => (
90-
isa => HashRef,
91-
traits => ['Hash'],
92-
is => 'rw',
93-
lazy => 1,
94-
default => sub { {} },
95-
);
96-
97-
has indices_info => (
98-
isa => HashRef,
99-
traits => ['Hash'],
100-
is => 'rw',
101-
lazy => 1,
102-
default => sub { {} },
103-
);
104-
105-
has aliases_info => (
106-
isa => HashRef,
107-
traits => ['Hash'],
108-
is => 'rw',
109-
default => sub { {} },
110-
);
111-
11279
has port => (
11380
isa => Int,
11481
is => 'ro',
@@ -126,13 +93,6 @@ has home => (
12693
default => sub { root_dir() },
12794
);
12895

129-
has quarantine => (
130-
is => 'ro',
131-
isa => Str,
132-
lazy => 1,
133-
builder => '_build_quarantine',
134-
);
135-
13696
has _minion => (
13797
is => 'ro',
13898
isa => 'Minion',
@@ -223,140 +183,12 @@ sub _build_cpan_file_map {
223183
return $cpan;
224184
}
225185

226-
sub _build_quarantine {
227-
my $path = "$ENV{HOME}/QUARANTINE";
228-
if ( !-d $path ) {
229-
File::Path::mkpath($path);
230-
}
231-
return $path;
232-
}
233-
234-
sub remote {
235-
shift->es->nodes->info->[0];
236-
}
237-
238186
sub run { }
239187
before run => sub {
240188
my $self = shift;
241189
$self->set_logger_once;
242190
};
243191

244-
sub _get_indices_info {
245-
my ( $self, $irefresh ) = @_;
246-
247-
if ( $irefresh || scalar( keys %{ $self->indices_info } ) == 0 ) {
248-
my $sinfo_rs = $self->es->cat->indices( h => [ 'index', 'health' ] );
249-
my $sindices_parsing = qr/^([^[:space:]]+) +([^[:space:]]+)/m;
250-
251-
$self->indices_info( {} );
252-
253-
while ( $sinfo_rs =~ /$sindices_parsing/g ) {
254-
$self->indices_info->{$1}
255-
= { 'index_name' => $1, 'health' => $2 };
256-
}
257-
}
258-
}
259-
260-
sub _get_aliases_info {
261-
my ( $self, $irefresh ) = @_;
262-
263-
if ( $irefresh || scalar( keys %{ $self->aliases_info } ) == 0 ) {
264-
my $sinfo_rs = $self->es->cat->aliases( h => [ 'alias', 'index' ] );
265-
my $saliases_parsing = qr/^([^[:space:]]+) +([^[:space:]]+)/m;
266-
267-
$self->aliases_info( {} );
268-
269-
while ( $sinfo_rs =~ /$saliases_parsing/g ) {
270-
$self->aliases_info->{$1} = { 'alias_name' => $1, 'index' => $2 };
271-
}
272-
}
273-
}
274-
275-
sub check_health {
276-
my ( $self, $irefresh ) = @_;
277-
my $ihealth = 0;
278-
279-
$irefresh = 0 unless ( defined $irefresh );
280-
281-
$ihealth = $self->await;
282-
283-
if ($ihealth) {
284-
$self->_get_indices_info($irefresh);
285-
286-
foreach ( keys %{ $self->indices_info } ) {
287-
$ihealth = 0
288-
if ( $self->indices_info->{$_}->{'health'} eq 'red' );
289-
}
290-
}
291-
292-
if ($ihealth) {
293-
$self->_get_aliases_info($irefresh);
294-
295-
$ihealth = 0 if ( scalar( keys %{ $self->aliases_info } ) == 0 );
296-
}
297-
298-
return $ihealth;
299-
}
300-
301-
sub await {
302-
my $self = $_[0];
303-
my $iready = 0;
304-
305-
if ( scalar( keys %{ $self->cluster_info } ) == 0 ) {
306-
my $es = $self->es;
307-
my $iseconds = 0;
308-
309-
log_info {"Awaiting Elasticsearch ..."};
310-
311-
do {
312-
eval {
313-
$iready = $es->ping;
314-
315-
if ($iready) {
316-
log_info {
317-
"Awaiting $iseconds / "
318-
. $self->arg_await_timeout
319-
. " : ready"
320-
};
321-
322-
$self->cluster_info( \%{ $es->info } );
323-
}
324-
};
325-
326-
if ($@) {
327-
if ( $iseconds < $self->arg_await_timeout ) {
328-
log_info {
329-
"Awaiting $iseconds / "
330-
. $self->arg_await_timeout
331-
. " : unavailable - sleeping ..."
332-
};
333-
334-
sleep(1);
335-
336-
$iseconds++;
337-
}
338-
else {
339-
log_error {
340-
"Awaiting $iseconds / "
341-
. $self->arg_await_timeout
342-
. " : unavailable - timeout!"
343-
};
344-
345-
#Set System Error: 112 - EHOSTDOWN - Host is down
346-
$self->exit_code(112);
347-
$self->handle_error( $@, 1 );
348-
}
349-
}
350-
} while ( !$iready && $iseconds <= $self->arg_await_timeout );
351-
}
352-
else {
353-
#ElasticSearch Service is available
354-
$iready = 1;
355-
}
356-
357-
return $iready;
358-
}
359-
360192
sub are_you_sure {
361193
my ( $self, $msg ) = @_;
362194
my $iconfirmed = 0;
@@ -437,46 +269,6 @@ This Role provides the following methods
437269
438270
=over 4
439271
440-
=item C<await()>
441-
442-
This method uses the
443-
L<C<Search::Elasticsearch::Client::2_0::Direct::ping()>|https://metacpan.org/pod/Search::Elasticsearch::Client::2_0::Direct#ping()>
444-
method to verify the service availabilty and wait for C<arg_await_timeout> seconds.
445-
When the service does not become available within C<arg_await_timeout> seconds it re-throws the
446-
Exception from the C<Search::Elasticsearch::Client> and sets B<Exit Code> to C< 112 >.
447-
The C<Search::Elasticsearch::Client> generates a C<"Search::Elasticsearch::Error::NoNodes"> Exception.
448-
When the service is available it will populate the C<cluster_info> C<HASH> structure with the basic information
449-
about the cluster.
450-
451-
B<Exceptions:> It will throw an exceptions when the I<ElasticSearch> service does not become available
452-
within C<arg_await_timeout> seconds (as described above).
453-
454-
See L<Option C<--await 15>>
455-
456-
See L<Method C<check_health()>>
457-
458-
=item C<check_health( [ refresh ] )>
459-
460-
This method uses the
461-
L<C<Search::Elasticsearch::Client::2_0::Direct::cat()>|https://metacpan.org/pod/Search::Elasticsearch::Client::2_0::Direct#cat()>
462-
method to collect basic data about the cluster structure as the general information,
463-
the health state of the indices and the created aliases.
464-
This information is stored in C<cluster_info>, C<indices_info> and C<aliases_info> as C<HASH> structures.
465-
If the parameter C<refresh> is set to C< 1 > the structures C<indices_info> and C<aliases_info> will always
466-
be updated.
467-
If the C<cluster_info> structure is empty it calls first the C<await()> method.
468-
If the service is unavailable the C<await()> method will produce an exception and the structures will be empty
469-
The method returns C< 1 > when the C<cluster_info> is populated, none of the indices in C<indices_info> has
470-
the Health State I<red> and at least one alias is created in C<aliases_info>
471-
otherwise the method returns C< 0 >
472-
473-
B<Parameters:>
474-
475-
C<refresh> - Integer evaluated as boolean when set to C< 1 > the cluster info structures
476-
will always be updated.
477-
478-
See L<Method C<await()>>
479-
480272
=item C<are_you_sure()>
481273
482274
Requests the user to confirm the operation with "I< YES >"

0 commit comments

Comments
 (0)