]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiUp.pm
lei up: infer rawstr from old searches via trailing "\n"
[public-inbox.git] / lib / PublicInbox / LeiUp.pm
1 # Copyright (C) 2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # "lei up" - updates the result of "lei q --save"
5 package PublicInbox::LeiUp;
6 use strict;
7 use v5.10.1;
8 # n.b. we use LeiInput to setup IMAP auth
9 use parent qw(PublicInbox::IPC PublicInbox::LeiInput);
10 use PublicInbox::LeiSavedSearch;
11 use PublicInbox::DS;
12 use PublicInbox::PktOp;
13 use PublicInbox::LeiFinmsg;
14 my $REMOTE_RE = qr!\A(?:imap|http)s?://!i; # http(s) will be for JMAP
15
16 sub up1 ($$) {
17         my ($lei, $out) = @_;
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
21         my %no = map {
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;
27         my $f = $lss->{'-f'};
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");
34         if ($rawstr) {
35                 scalar(@$q) > 1 and
36                         die "$f: lei.q has multiple values (@$q) (out=$out)\n";
37                 $lse->query_approxidate($lse->git, $mset_opt->{qstr} = $q->[0]);
38         } else {
39                 $mset_opt->{qstr} = $lse->query_argv_to_string($lse->git, $q);
40         }
41         # n.b. only a few CLI args are accepted for "up", so //= usually sets
42         for my $k ($lss->ARRAY_FIELDS) {
43                 my $v = $lss->{-cfg}->get_all("lei.q.$k") // next;
44                 $lei->{opt}->{$k} //= $v;
45         }
46
47         # --no-(local|remote) CLI flags overrided saved `lei.q.only'
48         my $only = $lei->{opt}->{only};
49         @$only = map { $lei->get_externals($_) } @$only if $only;
50         if (scalar keys %no && $only) {
51                 @$only = grep(!m!\Ahttps?://!i, @$only) if $no{remote};
52                 @$only = grep(m!\Ahttps?://!i, @$only) if $no{'local'};
53         }
54         if ($cli_exclude) {
55                 my $ex = $lei->canonicalize_excludes($cli_exclude);
56                 @$only = grep { !$ex->{$_} } @$only if $only;
57                 push @{$lei->{opt}->{exclude}}, @$cli_exclude;
58         }
59         delete $lei->{opt}->{only} if $no{external} || ($only && !@$only);
60         for my $k ($lss->BOOL_FIELDS, $lss->SINGLE_FIELDS) {
61                 my $v = $lss->{-cfg}->get_1("lei.q.$k") // next;
62                 $lei->{opt}->{$k} //= $v;
63         }
64         my $o = $lei->{opt}->{output} // '';
65         return die("lei.q.output unset in $f (out=$out)\n") if $o eq '';
66         $lss->translate_dedupe($lei) or return;
67         $lei->{lss} = $lss; # for LeiOverview->new and query_remote_mboxrd
68         my $lxs = $lei->lxs_prepare or return;
69         $lei->ale->refresh_externals($lxs, $lei);
70         $lei->_start_query;
71 }
72
73 sub redispatch_all ($$) {
74         my ($self, $lei) = @_;
75         my $upq = [ (@{$self->{o_local} // []}, @{$self->{o_remote} // []}) ];
76         return up1($lei, $upq->[0]) if @$upq == 1; # just one, may start MUA
77
78         # FIXME: this is also used per-query, see lei->_start_query
79         my $j = $lei->{opt}->{jobs} || do {
80                 my $n = $self->detect_nproc // 1;
81                 $n > 4 ? 4 : $n;
82         };
83         $j = ($j =~ /\A([0-9]+)/) ? $1 + 0 : 1; # may be --jobs=$x,$m on CLI
84         # re-dispatch into our event loop w/o creating an extra fork-level
85         # $upq will be drained via DESTROY as each query finishes
86         $lei->{fmsg} = PublicInbox::LeiFinmsg->new($lei);
87         my ($op_c, $op_p) = PublicInbox::PktOp->pair;
88         # call lei->dclose when upq is done processing:
89         $op_c->{ops} = { '' => [ $lei->can('dclose'), $lei ] };
90         my @first_batch = splice(@$upq, 0, $j); # initial parallelism
91         $lei->{-upq} = $upq;
92         $lei->{daemon_pid} = $$;
93         $lei->event_step_init; # wait for client disconnects
94         for my $out (@first_batch) {
95                 PublicInbox::DS::requeue(
96                         PublicInbox::LeiUp1::nxt($lei, $out, $op_p));
97         }
98 }
99
100 sub filter_lss {
101         my ($self, $lei, $all) = @_;
102         my @outs = PublicInbox::LeiSavedSearch::list($lei);
103         if ($all eq 'local') {
104                 $self->{o_local} = [ grep(!/$REMOTE_RE/, @outs) ];
105         } elsif ($all eq 'remote') {
106                 $self->{o_remote} = [ grep(/$REMOTE_RE/, @outs) ];
107         } elsif ($all eq '') {
108                 $self->{o_remote} = [ grep(/$REMOTE_RE/, @outs) ];
109                 $self->{o_local} = [ grep(!/$REMOTE_RE/, @outs) ];
110         } else {
111                 undef;
112         }
113 }
114
115 sub lei_up {
116         my ($lei, @outs) = @_;
117         my $opt = $lei->{opt};
118         my $self = bless { -mail_sync => 1 }, __PACKAGE__;
119         if (defined(my $all = $opt->{all})) {
120                 return $lei->fail("--all and @outs incompatible") if @outs;
121                 defined($opt->{mua}) and return
122                         $lei->fail('--all and --mua= are incompatible');
123                 filter_lss($self, $lei, $all) // return
124                         $lei->fail("only --all=$all not understood");
125         } elsif ($lei->{lse}) { # redispatched
126                 scalar(@outs) == 1 or die "BUG: lse set w/ >1 out[@outs]";
127                 return up1($lei, $outs[0]);
128         } else {
129                 $self->{o_remote} = [ grep(/$REMOTE_RE/, @outs) ];
130                 $self->{o_local} = [ grep(!/$REMOTE_RE/, @outs) ];
131         }
132         $lei->{lse} = $lei->_lei_store(1)->write_prepare($lei)->search;
133         ((@{$self->{o_local} // []} + @{$self->{o_remote} // []}) > 1 &&
134                 defined($opt->{mua})) and return $lei->fail(<<EOM);
135 multiple outputs and --mua= are incompatible
136 EOM
137         if ($self->{o_remote}) { # setup lei->{auth}
138                 $self->prepare_inputs($lei, $self->{o_remote}) or return;
139         }
140         if ($lei->{auth}) { # start auth worker
141                 require PublicInbox::NetWriter;
142                 bless $lei->{net}, 'PublicInbox::NetWriter';
143                 $lei->wq1_start($self);
144                 # net_merge_all_done will fire when auth is done
145         } else {
146                 redispatch_all($self, $lei); # see below
147         }
148 }
149
150 # called in top-level lei-daemon when LeiAuth is done
151 sub net_merge_all_done {
152         my ($self, $lei) = @_;
153         $lei->{net} = delete($self->{-net_new}) if $self->{-net_new};
154         $self->wq_close;
155         eval { redispatch_all($self, $lei) };
156         $lei->child_error(0, "E: $@") if $@;
157 }
158
159 sub _complete_up { # lei__complete hook
160         my ($lei, @argv) = @_;
161         my $match_cb = $lei->complete_url_prepare(\@argv);
162         map { $match_cb->($_) } PublicInbox::LeiSavedSearch::list($lei);
163 }
164
165 sub _wq_done_wait { # dwaitpid callback
166         my ($arg, $pid) = @_;
167         my ($wq, $lei) = @$arg;
168         $lei->child_error($?, 'auth failure') if $?
169 }
170
171 no warnings 'once';
172 *ipc_atfork_child = \&PublicInbox::LeiInput::input_only_atfork_child;
173
174 package PublicInbox::LeiUp1; # for redispatch_all
175 use strict;
176 use v5.10.1;
177
178 sub nxt ($$$) {
179         my ($lei, $out, $op_p) = @_;
180         bless { lei => $lei, out => $out, op_p => $op_p }, __PACKAGE__;
181 }
182
183 sub event_step { # runs via PublicInbox::DS::requeue
184         my ($self) = @_;
185         my $lei = $self->{lei}; # the original, from lei_up
186         my $l = bless { %$lei }, ref($lei); # per-output copy
187         delete($l->{sock}) or return; # client disconnected if {sock} is gone
188         $l->{opt} = { %{$l->{opt}} }; # deep copy
189         delete $l->{opt}->{all};
190         $l->qerr("# updating $self->{out}");
191         my $o = " (output: $self->{out})"; # add to all warnings
192         my $cb = $SIG{__WARN__} // \&CORE::warn;
193         local $SIG{__WARN__} = sub {
194                 my @m = @_;
195                 push(@m, $o) if !@m || $m[-1] !~ s/\n\z/$o\n/;
196                 $cb->(@m);
197         };
198         $l->{-up1} = $self; # for LeiUp1->DESTROY
199         delete @$l{qw(-socks -event_init_done)};
200         my ($op_c, $op_p) = PublicInbox::PktOp->pair;
201         $self->{unref_on_destroy} = $op_c->{sock}; # to cleanup $lei->{-socks}
202         $lei->pkt_ops($op_c->{ops} //= {}); # errors from $l -> script/lei
203         push @{$lei->{-socks}}, $op_c->{sock}; # script/lei signals to $l
204         $l->{sock} = $op_p->{op_p}; # receive signals from op_c->{sock}
205         $op_c = $op_p = undef;
206
207         eval { $l->dispatch('up', $self->{out}) };
208         $lei->child_error(0, $@) if $@ || $l->{failed}; # lei->fail()
209 }
210
211 sub DESTROY {
212         my ($self) = @_;
213         my $lei = $self->{lei}; # the original, from lei_up
214         return if $lei->{daemon_pid} != $$;
215         my $sock = delete $self->{unref_on_destroy};
216         my $s = $lei->{-socks} // [];
217         @$s = grep { $_ != $sock } @$s;
218         my $out = shift(@{$lei->{-upq}}) or return;
219         PublicInbox::DS::requeue(nxt($lei, $out, $self->{op_p}));
220 }
221
222 1;