]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiTag.pm
lei: standardize on _lei_wq_eof callback for workers
[public-inbox.git] / lib / PublicInbox / LeiTag.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 # handles "lei tag" command
5 package PublicInbox::LeiTag;
6 use strict;
7 use v5.10.1;
8 use parent qw(PublicInbox::IPC PublicInbox::LeiInput);
9
10 sub input_eml_cb { # used by PublicInbox::LeiInput::input_fh
11         my ($self, $eml) = @_;
12         if (my $xoids = $self->{lei}->{ale}->xoids_for($eml)) {
13                 $self->{lei}->{sto}->ipc_do('update_xvmd', $xoids, $eml,
14                                                 $self->{vmd_mod});
15         } else {
16                 ++$self->{missing};
17         }
18 }
19
20 sub input_mbox_cb { input_eml_cb($_[1], $_[0]) }
21
22 sub _lei_wq_eof { # EOF callback for main daemon
23         my ($lei) = @_;
24         my $tag = delete $lei->{tag} // return $lei->dclose;
25         $tag->wq_wait_old($lei->can('wq_done_wait'), $lei, 'non-fatal');
26 }
27
28 sub net_merge_complete { # callback used by LeiAuth
29         my ($self) = @_;
30         $self->wq_io_do('process_inputs');
31         $self->wq_close(1);
32 }
33
34 sub input_maildir_cb { # maildir_each_eml cb
35         my ($f, $kw, $eml, $self) = @_;
36         input_eml_cb($self, $eml);
37 }
38
39 sub input_net_cb { # imap_each, nntp_each cb
40         my ($url, $uid, $kw, $eml, $self) = @_;
41         input_eml_cb($self, $eml);
42 }
43
44 sub lei_tag { # the "lei tag" method
45         my ($lei, @argv) = @_;
46         my $sto = $lei->_lei_store(1);
47         $sto->write_prepare($lei);
48         my $self = bless { missing => 0 }, __PACKAGE__;
49         $lei->ale; # refresh and prepare
50         my $vmd_mod = $self->vmd_mod_extract(\@argv);
51         return $lei->fail(join("\n", @{$vmd_mod->{err}})) if $vmd_mod->{err};
52         $self->prepare_inputs($lei, \@argv) or return;
53         grep(defined, @$vmd_mod{qw(+kw +L -L -kw)}) or
54                 return $lei->fail('no keywords or labels specified');
55         my $ops = {};
56         $lei->{auth}->op_merge($ops, $self) if $lei->{auth};
57         $self->{vmd_mod} = $vmd_mod;
58         my $j = $self->{-wq_nr_workers} = 1; # locked for now
59         (my $op_c, $ops) = $lei->workers_start($self, 'lei-tag', $j, $ops);
60         $lei->{tag} = $self;
61         net_merge_complete($self) unless $lei->{auth};
62         $op_c->op_wait_event($ops);
63 }
64
65 sub note_missing {
66         my ($self) = @_;
67         my $n = $self->{missing} or return;
68         $self->{lei}->child_error(1 << 8, "$n missed messages");
69 }
70
71 sub ipc_atfork_child {
72         my ($self) = @_;
73         PublicInbox::LeiInput::input_only_atfork_child($self);
74         # this goes out-of-scope at worker process exit:
75         PublicInbox::OnDestroy->new($$, \&note_missing, $self);
76 }
77
78 # Workaround bash word-splitting s to ['kw', ':', 'keyword' ...]
79 # Maybe there's a better way to go about this in
80 # contrib/completion/lei-completion.bash
81 sub _complete_mark_common ($) {
82         my ($argv) = @_;
83         # Workaround bash word-splitting URLs to ['https', ':', '//' ...]
84         # Maybe there's a better way to go about this in
85         # contrib/completion/lei-completion.bash
86         my $re = '';
87         my $cur = pop(@$argv) // '';
88         if (@$argv) {
89                 my @x = @$argv;
90                 if ($cur eq ':' && @x) {
91                         push @x, $cur;
92                         $cur = '';
93                 }
94                 while (@x > 2 && $x[0] !~ /\A[+\-](?:kw|L)\z/ &&
95                                         $x[1] ne ':') {
96                         shift @x;
97                 }
98                 if (@x >= 2) { # qw(kw : $KEYWORD) or qw(kw :)
99                         $re = join('', @x);
100                 } else { # just return everything and hope for the best
101                         $re = join('', @$argv);
102                 }
103                 $re = quotemeta($re);
104         }
105         ($cur, $re);
106 }
107
108 # FIXME: same problems as _complete_forget_external and similar
109 sub _complete_tag {
110         my ($self, @argv) = @_;
111         my @L = eval { $self->_lei_store->search->all_terms('L') };
112         my @all = ((map { ("+kw:$_", "-kw:$_") } @PublicInbox::LeiInput::KW),
113                 (map { ("+L:$_", "-L:$_") } @L));
114         return @all if !@argv;
115         my ($cur, $re) = _complete_mark_common(\@argv);
116         map {
117                 # only return the part specified on the CLI
118                 # don't duplicate if already 100% completed
119                 /\A$re(\Q$cur\E.*)/ ? ($cur eq $1 ? () : $1) : ();
120         } grep(/$re\Q$cur/, @all);
121 }
122
123 no warnings 'once'; # the following works even when LeiAuth is lazy-loaded
124 *net_merge_all = \&PublicInbox::LeiAuth::net_merge_all;
125
126 1;