Skip to content

Commit fa5cf62

Browse files
authored
Merge pull request #1351 from metacpan/haarg/mirror-script-no-model
rewrite mirror script to avoid ESXM
2 parents 5ec1d0b + 7d6697c commit fa5cf62

File tree

1 file changed

+49
-13
lines changed

1 file changed

+49
-13
lines changed

lib/MetaCPAN/Script/Mirrors.pm

+49-13
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ use warnings;
66
use Cpanel::JSON::XS ();
77
use Log::Contextual qw( :log :dlog );
88
use Moose;
9+
use MetaCPAN::ESConfig qw( es_doc_path );
10+
use MetaCPAN::Util qw( true false );
911

1012
with 'MetaCPAN::Role::Script', 'MooseX::Getopt';
1113

@@ -19,26 +21,60 @@ sub index_mirrors {
1921
my $self = shift;
2022
log_info { 'Getting mirrors.json file from ' . $self->cpan };
2123

22-
my $json = $self->cpan->child( 'indices', 'mirrors.json' )->slurp;
23-
my $type = $self->model->doc('mirror');
24-
25-
# Clear out everything in the index
26-
# so don't end up with old mirrors
27-
$type->delete;
24+
my $es = $self->es;
2825

26+
my $json = $self->cpan->child( 'indices', 'mirrors.json' )->slurp;
2927
my $mirrors = Cpanel::JSON::XS::decode_json($json);
30-
foreach my $mirror (@$mirrors) {
31-
$mirror->{location}
32-
= { lon => $mirror->{longitude}, lat => $mirror->{latitude} };
28+
my %mirrors = map +( $_->{name} => $_ ), @$mirrors;
29+
30+
my $need_purge;
31+
32+
my $scroll = $es->scroll_helper( es_doc_path('mirror'), size => 500, );
33+
my $bulk = $es->bulk_helper(
34+
es_doc_path('mirror'),
35+
on_success => sub {
36+
my ( $method, $res ) = @_;
37+
if ( $method eq 'update' ) {
38+
39+
# result is not supported until 5, but this will work when we
40+
# update
41+
if ( exists $res->{result} ) {
42+
return
43+
if $res->{result} eq 'noop';
44+
}
45+
}
46+
$need_purge++;
47+
},
48+
);
49+
while ( my $doc = $scroll->next ) {
50+
if ( !$mirrors{ $doc->{_id} } ) {
51+
Dlog_trace {"Deleting $doc->{_id}"};
52+
$bulk->delete_ids( $doc->{_id} );
53+
}
54+
}
55+
56+
for my $mirror (@$mirrors) {
57+
my $data = {%$mirror};
58+
delete $data->{$_} for grep !defined $data->{$_}, keys %$data;
59+
$data->{location} = {
60+
lon => delete $mirror->{longitude},
61+
lat => delete $mirror->{latitude},
62+
};
63+
3364
Dlog_trace {"Indexing $_"} $mirror;
34-
$type->put( {
35-
map { $_ => $mirror->{$_} }
36-
grep { defined $mirror->{$_} } keys %$mirror
65+
$bulk->update( {
66+
id => $mirror->{name},
67+
doc => $data,
68+
doc_as_upsert => true,
3769
} );
3870
}
71+
72+
$bulk->flush;
73+
3974
log_info {'done'};
4075

41-
$self->cdn_purge_now( { keys => ['MIRRORS'], } );
76+
$self->cdn_purge_now( { keys => ['MIRRORS'] } )
77+
if $need_purge;
4278

4379
}
4480

0 commit comments

Comments
 (0)