]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiImport.pm
lei: improve management around short-lived workers
[public-inbox.git] / lib / PublicInbox / LeiImport.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 # front-end for the "lei import" sub-command
5 package PublicInbox::LeiImport;
6 use strict;
7 use v5.10.1;
8 use parent qw(PublicInbox::IPC PublicInbox::LeiInput);
9
10 # /^input_/ subs are used by (or override) PublicInbox::LeiInput superclass
11
12 sub input_eml_cb { # used by PublicInbox::LeiInput::input_fh
13         my ($self, $eml, $vmd) = @_;
14         my $xoids = $self->{lei}->{ale}->xoids_for($eml);
15         $self->{lei}->{sto}->ipc_do('set_eml', $eml, $vmd, $xoids);
16 }
17
18 sub input_mbox_cb { # MboxReader callback
19         my ($eml, $self) = @_;
20         my $vmd;
21         if ($self->{-import_kw}) {
22                 my $kw = PublicInbox::MboxReader::mbox_keywords($eml);
23                 $vmd = { kw => $kw } if scalar(@$kw);
24         }
25         input_eml_cb($self, $eml, $vmd);
26 }
27
28 sub input_maildir_cb { # maildir_each_eml cb
29         my ($f, $kw, $eml, $self) = @_;
30         input_eml_cb($self, $eml, $self->{-import_kw} ? { kw => $kw } : undef);
31 }
32
33 sub input_net_cb { # imap_each, nntp_each cb
34         my ($url, $uid, $kw, $eml, $self) = @_;
35         input_eml_cb($self, $eml, $self->{-import_kw} ? { kw => $kw } : undef);
36 }
37
38 sub import_done_wait { # dwaitpid callback
39         my ($arg, $pid) = @_;
40         my ($imp, $lei) = @$arg;
41         $lei->child_error($?, 'non-fatal errors during import') if $?;
42         my $sto = delete $lei->{sto};
43         my $wait = $sto->ipc_do('done') if $sto; # PublicInbox::LeiStore::done
44         $lei->dclose;
45 }
46
47 sub import_done { # EOF callback for main daemon
48         my ($lei) = @_;
49         my $imp = delete $lei->{imp} or return;
50         $imp->wq_wait_old(\&import_done_wait, $lei);
51 }
52
53 sub net_merge_complete { # callback used by LeiAuth
54         my ($self) = @_;
55         for my $input (@{$self->{inputs}}) {
56                 $self->wq_io_do('input_path_url', [], $input);
57         }
58         $self->wq_close(1);
59 }
60
61 sub lei_import { # the main "lei import" method
62         my ($lei, @inputs) = @_;
63         my $sto = $lei->_lei_store(1);
64         $sto->write_prepare($lei);
65         my $self = bless {}, __PACKAGE__;
66         $self->{-import_kw} = $lei->{opt}->{kw} // 1;
67         $self->prepare_inputs($lei, \@inputs) or return;
68         $lei->ale; # initialize for workers to read
69         my $j = $lei->{opt}->{jobs} // scalar(@{$self->{inputs}}) || 1;
70         if (my $net = $lei->{net}) {
71                 # $j = $net->net_concurrency($j); TODO
72         } else {
73                 my $nproc = $self->detect_nproc;
74                 $j = $nproc if $j > $nproc;
75         }
76         my $ops = { '' => [ \&import_done, $lei ] };
77         $lei->{auth}->op_merge($ops, $self) if $lei->{auth};
78         $self->{-wq_nr_workers} = $j // 1; # locked
79         my $op = $lei->workers_start($self, 'lei_import', undef, $ops);
80         $lei->{imp} = $self;
81         $self->wq_io_do('input_stdin', []) if $self->{0};
82         net_merge_complete($self) unless $lei->{auth};
83         while ($op && $op->{sock}) { $op->event_step }
84 }
85
86 no warnings 'once';
87 *ipc_atfork_child = \&PublicInbox::LeiInput::input_only_atfork_child;
88
89 # the following works even when LeiAuth is lazy-loaded
90 *net_merge_all = \&PublicInbox::LeiAuth::net_merge_all;
91 1;