Skip to content

Commit 91f07d6

Browse files
committed
remove index option from scripts and reimplement it for backup
The index option is meant to control the index that has all of our ES data. But in the future, each document type will need to be in a separate index. So the idea of having a global configurable index doesn't make sense. The backup script needs to be able to operate on all indexes, and works generically with any type. Reimplement an index option for it, allowing multiple indexes to be specified.
1 parent 466f634 commit 91f07d6

File tree

6 files changed

+49
-66
lines changed

6 files changed

+49
-66
lines changed

bin/cron/author.sh

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,4 @@
33
# export ES_SCRIPT_INDEX=author_01
44
# /home/metacpan/bin/metacpan-api-carton-exec bin/metacpan author --index author_01
55

6-
export ES_SCRIPT_INDEX=cpan_v1_01
7-
/home/metacpan/bin/metacpan-api-carton-exec bin/metacpan author --index cpan_v1_01
8-
9-
unset ES_SCRIPT_INDEX
6+
/home/metacpan/bin/metacpan-api-carton-exec bin/metacpan author

bin/cron/backups.sh

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,6 @@
11
#!/bin/sh
22

3-
#export ES_SCRIPT_INDEX=favorite_01
4-
#/home/metacpan/bin/metacpan-api-carton-exec bin/metacpan backup --index favorite_01 --type favorite
5-
6-
#export ES_SCRIPT_INDEX=author_01
7-
#/home/metacpan/bin/metacpan-api-carton-exec bin/metacpan backup --index author_01 --type author
8-
9-
export ES_SCRIPT_INDEX=cpan_v1_01
103
/home/metacpan/bin/metacpan-api-carton-exec bin/metacpan backup --index cpan_v1_01 --type favorite
114
/home/metacpan/bin/metacpan-api-carton-exec bin/metacpan backup --index cpan_v1_01 --type author
125

13-
export ES_SCRIPT_INDEX=user
146
/home/metacpan/bin/metacpan-api-carton-exec bin/metacpan backup --index user
15-
16-
unset ES_SCRIPT_INDEX

