]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/Admin.pm
admin: use a generic variable name
[public-inbox.git] / lib / PublicInbox / Admin.pm
index 1f1b133d40b4379f27fcdc8bdc28bc845b5e6c9f..8a9a81c91180bd0f315a5440df7ba01df1adfe4c 100644 (file)
@@ -1,18 +1,32 @@
-# Copyright (C) 2019 all contributors <meta@public-inbox.org>
+# Copyright (C) 2019-2020 all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 
 # common stuff for administrative command-line tools
 # Unstable internal API
 package PublicInbox::Admin;
 use strict;
-use warnings;
-use Cwd 'abs_path';
-use base qw(Exporter);
-our @EXPORT_OK = qw(resolve_repo_dir);
+use parent qw(Exporter);
+use Cwd qw(abs_path);
+use POSIX ();
+our @EXPORT_OK = qw(resolve_repo_dir setup_signals);
 use PublicInbox::Config;
 use PublicInbox::Inbox;
 use PublicInbox::Spawn qw(popen_rd);
 
+sub setup_signals {
+       my ($cb, $arg) = @_; # optional
+
+       # we call exit() here instead of _exit() so DESTROY methods
+       # get called (e.g. File::Temp::Dir and PublicInbox::Msgmap)
+       $SIG{INT} = $SIG{HUP} = $SIG{PIPE} = $SIG{TERM} = sub {
+               my ($sig) = @_;
+               # https://www.tldp.org/LDP/abs/html/exitcodes.html
+               eval { $cb->($sig, $arg) } if $cb;
+               $sig = 'SIG'.$sig;
+               exit(128 + POSIX->$sig);
+       };
+}
+
 sub resolve_repo_dir {
        my ($cd, $ver) = @_;
        my $prefix = defined $cd ? $cd : './';
@@ -71,7 +85,7 @@ sub resolve_inboxes ($;$$) {
        my ($argv, $opt, $cfg) = @_;
        $opt ||= {};
 
-       $cfg //= eval { PublicInbox::Config->new };
+       $cfg //= PublicInbox::Config->new;
        if ($opt->{all}) {
                my $cfgfile = PublicInbox::Config::default_file();
                $cfg or die "--all specified, but $cfgfile not readable\n";
@@ -84,7 +98,6 @@ sub resolve_inboxes ($;$$) {
        if ($cfg) {
                $cfg->each_inbox(sub {
                        my ($ibx) = @_;
-                       $ibx->{version} ||= 1;
                        my $path = abs_path($ibx->{inboxdir});
                        if (defined($path)) {
                                $dir2ibx{$path} = $ibx;
@@ -97,7 +110,7 @@ EOF
        }
        if ($opt->{all}) {
                my @all = values %dir2ibx;
-               @all = grep { $_->{version} >= $min_ver } @all;
+               @all = grep { $_->version >= $min_ver } @all;
                push @ibxs, @all;
        } else { # directories specified on the command-line
                my $i = 0;
@@ -123,7 +136,7 @@ EOF
 }
 
 # TODO: make Devel::Peek optional, only used for daemon
-my @base_mod = qw(Email::MIME Devel::Peek);
+my @base_mod = qw(Devel::Peek);
 my @over_mod = qw(DBD::SQLite DBI);
 my %mod_groups = (
        -index => [ @base_mod, @over_mod ],
@@ -186,15 +199,21 @@ invalid indexlevel=$indexlevel (must be `basic', `medium', or `full')
        die missing_mod_msg($err) ." required for indexlevel=$indexlevel\n";
 }
 
+sub index_terminate {
+       my (undef, $ibx) = @_; # $_[0] = signal name
+       $ibx->git->cleanup;
+}
+
 sub index_inbox {
        my ($ibx, $im, $opt) = @_;
        my $jobs = delete $opt->{jobs} if $opt;
-       if (ref($ibx) && ($ibx->{version} || 1) == 2) {
+       local %SIG = %SIG;
+       setup_signals(\&index_terminate, $ibx);
+       if (ref($ibx) && $ibx->version == 2) {
                eval { require PublicInbox::V2Writable };
                die "v2 requirements not met: $@\n" if $@;
-               my $v2w = $im // eval { $ibx->importer(0) } || eval {
-                       PublicInbox::V2Writable->new($ibx, {nproc=>$jobs});
-               };
+               $ibx->{-creat_opt}->{nproc} = $jobs;
+               my $v2w = $im // $ibx->importer($opt->{reindex} // $jobs);
                if (defined $jobs) {
                        if ($jobs == 0) {
                                $v2w->{parallel} = 0;
@@ -235,4 +254,15 @@ sub progress_prepare ($) {
        }
 }
 
+# same unit factors as git:
+sub parse_unsigned ($) {
+       my ($val) = @_;
+
+       $$val =~ /\A([0-9]+)([kmg])?\z/i or return;
+       my ($n, $unit_factor) = ($1, $2 // '');
+       my %u = ( k => 1024, m => 1024**2, g => 1024**3 );
+       $$val = $n * ($u{lc($unit_factor)} // 1);
+       1;
+}
+
 1;