about summary refs log tree commit homepage
path: root/lib/PublicInbox/V2Writable.pm
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2020-11-13 11:11:44 +0000
committerEric Wong <e@80x24.org>2020-11-15 06:12:43 +0000
commit7d42724e82c9e0ecfa07154c7a56e7f21e53e62f (patch)
tree6755566e109457ffcef08f3acb3b9301bb0dd858 /lib/PublicInbox/V2Writable.pm
parent834181c640236c91f367de04d5cc9834ec617f4e (diff)
downloadpublic-inbox-7d42724e82c9e0ecfa07154c7a56e7f21e53e62f.tar.gz
Just like the daemon processes, -extindex now supports graceful
shutdown via the same signals.  This lets users avoid having to
repeat indexing messages when a power outage strikes during a
long (multi-hour/day) indexing run.

Per-inbox (v1/v2) -index graceful shutdowns are not supported,
yet, but is planned for later.
Diffstat (limited to 'lib/PublicInbox/V2Writable.pm')
-rw-r--r--lib/PublicInbox/V2Writable.pm17
1 files changed, 16 insertions, 1 deletions
diff --git a/lib/PublicInbox/V2Writable.pm b/lib/PublicInbox/V2Writable.pm
index 11cde627..5bac04a4 100644
--- a/lib/PublicInbox/V2Writable.pm
+++ b/lib/PublicInbox/V2Writable.pm
@@ -1090,6 +1090,7 @@ sub sync_prepare ($$) {
                 $unit->{stack} = $stk; # may be undef
                 unshift @{$sync->{todo}}, $unit;
                 $regen_max += $nr;
+                last if $sync->{quit};
         }
 
         # XXX this should not happen unless somebody bypasses checks in
@@ -1102,9 +1103,11 @@ sub sync_prepare ($$) {
                         $oid = unpack('H*', $oid);
                         my $req = { %$sync, oid => $oid };
                         $self->git->cat_async($oid, $unindex_oid, $req);
+                        last if $sync->{quit};
                 }
                 $self->git->cat_async_wait;
         }
+        return 0 if $sync->{quit};
         if (!$regen_max) {
                 $sync->{-regen_fmt} = "%u/?\n";
                 return 0;
@@ -1236,6 +1239,7 @@ sub index_xap_step ($$$;$) {
 
 sub index_todo ($$$) {
         my ($self, $sync, $unit) = @_;
+        return if $sync->{quit};
         unindex_todo($self, $sync, $unit);
         my $stk = delete($unit->{stack}) or return;
         my $all = $self->git;
@@ -1268,6 +1272,12 @@ sub index_todo ($$$) {
                 } elsif ($f eq 'd') {
                         $all->cat_async($oid, $unindex_oid, $req);
                 }
+                if ($sync->{quit}) {
+                        warn "waiting to quit...\n";
+                        $all->async_wait_all;
+                        $self->update_last_commit($sync);
+                        return;
+                }
                 if (${$sync->{need_checkpoint}}) {
                         reindex_checkpoint($self, $sync);
                 }
@@ -1334,6 +1344,11 @@ sub index_sync {
                 ibx => $self->{ibx},
                 epoch_max => $epoch_max,
         };
+        my $quit = sub { $sync->{quit} = 1 };
+        local $SIG{QUIT} = $quit;
+        local $SIG{INT} = $quit;
+        local $SIG{TERM} = $quit;
+
         if (sync_prepare($self, $sync)) {
                 # tmp_clone seems to fail if inside a transaction, so
                 # we rollback here (because we opened {mm} for reading)
@@ -1352,7 +1367,7 @@ sub index_sync {
         }
         # work forwards through history
         index_todo($self, $sync, $_) for @{delete($sync->{todo}) // []};
-        $self->{oidx}->rethread_done($opt);
+        $self->{oidx}->rethread_done($opt) unless $sync->{quit};
         $self->done;
 
         if (my $nr = $sync->{nr}) {