1 # Copyright (C) 2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
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})
8 package PublicInbox::LeiPmdir;
11 use parent qw(PublicInbox::IPC);
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;
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
32 sub ipc_atfork_child {
34 my $ipt = $self->{ipt} // die 'BUG: no self->{ipt}';
35 $ipt->{lei} = $self->{lei};
36 $ipt->ipc_atfork_child; # calls _lei_atfork_child;
39 sub each_mdir_fn { # maildir_each_file callback
40 my ($f, $fl, $self, @args) = @_;
41 $self->wq_io_do('mdir_iter', [], $f, $fl, @args);
44 sub mdir_iter { # via wq_io_do
45 my ($self, $f, $fl, @args) = @_;
46 $self->{ipt}->pmdir_cb($f, $fl, @args);
51 my ($self, $lei) = @$arg;
52 $lei->can('wq_done_wait')->($arg, $pid);
55 sub _lei_wq_eof { # EOF callback for main lei daemon
57 my $pmd = delete $lei->{pmd} or return $lei->fail;
58 $lei->sto_done_request($pmd->{lei_sock});
59 $pmd->wq_wait_old(\&pmd_done_wait, $lei);