1 # Copyright (C) all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # "lei up" - updates the result of "lei q --save"
5 package PublicInbox::LeiUp;
8 # n.b. we use LeiInput to setup IMAP auth
9 use parent qw(PublicInbox::IPC PublicInbox::LeiInput);
10 use PublicInbox::LeiSavedSearch; # OverIdx
12 use PublicInbox::PktOp;
13 use PublicInbox::LeiFinmsg;
14 my $REMOTE_RE = qr!\A(?:imap|http)s?://!i; # http(s) will be for JMAP
18 # precedence note for CLI switches between lei q and up:
19 # `lei q --only' > `lei q --no-(remote|local|external)'
20 # `lei up --no-(remote|local|external)' > `lei.q.only' in saved search
22 my $v = $lei->{opt}->{$_}; # set by CLI
23 (defined($v) && !$v) ? ($_ => 1) : ();
24 } qw(remote local external);
25 my $cli_exclude = delete $lei->{opt}->{exclude};
26 my $lss = PublicInbox::LeiSavedSearch->up($lei, $out) or return;
28 my $mset_opt = $lei->{mset_opt} = { relevance => -2 };
29 my $q = $lss->{-cfg}->get_all('lei.q') //
30 die("lei.q unset in $f (out=$out)\n");
31 my $lse = $lei->{lse} // die 'BUG: {lse} missing';
32 my $rawstr = $lss->{-cfg}->{'lei.internal.rawstr'} //
33 (scalar(@$q) == 1 && substr($q->[0], -1) eq "\n");
35 die <<EOM if scalar(@$q) > 1;
36 $f: lei.q has multiple values (@$q) (out=$out)
37 $f: while lei.internal.rawstr is set
39 $lse->query_approxidate($lse->git, $mset_opt->{qstr} = $q->[0]);
41 $mset_opt->{qstr} = $lse->query_argv_to_string($lse->git, $q);
43 # n.b. only a few CLI args are accepted for "up", so //= usually sets
44 for my $k ($lss->ARRAY_FIELDS) {
45 my $v = $lss->{-cfg}->get_all("lei.q.$k") // next;
46 $lei->{opt}->{$k} //= $v;
49 # --no-(local|remote) CLI flags overrided saved `lei.q.only'
50 my $only = $lei->{opt}->{only};
51 @$only = map { $lei->get_externals($_) } @$only if $only;
52 if (scalar keys %no && $only) {
53 @$only = grep(!m!\Ahttps?://!i, @$only) if $no{remote};
54 @$only = grep(m!\Ahttps?://!i, @$only) if $no{'local'};
57 my $ex = $lei->canonicalize_excludes($cli_exclude);
58 @$only = grep { !$ex->{$_} } @$only if $only;
59 push @{$lei->{opt}->{exclude}}, @$cli_exclude;
61 delete $lei->{opt}->{only} if $no{external} || ($only && !@$only);
62 for my $k ($lss->BOOL_FIELDS, $lss->SINGLE_FIELDS) {
63 my $v = $lss->{-cfg}->get_1("lei.q.$k") // next;
64 $lei->{opt}->{$k} //= $v;
66 my $o = $lei->{opt}->{output} // '';
67 return die("lei.q.output unset in $f (out=$out)\n") if $o eq '';
68 $lss->translate_dedupe($lei) or return;
69 $lei->{lss} = $lss; # for LeiOverview->new and query_remote_mboxrd
70 my $lxs = $lei->lxs_prepare or return;
71 $lei->ale->refresh_externals($lxs, $lei);
75 sub redispatch_all ($$) {
76 my ($self, $lei) = @_;
77 my $upq = [ (@{$self->{o_local} // []}, @{$self->{o_remote} // []}) ];
78 return up1($lei, $upq->[0]) if @$upq == 1; # just one, may start MUA
80 PublicInbox::OverIdx::fork_ok($lei->{opt});
81 # FIXME: this is also used per-query, see lei->_start_query
82 my $j = $lei->{opt}->{jobs} || do {
83 my $n = $self->detect_nproc // 1;
86 $j = ($j =~ /\A([0-9]+)/) ? $1 + 0 : 1; # may be --jobs=$x,$m on CLI
87 # re-dispatch into our event loop w/o creating an extra fork-level
88 # $upq will be drained via DESTROY as each query finishes
89 $lei->{fmsg} = PublicInbox::LeiFinmsg->new($lei);
90 my ($op_c, $op_p) = PublicInbox::PktOp->pair;
91 # call lei->dclose when upq is done processing:
92 $op_c->{ops} = { '' => [ $lei->can('dclose'), $lei ] };
93 my @first_batch = splice(@$upq, 0, $j); # initial parallelism
95 $lei->{daemon_pid} = $$;
96 $lei->event_step_init; # wait for client disconnects
97 for my $out (@first_batch) {
98 PublicInbox::DS::requeue(
99 PublicInbox::LeiUp1::nxt($lei, $out, $op_p));
104 my ($self, $lei, $all) = @_;
105 my @outs = PublicInbox::LeiSavedSearch::list($lei);
106 if ($all eq 'local') {
107 $self->{o_local} = [ grep(!/$REMOTE_RE/, @outs) ];
108 } elsif ($all eq 'remote') {
109 $self->{o_remote} = [ grep(/$REMOTE_RE/, @outs) ];
110 } elsif ($all eq '') {
111 $self->{o_remote} = [ grep(/$REMOTE_RE/, @outs) ];
112 $self->{o_local} = [ grep(!/$REMOTE_RE/, @outs) ];
119 my ($lei, @outs) = @_;
120 my $opt = $lei->{opt};
121 my $self = bless { -mail_sync => 1 }, __PACKAGE__;
122 if (defined(my $all = $opt->{all})) {
123 return $lei->fail("--all and @outs incompatible") if @outs;
124 defined($opt->{mua}) and return
125 $lei->fail('--all and --mua= are incompatible');
126 filter_lss($self, $lei, $all) // return
127 $lei->fail("only --all=$all not understood");
128 } elsif ($lei->{lse}) { # redispatched
129 scalar(@outs) == 1 or die "BUG: lse set w/ >1 out[@outs]";
130 return up1($lei, $outs[0]);
132 $self->{o_remote} = [ grep(/$REMOTE_RE/, @outs) ];
133 $self->{o_local} = [ grep(!/$REMOTE_RE/, @outs) ];
135 $lei->{lse} = $lei->_lei_store(1)->write_prepare($lei)->search;
136 ((@{$self->{o_local} // []} + @{$self->{o_remote} // []}) > 1 &&
137 defined($opt->{mua})) and return $lei->fail(<<EOM);
138 multiple outputs and --mua= are incompatible
140 if ($self->{o_remote}) { # setup lei->{auth}
141 $self->prepare_inputs($lei, $self->{o_remote}) or return;
143 if ($lei->{auth}) { # start auth worker
144 require PublicInbox::NetWriter;
145 bless $lei->{net}, 'PublicInbox::NetWriter';
146 $lei->wq1_start($self);
147 # net_merge_all_done will fire when auth is done
149 redispatch_all($self, $lei); # see below
153 # called in top-level lei-daemon when LeiAuth is done
154 sub net_merge_all_done {
155 my ($self, $lei) = @_;
156 $lei->{net} = delete($self->{-net_new}) if $self->{-net_new};
158 eval { redispatch_all($self, $lei) };
159 $lei->child_error(0, "E: $@") if $@;
162 sub _complete_up { # lei__complete hook
163 my ($lei, @argv) = @_;
164 my $match_cb = $lei->complete_url_prepare(\@argv);
165 map { $match_cb->($_) } PublicInbox::LeiSavedSearch::list($lei);
168 sub _wq_done_wait { # awaitpid cb (via awaitpid_init)
169 my ($pid, $wq, $lei) = @_;
170 $lei->child_error($?, 'auth failure') if $?
174 *ipc_atfork_child = \&PublicInbox::LeiInput::input_only_atfork_child;
176 package PublicInbox::LeiUp1; # for redispatch_all
181 my ($lei, $out, $op_p) = @_;
182 bless { lei => $lei, out => $out, op_p => $op_p }, __PACKAGE__;
185 sub event_step { # runs via PublicInbox::DS::requeue
187 my $lei = $self->{lei}; # the original, from lei_up
188 my $l = bless { %$lei }, ref($lei); # per-output copy
189 delete($l->{sock}) or return; # client disconnected if {sock} is gone
190 $l->{opt} = { %{$l->{opt}} }; # deep copy
191 delete $l->{opt}->{all};
192 $l->qerr("# updating $self->{out}");
193 my $o = " (output: $self->{out})"; # add to all warnings
194 my $cb = $SIG{__WARN__} // \&CORE::warn;
195 local $SIG{__WARN__} = sub {
197 push(@m, $o) if !@m || $m[-1] !~ s/\n\z/$o\n/;
200 $l->{-up1} = $self; # for LeiUp1->DESTROY
201 delete @$l{qw(-socks -event_init_done)};
202 my ($op_c, $op_p) = PublicInbox::PktOp->pair;
203 $self->{unref_on_destroy} = $op_c->{sock}; # to cleanup $lei->{-socks}
204 $lei->pkt_ops($op_c->{ops} //= {}); # errors from $l -> script/lei
205 push @{$lei->{-socks}}, $op_c->{sock}; # script/lei signals to $l
206 $l->{sock} = $op_p->{op_p}; # receive signals from op_c->{sock}
207 $op_c = $op_p = undef;
209 eval { $l->dispatch('up', $self->{out}) };
210 $lei->child_error(0, $@) if $@ || $l->{failed}; # lei->fail()
215 my $lei = $self->{lei}; # the original, from lei_up
216 return if $lei->{daemon_pid} != $$;
217 my $sock = delete $self->{unref_on_destroy};
218 my $s = $lei->{-socks} // [];
219 @$s = grep { $_ != $sock } @$s;
220 my $out = shift(@{$lei->{-upq}}) or return;
221 PublicInbox::DS::requeue(nxt($lei, $out, $self->{op_p}));