]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiPmdir.pm
lei: commit store on interrupted partial imports
[public-inbox.git] / lib / PublicInbox / LeiPmdir.pm
1 # Copyright (C) all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # WQ worker for dealing with parallel Maildir reads;
5 # this does NOT use the {shard_info} field of LeiToMail
6 # (and we may remove {shard_info})
7 # WQ key: {pmd}
8 package PublicInbox::LeiPmdir;
9 use strict;
10 use v5.10.1;
11 use parent qw(PublicInbox::IPC);
12
13 sub new {
14         my ($cls, $lei, $ipt) = @_;
15         my $self = bless { -wq_ident => 'lei Maildir worker' }, $cls;
16         my $jobs = $lei->{opt}->{jobs} // '';
17         $jobs =~ /\A[0-9]+,([0-9]+)\z/ and $jobs = $1;
18         my $nproc = $jobs || do {
19                 # barely tested with >=4 CPUs, though I suspect I/O latency
20                 # of SATA SSD storage will make >=4 processes unnecessary,
21                 # here.  NVMe users may wish to use '-j'
22                 my $n = $self->detect_nproc;
23                 $n = $n > 4 ? 4 : $n;
24         };
25         my ($op_c, $ops) = $lei->workers_start($self, $nproc,
26                 undef, { ipt => $ipt }); # LeiInput subclass
27         $op_c->{ops} = $ops; # for PktOp->event_step
28         $self->{lei_sock} = $lei->{sock}; # keep client for pmd_done_wait
29         $lei->{pmd} = $self;
30 }
31
32 sub ipc_atfork_child {
33         my ($self) = @_;
34         my $ipt = $self->{ipt} // die 'BUG: no self->{ipt}';
35         my $lei = $ipt->{lei} = $self->{lei};
36         delete @$lei{qw(auth net)}; # no network access in this worker
37         $ipt->ipc_atfork_child; # calls _lei_atfork_child;
38 }
39
40 sub each_mdir_fn { # maildir_each_file callback
41         my ($f, $fl, $self, @args) = @_;
42         $self->wq_io_do('mdir_iter', [], $f, $fl, @args);
43 }
44
45 sub mdir_iter { # via wq_io_do
46         my ($self, $f, $fl, @args) = @_;
47         $self->{ipt}->pmdir_cb($f, $fl, @args);
48 }
49
50 sub _lei_wq_eof { # EOF callback for main lei daemon
51         $_[0]->wq_eof('pmd');
52 }
53
54 1;