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
31 sub ipc_atfork_child {
33 my $ipt = $self->{ipt} // die 'BUG: no self->{ipt}';
34 $ipt->{lei} = $self->{lei};
35 $ipt->ipc_atfork_child;
38 sub each_mdir_fn { # maildir_each_file callback
39 my ($f, $fl, $self, @args) = @_;
40 $self->wq_io_do('mdir_iter', [], $f, $fl, @args);
43 sub mdir_iter { # via wq_io_do
44 my ($self, $f, $fl, @args) = @_;
45 $self->{ipt}->pmdir_cb($f, $fl, @args);
50 my ($self, $lei) = @$arg;
51 my $wait = $lei->{sto}->ipc_do('done');
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 $pmd->wq_wait_old(\&pmd_done_wait, $lei);