Skip to content

Commit

Permalink
Merge pull request #46 from metacpan/mickey/handle_error
Browse files Browse the repository at this point in the history
handle_error : take exit_code + tidy
  • Loading branch information
mickeyn authored Oct 27, 2024
2 parents 7910662 + 94673ac commit ba5f06f
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 33 deletions.
10 changes: 4 additions & 6 deletions bin/mirrors.pl
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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} );
}
}

Expand Down
51 changes: 30 additions & 21 deletions bin/purge.pl
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion bin/release.pl
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@
else {
try { _import_archive( $file, $dist ) }
catch {
handle_error( "$file $_[0]", 1 );
handle_error( 1, "$file $_[0]" );
};
}
}
Expand Down
4 changes: 2 additions & 2 deletions lib/MetaCPAN/ES.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions lib/MetaCPAN/Ingest.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ba5f06f

Please sign in to comment.