@@ -6,6 +6,8 @@ use warnings;
6
6
use Cpanel::JSON::XS ();
7
7
use Log::Contextual qw( :log :dlog ) ;
8
8
use Moose;
9
+ use MetaCPAN::ESConfig qw( es_doc_path ) ;
10
+ use MetaCPAN::Util qw( true false ) ;
9
11
10
12
with ' MetaCPAN::Role::Script' , ' MooseX::Getopt' ;
11
13
@@ -19,26 +21,60 @@ sub index_mirrors {
19
21
my $self = shift ;
20
22
log_info { ' Getting mirrors.json file from ' . $self -> cpan };
21
23
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;
28
25
26
+ my $json = $self -> cpan-> child( ' indices' , ' mirrors.json' )-> slurp;
29
27
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
+
33
64
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,
37
69
} );
38
70
}
71
+
72
+ $bulk -> flush;
73
+
39
74
log_info {' done' };
40
75
41
- $self -> cdn_purge_now( { keys => [' MIRRORS' ], } );
76
+ $self -> cdn_purge_now( { keys => [' MIRRORS' ] } )
77
+ if $need_purge ;
42
78
43
79
}
44
80
0 commit comments