]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/LeiXSearch.pm
lei + ipc: simplify process reaping
[public-inbox.git] / lib / PublicInbox / LeiXSearch.pm
index fee1b859f9c64d2d22292b4b0a1fb4729ba0186b..fd2c8a37c0aab60b8f0198ea7b7b60ed3a256b7b 100644 (file)
@@ -15,6 +15,7 @@ use PublicInbox::Search qw(xap_terms);
 use PublicInbox::Spawn qw(popen_rd spawn which);
 use PublicInbox::MID qw(mids);
 use PublicInbox::Smsg;
+use PublicInbox::AutoReap;
 use PublicInbox::Eml;
 use PublicInbox::LEI;
 use Fcntl qw(SEEK_SET F_SETFL O_APPEND O_RDWR);
@@ -33,7 +34,7 @@ sub new {
 sub attach_external {
        my ($self, $ibxish) = @_; # ibxish = ExtSearch or Inbox
        my $desc = $ibxish->{inboxdir} // $ibxish->{topdir};
-       my $srch = $ibxish->search or
+       my $srch = $ibxish->search //
                return warn("$desc not indexed for Xapian ($@ $!)\n");
        my @shards = $srch->xdb_shards_flat or
                return warn("$desc has no Xapian shards\n");
@@ -184,11 +185,10 @@ sub query_one_mset { # for --threads and l2m w/o sort
        my $maxk = "external.$dir.maxuid";
        my $stop_at = $lss ? $lss->{-cfg}->{$maxk} : undef;
        if (defined $stop_at) {
-               die "$maxk=$stop_at has multiple values" if ref $stop_at;
-               my @e;
-               local $SIG{__WARN__} = sub { push @e, @_ };
-               $stop_at += 0;
-               return warn("$maxk=$stop_at: @e") if @e;
+               ref($stop_at) and
+                       return warn("$maxk=$stop_at has multiple values\n");
+               ($stop_at =~ /[^0-9]/) and
+                       return warn("$maxk=$stop_at not numeric\n");
        }
        my $first_ids;
        do {
@@ -282,11 +282,9 @@ sub each_remote_eml { # callback for MboxReader->mboxrd
        my $xoids = $lei->{ale}->xoids_for($eml, 1);
        my $smsg = bless {}, 'PublicInbox::Smsg';
        if ($self->{import_sto} && !$xoids) {
-               my $res = $self->{import_sto}->wq_do('add_eml', $eml);
-               if (ref($res) eq ref($smsg)) { # totally new message
-                       $smsg = $res;
-                       $smsg->{kw} = []; # short-circuit xsmsg_vmd
-               }
+               my ($res, $kw) = $self->{import_sto}->wq_do('add_eml', $eml);
+               $smsg = $res if ref($res) eq ref($smsg); # totally new message
+               $smsg->{kw} = $kw; # short-circuit xsmsg_vmd
        }
        $smsg->{blob} //= $xoids ? (keys(%$xoids))[0]
                                : $lei->git_oid($eml)->hexdigest;
@@ -347,18 +345,17 @@ sub query_remote_mboxrd {
        my @qform = (x => 'm');
        push(@qform, t => 1) if $opt->{threads};
        my $verbose = $opt->{verbose};
-       my ($reap_tail, $reap_curl);
+       my $reap_tail;
        my $cerr = File::Temp->new(TEMPLATE => 'curl.err-XXXX', TMPDIR => 1);
        fcntl($cerr, F_SETFL, O_APPEND|O_RDWR) or warn "set O_APPEND: $!";
-       my $rdr = { 2 => $cerr, pgid => 0 };
-       my $sigint_reap = $lei->can('sigint_reap');
+       my $rdr = { 2 => $cerr };
        if ($verbose) {
                # spawn a process to force line-buffering, otherwise curl
                # will write 1 character at-a-time and parallel outputs
                # mmmaaayyy llloookkk llliiikkkeee ttthhhiiisss
-               my $o = { 1 => $lei->{2}, 2 => $lei->{2}, pgid => 0 };
+               my $o = { 1 => $lei->{2}, 2 => $lei->{2} };
                my $pid = spawn(['tail', '-f', $cerr->filename], undef, $o);
-               $reap_tail = PublicInbox::OnDestroy->new($sigint_reap, $pid);
+               $reap_tail = PublicInbox::AutoReap->new($pid);
        }
        my $curl = PublicInbox::LeiCurl->new($lei, $self->{curl}) or return;
        push @$curl, '-s', '-d', '';
@@ -373,16 +370,13 @@ sub query_remote_mboxrd {
                my $cmd = $curl->for_uri($lei, $uri);
                $lei->qerr("# $cmd");
                my ($fh, $pid) = popen_rd($cmd, undef, $rdr);
-               $reap_curl = PublicInbox::OnDestroy->new($sigint_reap, $pid);
+               my $reap_curl = PublicInbox::AutoReap->new($pid);
                $fh = IO::Uncompress::Gunzip->new($fh, MultiStream => 1);
                PublicInbox::MboxReader->mboxrd($fh, \&each_remote_eml, $self,
                                                $lei, $each_smsg);
-               my $err = waitpid($pid, 0) == $pid ? undef
-                                               : "BUG: waitpid($cmd): $!";
-               @$reap_curl = (); # cancel OnDestroy
-               die $err if $err;
                my $nr = $lei->{-nr_remote_eml};
                my $wait = $lei->{sto}->wq_do('done') if $nr && $lei->{sto};
+               $reap_curl->join;
                if ($? == 0) {
                        # don't update if no results, maybe MTA is down
                        $key && $nr and
@@ -390,15 +384,15 @@ sub query_remote_mboxrd {
                        mset_progress($lei, $lei->{-current_url}, $nr, $nr);
                        next;
                }
-               $err = '';
+               my $err;
                if (-s $cerr) {
-                       seek($cerr, 0, SEEK_SET) or
+                       seek($cerr, 0, SEEK_SET) //
                                        warn "seek($cmd stderr): $!";
                        $err = do { local $/; <$cerr> } //
                                        warn "read($cmd stderr): $!";
-                       truncate($cerr, 0) or
-                                       warn "truncate($cmd stderr): $!";
+                       truncate($cerr, 0) // warn "truncate($cmd stderr): $!";
                }
+               $err //= '';
                next if (($? >> 8) == 22 && $err =~ /\b404\b/);
                $uri->query_form(q => $qstr);
                $lei->child_error($?, "E: <$uri> $err");
@@ -419,15 +413,14 @@ sub query_done { # EOF callback for main daemon
        my ($lei) = @_;
        local $PublicInbox::LEI::current_lei = $lei;
        my $l2m = delete $lei->{l2m};
-       $l2m->wq_wait_old(\&xsearch_done_wait, $lei) if $l2m;
-       if (my $lxs = delete $lei->{lxs}) {
-               $lxs->wq_wait_old(\&xsearch_done_wait, $lei);
-       }
-       if ($lei->{opt}->{'mail-sync'} && !$lei->{sto}) {
+       delete $lei->{lxs};
+       ($lei->{opt}->{'mail-sync'} && !$lei->{sto}) and
                warn "BUG: {sto} missing with --mail-sync";
-       }
        $lei->sto_done_request if $lei->{sto};
-       my $wait = $lei->{v2w} ? $lei->{v2w}->wq_do('done') : undef;
+       if (my $v2w = delete $lei->{v2w}) {
+               $v2w->wq_do('done');
+               $v2w->wq_close;
+       }
        $lei->{ovv}->ovv_end($lei);
        my $start_mua;
        if ($l2m) { # close() calls LeiToMail reap_compress
@@ -472,8 +465,8 @@ sub do_post_augment {
        $err = $@;
        if ($err) {
                if (my $lxs = delete $lei->{lxs}) {
-                       $lxs->wq_kill;
-                       $lxs->wq_close(0, undef, $lei);
+                       $lxs->wq_kill('-TERM');
+                       $lxs->wq_close;
                }
                $lei->fail("$err");
        }
@@ -521,7 +514,7 @@ sub start_query ($$) { # always runs in main (lei-daemon) process
        if ($self->{-do_lcat}) {
                $self->wq_io_do('lcat_dump', []);
        }
-       $self->wq_close(1); # lei_xsearch workers stop when done
+       $self->wq_close; # lei_xsearch workers stop when done
 }
 
 sub incr_start_query { # called whenever an l2m shard starts do_post_auth
@@ -576,12 +569,14 @@ sub do_query {
                }
                $l2m->wq_workers_start('lei2mail', undef,
                                        $lei->oldset, { lei => $lei });
+               $l2m->wq_wait_async(\&xsearch_done_wait, $lei);
                pipe($lei->{startq}, $lei->{au_done}) or die "pipe: $!";
                fcntl($lei->{startq}, $F_SETPIPE_SZ, 4096) if $F_SETPIPE_SZ;
                delete $l2m->{au_peers};
        }
        $self->wq_workers_start('lei_xsearch', undef,
                                $lei->oldset, { lei => $lei });
+       $self->wq_wait_async(\&xsearch_done_wait, $lei);
        my $op_c = delete $lei->{pkt_op_c};
        delete $lei->{pkt_op_p};
        @$end = ();