lib/MetaCPAN/Model.pm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ analyzer edge => (
4444

4545
index cpan => (
4646
namespace => 'MetaCPAN::Document',
47-
alias_for => ( $ENV{'ES_SCRIPT_INDEX'} || 'cpan_v1_01' ),
47+
alias_for => 'cpan_v1_01',
4848
shards => 3
4949
);
5050

lib/MetaCPAN/Role/Script.pm

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -86,16 +86,6 @@ has model => (
8686
traits => ['NoGetopt'],
8787
);
8888

89-
has index => (
90-
reader => '_index',
91-
is => 'ro',
92-
isa => Str,
93-
lazy => 1,
94-
default => 'cpan',
95-
documentation =>
96-
'Index to use, defaults to "cpan" (when used: also export ES_SCRIPT_INDEX)',
97-
);
98-
9989
has cluster_info => (
10090
isa => HashRef,
10191
traits => ['Hash'],
@@ -158,20 +148,6 @@ has queue => (
158148
documentation => 'add indexing jobs to the minion queue',
159149
);
160150

161-
sub BUILDARGS {
162-
my ( $self, @args ) = @_;
163-
my %args = @args == 1 ? %{ $args[0] } : @args;
164-
165-
if ( exists $args{index} ) {
166-
die
167-
"when setting --index, please export ES_SCRIPT_INDEX to the same value\n"
168-
unless $ENV{ES_SCRIPT_INDEX}
169-
and $args{index} eq $ENV{ES_SCRIPT_INDEX};
170-
}
171-
172-
return \%args;
173-
}
174-
175151
sub handle_error {
176152
my ( $self, $error, $die_always ) = @_;
177153

@@ -194,7 +170,7 @@ sub print_error {
194170

195171
sub index {
196172
my $self = shift;
197-
return $self->model->index( $self->_index );
173+
return $self->model->index( 'cpan' );
198174
}
199175

200176
sub _build_model {

lib/MetaCPAN/Script/Backup.pm

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use Cpanel::JSON::XS qw( decode_json encode_json );
88
use DateTime ();
99
use IO::Zlib ();
1010
use Log::Contextual qw( :log :dlog );
11-
use MetaCPAN::Types::TypeTiny qw( Bool Int Path Str );
11+
use MetaCPAN::Types::TypeTiny qw( Bool Int Path Str CommaSepOption );
1212
use Moose;
1313
use Try::Tiny qw( catch try );
1414

@@ -22,6 +22,15 @@ has batch_size => (
2222
'Number of documents to restore in one batch, defaults to 100',
2323
);
2424

25+
has index => (
26+
reader => '_index',
27+
is => 'ro',
28+
isa => CommaSepOption,
29+
coerce => 1,
30+
default => 'cpan',
31+
documentation => 'ES indexes to backup, defaults to "cpan"',
32+
);
33+
2534
has type => (
2635
is => 'ro',
2736
isa => Str,
@@ -61,34 +70,38 @@ sub run {
6170
return $self->run_restore if $self->restore;
6271

6372
my $es = $self->es;
64-
$self->index->refresh;
65-
66-
my $filename = join( '-',
67-
DateTime->now->strftime('%F'),
68-
grep {defined} $self->index->name,
69-
$self->type );
70-
71-
my $file = $self->home->child( qw(var backup), "$filename.json.gz" );
72-
$file->parent->mkpath unless ( -e $file->parent );
73-
my $fh = IO::Zlib->new( "$file", 'wb4' );
74-
75-
my $scroll = $es->scroll_helper(
76-
index => $self->index->name,
77-
$self->type ? ( type => $self->type ) : (),
78-
size => $self->size,
79-
fields => [qw(_parent _source)],
80-
scroll => '1m',
81-
body => {
82-
sort => '_doc',
83-
},
84-
);
8573

86-
log_info { 'Backing up ', $scroll->total, ' documents' };
74+
for my $index (@{ $self->_index }) {
75+
76+
$self->es->indices->refresh( index => $index );
8777

88-
while ( my $result = $scroll->next ) {
89-
print $fh encode_json($result), $/;
78+
my $filename = join( '-',
79+
DateTime->now->strftime('%F'),
80+
grep { defined } $index,
81+
$self->type );
82+
83+
my $file = $self->home->child( qw(var backup), "$filename.json.gz" );
84+
$file->parent->mkpath unless ( -e $file->parent );
85+
my $fh = IO::Zlib->new( "$file", 'wb4' );
86+
87+
my $scroll = $es->scroll_helper(
88+
index => $index,
89+
$self->type ? ( type => $self->type ) : (),
90+
size => $self->size,
91+
fields => [qw(_parent _source)],
92+
scroll => '1m',
93+
body => {
94+
sort => '_doc',
95+
},
96+
);
97+
98+
log_info { 'Backing up ', $scroll->total, ' documents' };
99+
100+
while ( my $result = $scroll->next ) {
101+
print $fh encode_json($result), $/;
102+
}
103+
close $fh;
90104
}
91-
close $fh;
92105
log_info {'done'};
93106
}
94107

lib/MetaCPAN/Types/TypeTiny.pm

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ use Type::Library -base, -declare => ( qw(
1818
1919
Logger
2020
HashRefCPANMeta
21+
22+
CommaSepOption
2123
) );
2224
use Type::Utils qw( as coerce declare extends from via );
2325

@@ -126,4 +128,9 @@ if ( eval { require MooseX::Getopt; 1 } ) {
126128
}
127129
}
128130

131+
declare CommaSepOption, as ArrayRef[StrMatch[qr{^[^, ]+$}]];
132+
coerce CommaSepOption, from ArrayRef[Str], via {
133+
return [map split(/\s*,\s*/), @$_];
134+
};
135+
129136
1;

0 commit comments

Comments
 (0)