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