about summary refs log tree commit homepage
path: root/lib/PublicInbox/Admin.pm
diff options
context:
space:
mode:
authorEric Wong <e@yhbt.net>2020-08-10 02:11:56 +0000
committerEric Wong <e@yhbt.net>2020-08-10 05:56:03 +0000
commite3944dfcfb3c0d8964d7cd458a9f14bbf503d00f (patch)
tree3746ccc10fac1084487f2e766a0c6910038fb893 /lib/PublicInbox/Admin.pm
parent425acc673f5394b0e8472558921db949eac034a5 (diff)
downloadpublic-inbox-e3944dfcfb3c0d8964d7cd458a9f14bbf503d00f.tar.gz
-index now invokes ->DESTROY like xcpdb does, which is necessary
to cleanup $INBOX_DIR/msgmap-XXXXXXX files.  We'll also exit
with the expected values for various signals by adding 128
as described in <https://www.tldp.org/LDP/abs/html/exitcodes.html>

-xcpdb now terminates worker processes and xapian-compact(1)
invocations when prematurely killed, too.
Diffstat (limited to 'lib/PublicInbox/Admin.pm')
-rw-r--r--lib/PublicInbox/Admin.pm29
1 files changed, 25 insertions, 4 deletions
diff --git a/lib/PublicInbox/Admin.pm b/lib/PublicInbox/Admin.pm
index e42b01e0..af2b3da9 100644
--- a/lib/PublicInbox/Admin.pm
+++ b/lib/PublicInbox/Admin.pm
@@ -5,14 +5,28 @@
 # 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 : './';
@@ -185,9 +199,16 @@ 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;
+        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 $@;