# make sure client sees stdout before exit
$self->{1}->autoflush(1) if $self->{1};
dump_and_clear_log();
- if (my $s = $self->{pkt_op_p} // $self->{sock}) {
- send($s, "x_it $code", MSG_EOR);
+ if ($self->{pkt_op_p}) { # to top lei-daemon
+ $self->{pkt_op_p}->pkt_do('x_it', $code);
+ } elsif ($self->{sock}) { # to lei(1) client
+ send($self->{sock}, "x_it $code", MSG_EOR);
} # else ignore if client disconnected
}
my ($self, $buf, $exit_code) = @_;
err($self, $buf) if defined $buf;
# calls fail_handler:
- send($self->{pkt_op_p}, '!', MSG_EOR) if $self->{pkt_op_p};
+ $self->{pkt_op_p}->pkt_do('!') if $self->{pkt_op_p};
x_it($self, ($exit_code // 1) << 8);
undef;
}
sub child_error { # passes non-fatal curl exit codes to user
my ($self, $child_error, $msg) = @_; # child_error is $?
$self->err($msg) if $msg;
- if (my $s = $self->{pkt_op_p} // $self->{sock}) {
- # send to the parent lei-daemon or to lei(1) client
- send($s, "child_error $child_error", MSG_EOR);
- } elsif (!$PublicInbox::DS::in_loop) {
- $self->{child_error} = $child_error;
+ if ($self->{pkt_op_p}) { # to top lei-daemon
+ $self->{pkt_op_p}->pkt_do('child_error', $child_error);
+ } elsif ($self->{sock}) { # to lei(1) client
+ send($self->{sock}, "child_error $child_error", MSG_EOR);
} # else noop if client disconnected
}
sub note_sigpipe { # triggers sigpipe_handler
my ($self, $fd) = @_;
close(delete($self->{$fd})); # explicit close silences Perl warning
- send($self->{pkt_op_p}, '|', MSG_EOR) if $self->{pkt_op_p};
+ $self->{pkt_op_p}->pkt_do('|') if $self->{pkt_op_p};
x_it($self, 13);
}
if (my $op = delete $self->{pkt_op_c}) { # in case of die
$op->close; # PublicInbox::PktOp::close
}
- my $unclosed_after_die = delete($self->{pkt_op_p}) or return;
- close $unclosed_after_die;
+ my $pkt_op_p = delete($self->{pkt_op_p}) or return;
+ close $pkt_op_p->{op_p};
}
sub pkt_op_pair {
package PublicInbox::LeiAuth;
use strict;
use v5.10.1;
-use PublicInbox::PktOp qw(pkt_do);
sub do_auth_atfork { # used by IPC WQ workers
my ($self, $wq) = @_;
eval {
my $mics = $net->imap_common_init($lei);
my $nn = $net->nntp_common_init($lei);
- pkt_do($lei->{pkt_op_p}, 'net_merge_continue', $net) or
+ $lei->{pkt_op_p}->pkt_do('net_merge_continue', $net) or
die "pkt_do net_merge_continue: $!";
$net->{mics_cached} = $mics if $mics;
$net->{nn_cached} = $nn if $nn;
my ($wq, $net_new) = @_;
my $net = $wq->{lei}->{net};
%$net = (%$net, %$net_new);
- pkt_do($wq->{lei}->{pkt_op_p}, 'net_merge_done1') or
+ $wq->{lei}->{pkt_op_p}->pkt_do('net_merge_done1') or
die "pkt_op_do net_merge_done1: $!";
}
use PublicInbox::Eml;
use PublicInbox::ProcessPipe;
use PublicInbox::Spawn qw(spawn);
-use PublicInbox::PktOp qw(pkt_do);
use Symbol qw(gensym);
use IO::Handle; # ->autoflush
use Fcntl qw(SEEK_SET SEEK_END O_CREAT O_EXCL O_WRONLY);
my ($self) = @_;
my $lei = $self->{lei};
# lei_xsearch can start as soon as all l2m workers get here
- pkt_do($lei->{pkt_op_p}, 'incr_start_query') or
+ $lei->{pkt_op_p}->pkt_do('incr_start_query') or
die "incr_start_query: $!";
my $aug;
if (lock_free($self)) { # all workers do_augment
local $0 = 'do_augment';
eval { do_augment($self, $lei) };
$lei->fail($@) if $@;
- pkt_do($lei->{pkt_op_p}, $aug) == 1 or
+ $lei->{pkt_op_p}->pkt_do($aug) == 1 or
die "do_post_augment trigger: $!";
}
# done augmenting, connect the compressor pipe for each worker
$lei->{ale}->git->async_wait_all;
my $nr = delete($lei->{-nr_write}) or return;
return if $lei->{early_mua} || !$lei->{-progress} || !$lei->{pkt_op_p};
- require PublicInbox::PktOp;
- PublicInbox::PktOp::pkt_do($lei->{pkt_op_p}, 'l2m_progress', $nr);
+ $lei->{pkt_op_p}->pkt_do('l2m_progress', $nr);
}
# called in top-level lei-daemon when LeiAuth is done
use v5.10.1;
use parent qw(PublicInbox::LeiSearch PublicInbox::IPC);
use PublicInbox::DS qw(now);
-use PublicInbox::PktOp qw(pkt_do);
use File::Temp 0.19 (); # 0.19 for ->newdir
use File::Spec ();
use PublicInbox::Search qw(xap_terms);
my $lei = shift;
return if $lei->{early_mua} || !$lei->{-progress};
if ($lei->{pkt_op_p}) {
- pkt_do($lei->{pkt_op_p}, 'mset_progress', @_);
+ $lei->{pkt_op_p}->pkt_do('mset_progress', @_);
} else { # single lei-daemon consumer
my ($desc, $mset_size, $mset_total_est) = @_;
$lei->{-mset_total} += $mset_size if $mset_total_est ne '?';
package PublicInbox::PktOp;
use strict;
use v5.10.1;
-use parent qw(PublicInbox::DS Exporter);
+use parent qw(PublicInbox::DS);
use Errno qw(EAGAIN EINTR);
use PublicInbox::Syscall qw(EPOLLIN EPOLLET);
use Socket qw(AF_UNIX MSG_EOR SOCK_SEQPACKET);
use PublicInbox::IPC qw(ipc_freeze ipc_thaw);
-our @EXPORT_OK = qw(pkt_do);
sub new {
my ($cls, $r) = @_;
$self->SUPER::new($r, EPOLLIN|EPOLLET);
}
-# returns a blessed object as the consumer, and a GLOB/IO for the producer
+# returns a blessed objects as the consumer and producer
sub pair {
my ($cls) = @_;
my ($c, $p);
socketpair($c, $p, AF_UNIX, SOCK_SEQPACKET, 0) or die "socketpair: $!";
- (new($cls, $c), $p);
+ (new($cls, $c), bless { op_p => $p }, $cls);
}
sub pkt_do { # for the producer to trigger event_step in consumer
- my ($producer, $cmd, @args) = @_;
- send($producer, @args ? "$cmd\0".ipc_freeze(\@args) : $cmd, MSG_EOR);
+ my ($self, $cmd, @args) = @_;
+ send($self->{op_p}, @args ? "$cmd\0".ipc_freeze(\@args) : $cmd, MSG_EOR)
}
sub event_step {
my ($self) = @_;
if (my $lei = delete $self->{lei}) {
$lei->_lei_atfork_child;
- close(delete $lei->{pkt_op_p});
+ my $pkt_op_p = delete $lei->{pkt_op_p};
+ close($pkt_op_p->{op_p});
}
$self->SUPER::ipc_atfork_child;
}