From 94673ac4a9bf3aee8b6127e16657aa0e4bbe7f66 Mon Sep 17 00:00:00 2001 From: Mickey Nasriachi Date: Sun, 27 Oct 2024 08:44:16 +0100 Subject: [PATCH] handle_error : take exit_code + tidy --- bin/mirrors.pl | 10 ++++----- bin/purge.pl | 51 +++++++++++++++++++++++++----------------- bin/release.pl | 2 +- lib/MetaCPAN/ES.pm | 4 ++-- lib/MetaCPAN/Ingest.pm | 6 ++--- 5 files changed, 40 insertions(+), 33 deletions(-) diff --git a/bin/mirrors.pl b/bin/mirrors.pl index ac60ca0..b2b50e4 100644 --- a/bin/mirrors.pl +++ b/bin/mirrors.pl @@ -19,8 +19,7 @@ # setup my $cpan = cpan_dir(); -my $es = MetaCPAN::ES->new( type => "mirror" ); - +my $es = MetaCPAN::ES->new( type => "mirror" ); index_mirrors(); @@ -50,14 +49,13 @@ () }; #Dlog_trace {"Indexing $_"} $mirror; - log_debug {sprintf("Indexing %s", $mirror->{name})}; + log_debug { sprintf( "Indexing %s", $mirror->{name} ) }; - my @doc = - map { $_ => $mirror->{$_} } + my @doc = map { $_ => $mirror->{$_} } grep { defined $mirror->{$_} } keys %$mirror; - $es->index( body => { @doc } ); + $es->index( body => {@doc} ); } } diff --git a/bin/purge.pl b/bin/purge.pl index eacb53e..dee7bca 100644 --- a/bin/purge.pl +++ b/bin/purge.pl @@ -21,58 +21,67 @@ # setup my $type2index = { - release => 'cpan', - file => 'cpan', - author => 'cpan', - favorite => 'cpan', - permission => 'cpan', - contributor => 'contributor', + release => 'cpan', + file => 'cpan', + author => 'cpan', + favorite => 'cpan', + permission => 'cpan', + contributor => 'contributor', }; - purge_author() if $author; log_info {'Done'}; sub purge_author () { + # confirm $release - ? are_you_sure( sprintf("%s's %s release is about to be purged!", $author, $release), $force ) - : are_you_sure( sprintf("All of %s's releases are about to be purged!", $author), $force ); + ? are_you_sure( + sprintf( + "%s's %s release is about to be purged!", $author, $release + ), + $force + ) + : are_you_sure( + sprintf( "All of %s's releases are about to be purged!", $author ), + $force ); my $query = { bool => { must => [ { term => { author => $author } }, - ( $release - ? { term => { release => $release } } - : () + ( + $release + ? { term => { release => $release } } + : () ) ] } }; - purge_ids( type => 'favorite', query => $query); - purge_ids( type => 'file', query => $query); - purge_ids( type => 'release', query => $query); + purge_ids( type => 'favorite', query => $query ); + purge_ids( type => 'file', query => $query ); + purge_ids( type => 'release', query => $query ); if ( !$release ) { - purge_ids( type => 'author', id => $author ); + purge_ids( type => 'author', id => $author ); purge_ids( type => 'contributor', id => $author ); } } -sub purge_ids ( %args ) { +sub purge_ids (%args) { my $type = $args{type}; - my $es = MetaCPAN::ES->new( + my $es = MetaCPAN::ES->new( index => $type2index->{$type}, - type => $type + type => $type ); my $bulk = $es->bulk; my $id = $args{id}; - my $ids = $id - ? [ $id ] + my $ids + = $id + ? [$id] : $es->get_ids( query => $args{query} ); $bulk->delete_ids(@$ids); diff --git a/bin/release.pl b/bin/release.pl index f6d7d32..842a6ac 100644 --- a/bin/release.pl +++ b/bin/release.pl @@ -216,7 +216,7 @@ else { try { _import_archive( $file, $dist ) } catch { - handle_error( "$file $_[0]", 1 ); + handle_error( 1, "$file $_[0]" ); }; } } diff --git a/lib/MetaCPAN/ES.pm b/lib/MetaCPAN/ES.pm index fbd22ae..9532424 100644 --- a/lib/MetaCPAN/ES.pm +++ b/lib/MetaCPAN/ES.pm @@ -129,14 +129,14 @@ sub get_ids ( $self, %args ) { sub delete_ids ( $self, $ids ) { my $bulk = $self->bulk; - while ( my @batch = splice(@$ids, 0, 500) ) { + while ( my @batch = splice( @$ids, 0, 500 ) ) { $bulk->delete_ids(@batch); } $bulk->flush; } -sub clear_type ( $self ) { +sub clear_type ($self) { my $ids = $self->get_ids(); $self->delete_ids(@$ids); diff --git a/lib/MetaCPAN/Ingest.pm b/lib/MetaCPAN/Ingest.pm index 9e05bf0..2c605d9 100644 --- a/lib/MetaCPAN/Ingest.pm +++ b/lib/MetaCPAN/Ingest.pm @@ -163,12 +163,12 @@ sub fix_version ($version) { return ( ( $v ? 'v' : '' ) . $version ); } -sub handle_error ( $error, $die_always ) { +sub handle_error ( $exit_code, $error, $die_always ) { # Always log. log_fatal {$error}; - $! = 1; ### $self->exit_code if ( $self->exit_code != 0 ); + $! = $exit_code; Carp::croak $error if $die_always; } @@ -224,7 +224,7 @@ sub read_url ( $url ) { my $ua = ua(); my $resp = $ua->get($url); - handle_error( $resp->status_line, 1 ) unless $resp->is_success; + handle_error(1, $resp->status_line, 1 ) unless $resp->is_success; # clean up headers if .json.gz is served as gzip type # rather than json encoded with gzip