about summary refs log tree commit homepage
path: root/lib/PublicInbox/SearchIdx.pm
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2020-11-27 21:33:55 +0000
committerEric Wong <e@80x24.org>2020-11-28 04:53:25 +0000
commitc2f82b2e27e1b3c11a4c0b00b90829a4ee99c602 (patch)
tree3b33517c4c28e7d3eebf29cd8d1a5d16b82e4cb4 /lib/PublicInbox/SearchIdx.pm
parent811b8d3cbaa790f59b7b107140b86248da16499b (diff)
downloadpublic-inbox-c2f82b2e27e1b3c11a4c0b00b90829a4ee99c602.tar.gz
v1 and v2 inbox indexing now supports graceful shutdown checks
just like ExtSearchIdx.  Additionally, we'll consistently
perform quit checks at the top of loops for consistency.

Interaction with the --xapian-only and --sequential-shard
options are a bit lacking, and will warn the user to use
"--reindex --xapian-only" to fix.
Diffstat (limited to 'lib/PublicInbox/SearchIdx.pm')
-rw-r--r--lib/PublicInbox/SearchIdx.pm25
1 files changed, 21 insertions, 4 deletions
diff --git a/lib/PublicInbox/SearchIdx.pm b/lib/PublicInbox/SearchIdx.pm
index 18390602..d06c159b 100644
--- a/lib/PublicInbox/SearchIdx.pm
+++ b/lib/PublicInbox/SearchIdx.pm
@@ -632,10 +632,11 @@ sub index_sync {
         my ($self, $opt) = @_;
         delete $self->{lock_path} if $opt->{-skip_lock};
         $self->with_umask(\&_index_sync, $self, $opt);
-        if ($opt->{reindex}) {
+        if ($opt->{reindex} && !$opt->{quit}) {
                 my %again = %$opt;
                 delete @again{qw(rethread reindex)};
                 index_sync($self, \%again);
+                $opt->{quit} = $again{quit}; # propagate to caller
         }
 }
 
@@ -688,7 +689,7 @@ sub v1_checkpoint ($$;$) {
         if (my $pr = $sync->{-opt}->{-progress}) {
                 $pr->("indexed $nr/$sync->{ntodo}\n") if $nr;
         }
-        if (!$stk) { # more to come
+        if (!$stk && !$sync->{quit}) { # more to come
                 begin_txn_lazy($self);
                 $self->{mm}->{dbh}->begin_work;
         }
@@ -709,6 +710,7 @@ sub process_stack {
         if (my @leftovers = keys %{delete($sync->{D}) // {}}) {
                 warn('W: unindexing '.scalar(@leftovers)." leftovers\n");
                 for my $oid (@leftovers) {
+                        last if $sync->{quit};
                         $oid = unpack('H*', $oid);
                         $git->cat_async($oid, \&unindex_both, $sync);
                 }
@@ -718,6 +720,7 @@ sub process_stack {
         }
         while (my ($f, $at, $ct, $oid, $cur_cmt) = $stk->pop_rec) {
                 my $arg = { %$sync, cur_cmt => $cur_cmt };
+                last if $sync->{quit};
                 if ($f eq 'm') {
                         $arg->{autime} = $at;
                         $arg->{cotime} = $ct;
@@ -731,7 +734,7 @@ sub process_stack {
                         $git->cat_async($oid, \&unindex_both, $arg);
                 }
         }
-        v1_checkpoint($self, $sync, $stk);
+        v1_checkpoint($self, $sync, $sync->{quit} ? undef : $stk);
 }
 
 sub log2stack ($$$) {
@@ -841,6 +844,16 @@ sub reindex_from ($$) {
         ref($reindex) eq 'HASH' ? $reindex->{from} : '';
 }
 
+sub quit_cb ($) {
+        my ($sync) = @_;
+        sub {
+                # we set {-opt}->{quit} too, so ->index_sync callers
+                # can abort multi-inbox loops this way
+                $sync->{quit} = $sync->{-opt}->{quit} = 1;
+                warn "gracefully quitting\n";
+        }
+}
+
 # indexes all unindexed messages (v1 only)
 sub _index_sync {
         my ($self, $opt) = @_;
@@ -850,6 +863,10 @@ sub _index_sync {
         $ibx->git->batch_prepare;
         my $pr = $opt->{-progress};
         my $sync = { reindex => $opt->{reindex}, -opt => $opt, ibx => $ibx };
+        my $quit = quit_cb($sync);
+        local $SIG{QUIT} = $quit;
+        local $SIG{INT} = $quit;
+        local $SIG{TERM} = $quit;
         my $xdb = $self->begin_txn_lazy;
         $self->{oidx}->rethread_prepare($opt);
         my $mm = _msgmap_init($self);
@@ -870,7 +887,7 @@ sub _index_sync {
         my $stk = prepare_stack($sync, $range);
         $sync->{ntodo} = $stk ? $stk->num_records : 0;
         $pr->("$sync->{ntodo}\n") if $pr; # continue previous line
-        process_stack($self, $sync, $stk);
+        process_stack($self, $sync, $stk) if !$sync->{quit};
 }
 
 sub DESTROY {