Skip to content

Commit 9130a65

Browse files
committed
Added mirrors script
1 parent abdcc19 commit 9130a65

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed

bin/mirrors.pl

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
use strict;
2+
use warnings;
3+
use v5.36;
4+
5+
use Getopt::Long;
6+
use MetaCPAN::Logger qw< :log :dlog >;
7+
use Cpanel::JSON::XS ();
8+
9+
use MetaCPAN::ES;
10+
use MetaCPAN::Ingest qw< cpan_dir >;
11+
12+
# args
13+
my ( $distribution, $files_only, $undo );
14+
GetOptions(
15+
"distribution=s" => \$distribution,
16+
"files_only" => \$files_only,
17+
"undo" => \$undo,
18+
);
19+
20+
# setup
21+
my $cpan = cpan_dir();
22+
my $es = MetaCPAN::ES->new( type => "mirror" );
23+
24+
25+
index_mirrors();
26+
27+
$es->index_refresh;
28+
29+
# TODO:
30+
# cdn_purge_now( { keys => ['MIRRORS'], } );
31+
32+
log_info {"done"};
33+
34+
###
35+
36+
sub index_mirrors () {
37+
log_info { 'Getting mirrors.json file from ' . $cpan };
38+
39+
my $json = $cpan->child( 'indices', 'mirrors.json' )->slurp;
40+
41+
# Clear out everything in the index
42+
# so don't end up with old mirrors
43+
$es->clear_type;
44+
45+
my $mirrors = Cpanel::JSON::XS::decode_json($json);
46+
foreach my $mirror (@$mirrors) {
47+
$mirror->{location} = {
48+
lon => delete $mirror->{longitude},
49+
lat => delete $mirror->{latitude}
50+
};
51+
52+
#Dlog_trace {"Indexing $_"} $mirror;
53+
log_debug {sprintf("Indexing %s", $mirror->{name})};
54+
55+
my @doc =
56+
map { $_ => $mirror->{$_} }
57+
grep { defined $mirror->{$_} }
58+
keys %$mirror;
59+
60+
$es->index( body => { @doc } );
61+
}
62+
}
63+
64+
1;
65+
66+
__END__
67+
68+
=pod
69+
70+
=head1 SYNOPSIS
71+
72+
$ bin/mirrors.pl
73+
74+
=head1 SOURCE
75+
76+
L<http://www.cpan.org/indices/mirrors.json>
77+
78+
=cut

lib/MetaCPAN/ES.pm

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use strict;
44
use warnings;
55
use v5.36;
66

7+
use MetaCPAN::Logger qw< :log :dlog >;
78
use Search::Elasticsearch;
89

910
use MetaCPAN::Ingest qw< config >;
@@ -108,4 +109,25 @@ sub count ( $self, %args ) {
108109
);
109110
}
110111

112+
sub clear_type ( $self ) {
113+
my $bulk = $self->bulk;
114+
my $scroll = $self->scroll(
115+
query => { match_all => {} },
116+
sort => '_doc',
117+
);
118+
119+
my @ids;
120+
while ( my $search = $scroll->next ) {
121+
push @ids => $search->{_id};
122+
log_debug { "deleting id=" . $search->{_id} };
123+
if ( @ids == 500 ) {
124+
$bulk->delete_ids(@ids);
125+
@ids = ();
126+
}
127+
}
128+
$bulk->delete_ids(@ids);
129+
130+
$bulk->flush;
131+
}
132+
111133
1;

0 commit comments

Comments
 (0)