]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/LeiUp.pm
lei q: improve --limit behavior and progress
[public-inbox.git] / lib / PublicInbox / LeiUp.pm
index 49b558fdb2932f2be458e57c2489c8ec2ce2ca32..89cf0112e9b58964d0ddaf9e363cdb3e90042d2a 100644 (file)
@@ -5,15 +5,19 @@
 package PublicInbox::LeiUp;
 use strict;
 use v5.10.1;
+# n.b. we use LeiInput to setup IMAP auth
+use parent qw(PublicInbox::IPC PublicInbox::LeiInput);
 use PublicInbox::LeiSavedSearch;
-use parent qw(PublicInbox::IPC);
+use PublicInbox::DS;
+use PublicInbox::PktOp;
+use PublicInbox::LeiFinmsg;
+my $REMOTE_RE = qr!\A(?:imap|http)s?://!i; # http(s) will be for JMAP
 
 sub up1 ($$) {
        my ($lei, $out) = @_;
        my $lss = PublicInbox::LeiSavedSearch->up($lei, $out) or return;
        my $f = $lss->{'-f'};
        my $mset_opt = $lei->{mset_opt} = { relevance => -2 };
-       $mset_opt->{limit} = $lei->{opt}->{limit} // 10000;
        my $q = $mset_opt->{q_raw} = $lss->{-cfg}->{'lei.q'} //
                                return $lei->fail("lei.q unset in $f");
        my $lse = $lei->{lse} // die 'BUG: {lse} missing';
@@ -22,25 +26,19 @@ sub up1 ($$) {
        } else {
                $lse->query_approxidate($lse->git, $mset_opt->{qstr} = $q);
        }
-       my $o = $lei->{opt}->{output} = $lss->{-cfg}->{'lei.q.output'} //
-               return $lei->fail("lei.q.output unset in $f");
-       ref($o) and return $lei->fail("multiple values of lei.q.output in $f");
-       if (defined(my $dd = $lss->{-cfg}->{'lei.q.dedupe'})) {
-               $lss->translate_dedupe($lei, $dd) or return;
-               $lei->{opt}->{dedupe} = $dd;
-       }
-       for my $k (qw(only include exclude)) {
+       # n.b. only a few CLI args are accepted for "up", so //= usually sets
+       for my $k ($lss->ARRAY_FIELDS) {
                my $v = $lss->{-cfg}->get_all("lei.q.$k") // next;
-               $lei->{opt}->{$k} = $v;
+               $lei->{opt}->{$k} //= $v;
        }
-       for my $k (qw(external local remote
-                       import-remote import-before threads)) {
-               my $c = "lei.q.$k";
-               my $v = $lss->{-cfg}->{$c} // next;
-               ref($v) and return $lei->fail("multiple values of $c in $f");
-               $lei->{opt}->{$k} = $v;
+       for my $k ($lss->BOOL_FIELDS, $lss->SINGLE_FIELDS) {
+               my $v = $lss->{-cfg}->get_1('lei.q', $k) // next;
+               $lei->{opt}->{$k} //= $v;
        }
-       $lei->{lss} = $lss; # for LeiOverview->new
+       my $o = $lei->{opt}->{output} // '';
+       return $lei->fail("lei.q.output unset in $f") if $o eq '';
+       $lss->translate_dedupe($lei) or return;
+       $lei->{lss} = $lss; # for LeiOverview->new and query_remote_mboxrd
        my $lxs = $lei->lxs_prepare or return;
        $lei->ale->refresh_externals($lxs, $lei);
        $lei->_start_query;
@@ -48,70 +46,101 @@ sub up1 ($$) {
 
 sub up1_redispatch {
        my ($lei, $out, $op_p) = @_;
-       my $l = bless { %$lei }, ref($lei);
-       $l->{opt} = { %{$l->{opt}} };
-       delete $l->{sock};
-       $l->{''} = $op_p; # daemon only
-       eval {
+       my $l;
+       if (defined($lei->{opt}->{mua})) { # single output
+               $l = $lei;
+       } else { # multiple outputs
+               $l = bless { %$lei }, ref($lei);
+               $l->{opt} = { %{$l->{opt}} }; # deep copy
+               delete $l->{opt}->{all};
+               delete $l->{sock}; # do not close
+               # make close($l->{1}) happy in lei->dclose
+               open my $fh, '>&', $l->{1} or
+                       return $l->child_error(0, "dup: $!");
+               $l->{1} = $fh;
                $l->qerr("# updating $out");
-               up1($l, $out);
-               $l->qerr("# $out done");
-       };
-       $l->err($@) if $@;
+       }
+       $l->{''} = $op_p; # daemon only ($l => $lei => script/lei)
+       eval { $l->dispatch('up', $out) };
+       $lei->child_error(0, $@) if $@ || $l->{failed}; # lei->fail()
+}
+
+sub redispatch_all ($$) {
+       my ($self, $lei) = @_;
+       # re-dispatch into our event loop w/o creating an extra fork-level
+       $lei->{fmsg} = PublicInbox::LeiFinmsg->new($lei);
+       my ($op_c, $op_p) = PublicInbox::PktOp->pair;
+       for my $o (@{$self->{local} // []}, @{$self->{remote} // []}) {
+               PublicInbox::DS::requeue(sub {
+                       up1_redispatch($lei, $o, $op_p);
+               });
+       }
+       $lei->event_step_init;
+       $lei->pkt_ops($op_c->{ops} = { '' => [$lei->can('dclose'), $lei] });
 }
 
 sub lei_up {
        my ($lei, @outs) = @_;
-       $lei->{lse} = $lei->_lei_store(1)->search;
        my $opt = $lei->{opt};
-       $opt->{save} = -1;
-       my @local;
-       if (defined $opt->{all}) {
+       my $self = bless { -mail_sync => 1 }, __PACKAGE__;
+       if (defined(my $all = $opt->{all})) {
                return $lei->fail("--all and @outs incompatible") if @outs;
-               length($opt->{mua}//'') and return
+               defined($opt->{mua}) and return
                        $lei->fail('--all and --mua= are incompatible');
-
-               # supporting IMAP outputs is more involved due to
-               # git-credential prompts.  TODO: add this in 1.8
-               $opt->{all} eq 'local' or return
-                       $lei->fail('only --all=local works at the moment');
-               my @all = PublicInbox::LeiSavedSearch::list($lei);
-               @local = grep(!m!\Aimaps?://!i, @all);
+               @outs = PublicInbox::LeiSavedSearch::list($lei);
+               if ($all eq 'local') {
+                       $self->{local} = [ grep(!/$REMOTE_RE/, @outs) ];
+               } elsif ($all eq 'remote') {
+                       $self->{remote} = [ grep(/$REMOTE_RE/, @outs) ];
+               } elsif ($all eq '') {
+                       $self->{remote} = [ grep(/$REMOTE_RE/, @outs) ];
+                       $self->{local} = [ grep(!/$REMOTE_RE/, @outs) ];
+               } else {
+                       $lei->fail("only --all=$all not understood");
+               }
+       } elsif ($lei->{lse}) {
+               scalar(@outs) == 1 or die "BUG: lse set w/ >1 out[@outs]";
+               return up1($lei, $outs[0]);
        } else {
-               @local = @outs;
+               $self->{remote} = [ grep(/$REMOTE_RE/, @outs) ];
+               $self->{local} = [ grep(!/$REMOTE_RE/, @outs) ];
        }
-       if (scalar(@outs) > 1) {
-               length($opt->{mua}//'') and return $lei->fail(<<EOM);
+       $lei->{lse} = $lei->_lei_store(1)->write_prepare($lei)->search;
+       ((@{$self->{local} // []} + @{$self->{remote} // []}) > 1 &&
+               defined($opt->{mua})) and return $lei->fail(<<EOM);
 multiple outputs and --mua= are incompatible
 EOM
-               # TODO:
-               return $lei->fail(<<EOM) if grep(m!\Aimaps?://!i, @outs);
-multiple destinations only supported for local outputs (FIXME)
-EOM
+       if ($self->{remote}) { # setup lei->{auth}
+               $self->prepare_inputs($lei, $self->{remote}) or return;
        }
-       if (scalar(@local) > 1) {
-               $lei->_lei_store->write_prepare($lei); # share early
-               # daemon mode, re-dispatch into our event loop w/o
-               # creating an extra fork-level
-               require PublicInbox::DS;
-               require PublicInbox::PktOp;
-               my ($op_c, $op_p) = PublicInbox::PktOp->pair;
-               for my $o (@local) {
-                       PublicInbox::DS::requeue(sub {
-                               up1_redispatch($lei, $o, $op_p);
-                       });
-               }
-               $lei->event_step_init;
-               $op_c->{ops} = { '' => [$lei->can('dclose'), $lei] };
+       if ($lei->{auth}) { # start auth worker
+               require PublicInbox::NetWriter;
+               bless $lei->{net}, 'PublicInbox::NetWriter';
+               $lei->{auth}->op_merge(my $ops = {}, $self, $lei);
+               (my $op_c, $ops) = $lei->workers_start($self, 1, $ops);
+               $lei->{wq1} = $self;
+               $lei->wait_wq_events($op_c, $ops);
+               # net_merge_all_done will fire when auth is done
        } else {
-               up1($lei, $local[0]);
+               redispatch_all($self, $lei); # see below
        }
 }
 
+# called in top-level lei-daemon when LeiAuth is done
+sub net_merge_all_done {
+       my ($self, $lei) = @_;
+       $lei->{net} = delete($self->{-net_new}) if $self->{-net_new};
+       $self->wq_close(1);
+       redispatch_all($self, $lei);
+}
+
 sub _complete_up {
        my ($lei, @argv) = @_;
        my $match_cb = $lei->complete_url_prepare(\@argv);
        map { $match_cb->($_) } PublicInbox::LeiSavedSearch::list($lei);
 }
 
+no warnings 'once';
+*ipc_atfork_child = \&PublicInbox::LeiInput::input_only_atfork_child;
+
 1;