lib/Mojo/IOLoop/ReadWriteProcess.pm

filter: Statementstmt 98.0% 402 / 410 Branchbran 69.6% 220 / 316 Conditioncond 53.7% 78 / 145 MC/DCmcdc 22.9% 17 / 74 Subroutinesub 98.7% 76 / 77 Podpod 93.1% 27 / 29 totaltotal 78.0% 820 / 1051 CC 419 SCAR 38.9
CC
419
Cov
80%
_fork
138.0
_fork
138.0
_fork
138.0
CRAP 48.8
« lib/Mojo/IOLoop/ReadWriteProcess/Shared/Memory.pm ↑ Summary lib/Mojo/IOLoop/ReadWriteProcess/Session.pm »
1 package Mojo::IOLoop::ReadWriteProcess;
2  
3 41 our $VERSION = '1.1.0';
4  
5 40 ▶use Mojo::Base 'Mojo::EventEmitter';
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
6 40 ▶use Mojo::File 'path';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 40 ▶use Mojo::Util qw(b64_decode b64_encode scope_guard);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 40 ▶use Mojo::IOLoop::Stream;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 40 ▶use Mojo::IOLoop::ReadWriteProcess::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 40 ▶use Mojo::IOLoop::ReadWriteProcess::Pool;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 40 ▶use Mojo::IOLoop::ReadWriteProcess::Queue;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 40 ▶use Mojo::IOLoop::ReadWriteProcess::Session;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 40 ▶use Mojo::IOLoop::ReadWriteProcess::Shared::Lock;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 40 ▶use Mojo::IOLoop::ReadWriteProcess::Shared::Memory;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 40 ▶use Mojo::IOLoop::ReadWriteProcess::Shared::Semaphore;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 40 ▶use B::Deparse;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 40 ▶use Carp 'confess';
Subroutine BEGIN: called CC=1 SCAR=0.0
21 40 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 40 ▶use IO::Pipe;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 40 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 40 ▶use IPC::Open3;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 40 ▶use Time::HiRes 'sleep';
Subroutine BEGIN: called CC=1 SCAR=0.0
26 40 ▶use Symbol 'gensym';
Subroutine BEGIN: called CC=1 SCAR=0.0
27 40 ▶use Storable;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 40 ▶use POSIX qw( :sys_wait_h :signal_h );
Subroutine BEGIN: called CC=1 SCAR=0.0
29 our @EXPORT_OK
30 40 = (qw(parallel batch process pool queue), qw(shared_memory lock semaphore));
31 40 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 40 ▶use constant DEBUG => $ENV{MOJO_PROCESS_DEBUG};
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 39 has [
36 qw(kill_sleeptime sleeptime_during_kill),
37 qw(separate_err autoflush set_pipes verbose),
38 qw(internal_pipes channels)
39 ] => 1;
40  
41 40 has [qw(blocking_stop serialize quirkiness total_sleeptime_during_kill)] => 0;
42  
43 40 has [
44 qw(execute code process_id pidfile return_status),
45 qw(channel_in channel_out write_stream read_stream error_stream),
46 qw(_internal_err _internal_return _status args)
47 ];
48  
49 40 has max_kill_attempts => 5;
50 47 has kill_whole_group => 0;
51  
52 194 ▶has error => sub { Mojo::Collection->new };
Subroutine __ANON__: called CC=1 SCAR=0.0
53  
54 2 ▶has ioloop => sub { Mojo::IOLoop->singleton };
Subroutine __ANON__: called CC=1 SCAR=0.0
55 284 ▶has session => sub { Mojo::IOLoop::ReadWriteProcess::Session->singleton };
Subroutine __ANON__: called CC=1 SCAR=0.0
56  
57 6 ▶has _deparse => sub { B::Deparse->new }
Subroutine __ANON__: called CC=1 SCAR=0.0
58 47 if DEBUG;
59 2 ▶has _deserialize => sub { \&Storable::thaw };
Subroutine __ANON__: called CC=1 SCAR=0.0
60 1 ▶has _serialize => sub { \&Storable::freeze };
Subroutine __ANON__: called CC=1 SCAR=0.0
61 55 has _default_kill_signal => POSIX::SIGTERM;
62 55 has _default_blocking_signal => POSIX::SIGKILL;
63  
64 # Override new() just to support sugar syntax
65 # so it is possible to do : process->new(sub{ print "Hello World\n" })->start->stop; and so on.
66 394 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
Pod documented
Pod documented
67 394 ▶ push(@_, code => splice @_, 1, 1) if ref $_[1] eq "CODE";
Branch: 3 branches
BranchTrueFalseTotal
if ref $_[1] eq "CODE" 200 193 393
if ref $_[1] eq "CODE" 2 1 3
if ref $_[1] eq "CODE" 1 1 2
68 394 return shift->SUPER::new(@_);
69 }
70  
71 3 ▶sub to_ioloop {
Subroutine to_ioloop: called CC=2 SCAR=7.0
Pod documented
72 3 my $self = shift;
73 3 ▶ confess 'Pipes needs to be set!' unless $self->read_stream;
Branch: 2 branches
BranchTrueFalseTotal
unless $self->read_stream 0 2 2
unless $self->read_stream 1 1 2
74 12 my $stream = Mojo::IOLoop::Stream->new($self->read_stream)->timeout(0);
75 12 $self->ioloop->stream($stream);
76 2 my $me = $$;
77 $stream->on(
78 12 ▶ close => sub {
Subroutine __ANON__: called CC=3 SCAR=11.2
79 2 ▶ return unless $$ == $me;
Branch: 2 branches
BranchTrueFalseTotal
unless $$ == $me 5 6 11
unless $$ == $me 0 6 6
80 2 ▶ $self->_collect->stop unless defined $self->_status;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $self->_status 0 1 1
unless defined $self->_status 5 16 21
81 2 });
82 2 return $stream;
83 }
84  
85 179 ▶sub process { __PACKAGE__->new(@_) }
Subroutine process: called CC=1 SCAR=0.0
Pod documented
86 6 ▶sub batch { Mojo::IOLoop::ReadWriteProcess::Pool->new(@_) }
Subroutine batch: called CC=1 SCAR=0.0
Pod documented
87 6 ▶sub queue { Mojo::IOLoop::ReadWriteProcess::Queue->new(@_) }
Subroutine queue: called CC=1 SCAR=0.0
Pod documented
88 1 ▶sub lock { Mojo::IOLoop::ReadWriteProcess::Shared::Lock->new(@_) }
Subroutine lock: called CC=1 SCAR=0.0
Pod documented
89 1 ▶sub semaphore { Mojo::IOLoop::ReadWriteProcess::Shared::Semaphore->new(@_) }
Subroutine semaphore: called CC=1 SCAR=0.0
Pod undocumented
90 1 ▶sub shared_memory { Mojo::IOLoop::ReadWriteProcess::Shared::Memory->new(@_) }
Subroutine shared_memory: called CC=1 SCAR=0.0
Pod undocumented
91  
92 3 ▶sub parallel {
Subroutine parallel: called CC=2 SCAR=6.9
Pod documented
93 3 my $c = batch();
94 3 $c->add(@_) for 1 .. +pop();
95 3 return $c;
96 }
97  
98 4 ▶sub _diag {
Subroutine _diag: called CC=2 SCAR=7.1
99 4 my ($self, @messages) = @_;
100 4 my $caller = (caller(1))[3];
101 4 ▶ print STDERR ">> ${caller}(): @messages\n" if (DEBUG || $self->verbose);
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 3 0 3
102 }
103  
104 37 ▶sub _open_collect_status {
Subroutine _open_collect_status: called CC=4 SCAR=14.2
105 37 my ($self, $pid, $e, $errno) = @_;
106  
107 37 ▶ return unless $self;
Branch: 2 branches
BranchTrueFalseTotal
unless $self 0 27 27
unless $self 12 9 21
108  
109 37 ▶ $self->_status($e // $?) unless defined $self->_status;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $self->_status 27 0 27
unless defined $self->_status 5 9 14
Condition66%4/6
$e // $?
l!l&&r!l&&!r
2700
$e // $?
l!l&&r!l&&!r
111
MC/DC0%0/2
$e // $?
$e $?
MC/DC0%0/2
$e // $?
$e $?
Truth table33%1/3
A // B
A= $eB= $?
ABresult
1X1
011
000
Truth table0%0/3
A // B
A= $eB= $?
ABresult
1X1
011
000
110 69 $self->_diag("Forked code Process Exit status: " . $self->exit_status)
111 if DEBUG;
112  
113 69 $self->_clean_pidfile;
114  
115 69 return $self;
116 }
117  
118 # Use open3 to launch external program.
119 89 ▶sub _open {
Subroutine _open: called CC=5 SCAR=16.1
120 48 my ($self, @args) = @_;
121 48 $self->_diag('Execute: ' . (join ', ', map { "'$_'" } @args)) if DEBUG;
122  
123 48 $self->on(collect_status => \&_open_collect_status);
124  
125 48 my ($wtr, $rdr, $err);
126 48 $err = gensym;
127 48 ▶ my $pid = open3($wtr, $rdr, ($self->separate_err) ? $err : undef, @args);
Branch: 2 branches
BranchTrueFalseTotal
$self->separate_err ? : 22 25 47
$self->separate_err ? : 7 9 16
128  
129 44 ▶ die "Cannot create pipe: $!" unless defined $pid;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $pid 5 42 47
unless defined $pid 7 0 7
130 43 $self->process_id($pid);
131  
132 # Defered collect of return status and removal of pidfile
133  
134 43 ▶ return $self unless $self->set_pipes();
Branch: 2 branches
BranchTrueFalseTotal
unless $self->set_pipes 3 39 42
unless $self->set_pipes 7 0 7
135  
136 40 $self->read_stream(IO::Handle->new_from_fd($rdr, "r"));
137 40 $self->write_stream(IO::Handle->new_from_fd($wtr, "w"));
138 40 ▶ $self->error_stream(($self->separate_err)
Branch: 2 branches
BranchTrueFalseTotal
$self->separate_err ? : 21 18 39
$self->separate_err ? : 6 0 6
139 ? IO::Handle->new_from_fd($err, "r")
140 : $self->write_stream);
141  
142 40 return $self;
143 }
144  
145 492 ▶sub _clean_pidfile { unlink(shift->pidfile) if $_[0]->pidfile }
Subroutine _clean_pidfile: called CC=2 SCAR=7.4
Branch: 3 branches
BranchTrueFalseTotal
if $_[0]->pidfile 11 480 491
if $_[0]->pidfile 5 0 5
if $_[0]->pidfile 5 0 5
146  
147 3 ▶sub _collect {
Subroutine _collect: called CC=2 SCAR=7.6
148 8 my ($self, $pid) = @_;
149 25 ▶ $pid //= $self->pid;
Condition50%3/6
$pid //= $self->pid
l!l&&r!l&&!r
400
$pid //= $self->pid
l!l&&r!l&&!r
210
MC/DC0%0/2
$pid //= $self->pid
$pid $self->pid
MC/DC0%0/2
$pid //= $self->pid
$pid $self->pid
Truth table33%1/3
A //= B
A= $pidB= $self->pid
ABresult
1X1
011
000
Truth table0%0/3
A //= B
A= $pidB= $self->pid
ABresult
1X1
011
000
150  
151 6 $self->session->consume_collected_info;
152 $self->session->_protect(
153 18 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=15.8
154 6 local $?;
155 5 ▶ waitpid $pid, 0 unless defined $self->_status;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $self->_status 0 6 6
unless defined $self->_status 5 0 5
156 5 ▶ return $self->_open_collect_status($pid) if $self->execute;
Branch: 2 branches
BranchTrueFalseTotal
if $self->execute 0 21 21
if $self->execute 0 5 5
157 5 ▶ return $self->_fork_collect_status($pid) if $self->code;
Branch: 2 branches
BranchTrueFalseTotal
if $self->code 4 0 4
if $self->code 0 5 5
158 6 });
159  
160 6 $self;
161 }
162  
163 240 ▶sub _fork_collect_status {
Subroutine _fork_collect_status: called CC=15 SCAR=38.6
164 244 my ($self, $pid, $e, $errno) = @_;
165  
166 244 ▶ return unless $self;
Branch: 3 branches
BranchTrueFalseTotal
unless $self 1 238 239
unless $self 0 0 0
unless $self 0 0 0
167  
168 243 my $return_reader;
169 my $internal_err_reader;
170 243 my $rt;
171 239 my @result_error;
172  
173 239 ▶ $self->_status($e // $?) unless defined $self->_status;
Branch: 3 branches
BranchTrueFalseTotal
unless defined $self->_status 238 0 238
unless defined $self->_status 0 0 0
unless defined $self->_status 0 0 0
Condition66%6/9
$e // $?
l!l&&r!l&&!r
23620
$e // $?
l!l&&r!l&&!r
280
$e // $?
l!l&&r!l&&!r
011
MC/DC0%0/2
$e // $?
$e $?
MC/DC0%0/2
$e // $?
$e $?
MC/DC50%1/2
$e // $?
$e $?
Truth table66%2/3
A // B
A= $eB= $?
ABresult
1X1
011
000
Truth table66%2/3
A // B
A= $eB= $?
ABresult
1X1
011
000
Truth table66%2/3
A // B
A= $eB= $?
ABresult
1X1
011
000
174 239 $self->_diag("Forked code Process Exit status: " . $self->exit_status)
175 if DEBUG;
176  
177 239 ▶ if ($self->_internal_return) {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->_internal_return) 183 55 238
if ($self->_internal_return) 0 0 0
if ($self->_internal_return) 0 0 0
178 184 ▶ $return_reader
Branch: 3 branches
BranchTrueFalseTotal
$self->_internal_return->isa('IO::Pipe::End') ? : 0 183 183
$self->_internal_return->isa('IO::Pipe::End') ? : 0 0 0
$self->_internal_return->isa('IO::Pipe::End') ? : 0 0 0
179 = $self->_internal_return->isa("IO::Pipe::End")
180 ? $self->_internal_return
181 : $self->_internal_return->reader();
182 184 ▶ $self->_new_err('Cannot read from return code pipe') && return
Branch: 3 branches
BranchTrueFalseTotal
unless "IO::Select"->new($return_reader)->can_read(10) 0 183 183
unless "IO::Select"->new($return_reader)->can_read(10) 0 0 0
unless "IO::Select"->new($return_reader)->can_read(10) 0 0 0
Condition35%6/17
'IO::Select'->new($return_reader)->can_read(10) || $self->_new_err('Cannot read from return code pipe') && (return)
l!l&&r!l&&!r
18300
$self->_new_err("Cannot read from return code pipe") and return
!ll
00
'IO::Select'->new($return_reader)->can_read(10) || $self->_new_err('Cannot read from return code pipe') && (return)
l!l
080
$self->_new_err("Cannot read from return code pipe") and return
!ll
100
'IO::Select'->new($return_reader)->can_read(10) || $self->_new_err('Cannot read from return code pipe') && (return)
l!l
200
$self->_new_err("Cannot read from return code pipe") and return
!ll
110
MC/DC0%0/2
'IO::Select'->new($return_reader)->can_read(10) || $self->_new_err('Cannot read from return code pipe') && (return)
'IO::Select'->new($return_reader)->can_read(10) $self->_new_err("Cannot read from return code pipe")
MC/DC0%0/2
'IO::Select'->new($return_reader)->can_read(10) || $self->_new_err('Cannot read from return code pipe') && (return)
'IO::Select'->new($return_reader)->can_read(10) $self->_new_err("Cannot read from return code pipe")
MC/DC0%0/2
'IO::Select'->new($return_reader)->can_read(10) || $self->_new_err('Cannot read from return code pipe') && (return)
'IO::Select'->new($return_reader)->can_read(10) $self->_new_err("Cannot read from return code pipe")
Truth table33%1/3
A || B
A= 'IO::Select'->new($return_reader)->can_read(10)B= $self->_new_err("Cannot read from return code pipe")
ABresult
1X1
011
000
Truth table0%0/3
A || B
A= 'IO::Select'->new($return_reader)->can_read(10)B= $self->_new_err("Cannot read from return code pipe")
ABresult
1X1
011
000
Truth table33%1/3
A || B
A= 'IO::Select'->new($return_reader)->can_read(10)B= $self->_new_err("Cannot read from return code pipe")
ABresult
1X1
011
000
183 unless IO::Select->new($return_reader)->can_read(10);
184 184 $rt = $return_reader->getline();
185 183 ▶ $self->_diag("Forked code Process Returns: " . ($rt ? $rt : 'nothing'))
Branch: 2 branches
BranchTrueFalseTotal
$rt ? : 0 0 0
$rt ? : 0 0 0
186 if DEBUG;
187 $self->return_status(
188 183 ▶ $self->serialize ? eval { $self->_deserialize->(b64_decode($rt)) }
Branch: 6 branches
BranchTrueFalseTotal
$self->serialize ? : 1 182 183
$rt ? : 152 30 182
$self->serialize ? : 0 0 0
$rt ? : 0 0 0
$self->serialize ? : 0 0 0
$rt ? : 0 0 0
189 : $rt ? $rt
190 : ());
191 }
192 238 ▶ if ($self->_internal_err) {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->_internal_err) 184 54 238
if ($self->_internal_err) 0 0 0
if ($self->_internal_err) 0 0 0
193 184 ▶ $internal_err_reader
Branch: 3 branches
BranchTrueFalseTotal
$self->_internal_err->isa('IO::Pipe::End') ? : 1 183 184
$self->_internal_err->isa('IO::Pipe::End') ? : 0 0 0
$self->_internal_err->isa('IO::Pipe::End') ? : 0 0 0
194 = $self->_internal_err->isa("IO::Pipe::End")
195 ? $self->_internal_err
196 : $self->_internal_err->reader();
197 184 ▶ $self->_new_err('Cannot read from errors code pipe') && return
Branch: 3 branches
BranchTrueFalseTotal
unless "IO::Select"->new($internal_err_reader)->can_read(10) 1 183 184
unless "IO::Select"->new($internal_err_reader)->can_read(10) 0 0 0
unless "IO::Select"->new($internal_err_reader)->can_read(10) 0 5 5
Condition47%8/17
'IO::Select'->new($internal_err_reader)->can_read(10) || $self->_new_err('Cannot read from errors code pipe') && (return)
l!l&&r!l&&!r
18310
$self->_new_err("Cannot read from errors code pipe") and return
!ll
01
'IO::Select'->new($internal_err_reader)->can_read(10) || $self->_new_err('Cannot read from errors code pipe') && (return)
l!l
102
$self->_new_err("Cannot read from errors code pipe") and return
!ll
111
'IO::Select'->new($internal_err_reader)->can_read(10) || $self->_new_err('Cannot read from errors code pipe') && (return)
l!l
000
$self->_new_err("Cannot read from errors code pipe") and return
!ll
000
MC/DC0%0/2
'IO::Select'->new($internal_err_reader)->can_read(10) || $self->_new_err('Cannot read from errors code pipe') && (return)
'IO::Select'->new($internal_err_reader)->can_read(10) $self->_new_err("Cannot read from errors code pipe")
MC/DC0%0/2
'IO::Select'->new($internal_err_reader)->can_read(10) || $self->_new_err('Cannot read from errors code pipe') && (return)
'IO::Select'->new($internal_err_reader)->can_read(10) $self->_new_err("Cannot read from errors code pipe")
MC/DC0%0/2
'IO::Select'->new($internal_err_reader)->can_read(10) || $self->_new_err('Cannot read from errors code pipe') && (return)
'IO::Select'->new($internal_err_reader)->can_read(10) $self->_new_err("Cannot read from errors code pipe")
Truth table66%2/3
A || B
A= 'IO::Select'->new($internal_err_reader)->can_read(10)B= $self->_new_err("Cannot read from errors code pipe")
ABresult
1X1
011
000
Truth table0%0/3
A || B
A= 'IO::Select'->new($internal_err_reader)->can_read(10)B= $self->_new_err("Cannot read from errors code pipe")
ABresult
1X1
011
000
Truth table0%0/3
A || B
A= 'IO::Select'->new($internal_err_reader)->can_read(10)B= $self->_new_err("Cannot read from errors code pipe")
ABresult
1X1
011
000
198 unless IO::Select->new($internal_err_reader)->can_read(10);
199 183 @result_error = $internal_err_reader->getlines();
200 push(
201 60 @{$self->error},
202 183 ▶ map { Mojo::IOLoop::ReadWriteProcess::Exception->new($_) } @result_error
Branch: 3 branches
BranchTrueFalseTotal
if @result_error 60 123 183
if @result_error 0 0 0
if @result_error 5 0 5
203 ) if @result_error;
204 183 $self->_diag("Forked code Process Errors: " . join("\n", @result_error))
205 if DEBUG;
206 }
207  
208 237 $self->_clean_pidfile;
209 237 return $self;
210 }
211  
212 # Handle forking of code
213 222 ▶sub _fork {
Subroutine _fork: called CC=60 SCAR=49.3
214 222 my ($self, $code, @args) = @_;
215 222 ▶ die "Can't spawn child without code" unless ref($code) eq "CODE";
Branch: 2 branches
BranchTrueFalseTotal
unless ref $code eq "CODE" 3 219 222
unless ref $code eq "CODE" 0 5 5
216  
217 # STDIN/STDOUT/STDERR redirect.
218 219 my ($input_pipe, $output_pipe, $output_err_pipe);
219  
220 # Separated handles that could be used for internal comunication.
221 219 my ($channel_in, $channel_out);
222  
223  
224 219 ▶ if ($self->set_pipes) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->set_pipes) 169 50 219
if ($self->set_pipes) 5 0 5
225 169 ▶ $input_pipe = IO::Pipe->new()
Branch: 2 branches
BranchTrueFalseTotal
$input_pipe = "IO::Pipe"->new or $self->_new_err("Failed creating input pipe") 167 2 169
$input_pipe = "IO::Pipe"->new or $self->_new_err("Failed creating input pipe") 5 0 5
226 or $self->_new_err('Failed creating input pipe');
227 169 ▶ $output_pipe = IO::Pipe->new()
Branch: 2 branches
BranchTrueFalseTotal
$output_pipe = "IO::Pipe"->new or $self->_new_err("Failed creating output pipe") 166 3 169
$output_pipe = "IO::Pipe"->new or $self->_new_err("Failed creating output pipe") 5 0 5
228 or $self->_new_err('Failed creating output pipe');
229 169 ▶ $output_err_pipe = IO::Pipe->new()
Branch: 2 branches
BranchTrueFalseTotal
$output_err_pipe = "IO::Pipe"->new or $self->_new_err("Failed creating output error pipe") 166 3 169
$output_err_pipe = "IO::Pipe"->new or $self->_new_err("Failed creating output error pipe") 5 0 5
230 or $self->_new_err('Failed creating output error pipe');
231 169 ▶ if ($self->channels) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->channels) 168 1 169
if ($self->channels) 5 0 5
232 169 ▶ $channel_in = IO::Pipe->new()
Branch: 2 branches
BranchTrueFalseTotal
$channel_in = "IO::Pipe"->new or $self->_new_err("Failed creating Channel input pipe") 166 3 169
$channel_in = "IO::Pipe"->new or $self->_new_err("Failed creating Channel input pipe") 5 0 5
233 or $self->_new_err('Failed creating Channel input pipe');
234 169 ▶ $channel_out = IO::Pipe->new()
Branch: 1 branch
BranchTrueFalseTotal
$channel_out = "IO::Pipe"->new or $self->_new_err("Failed creating Channel output pipe") 166 3 169
235 or $self->_new_err('Failed creating Channel output pipe');
236 }
237 }
238 219 ▶ if ($self->internal_pipes) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->internal_pipes) 215 4 219
239 215 ▶ my $internal_err = IO::Pipe->new()
Branch: 1 branch
BranchTrueFalseTotal
my $internal_err = "IO::Pipe"->new or $self->_new_err("Failed creating internal error pipe") 212 3 215
240 or $self->_new_err('Failed creating internal error pipe');
241 214 ▶ my $internal_return = IO::Pipe->new()
Branch: 1 branch
BranchTrueFalseTotal
my $internal_return = "IO::Pipe"->new or $self->_new_err("Failed creating internal return pipe") 212 3 215
242 or $self->_new_err('Failed creating internal return pipe');
243  
244 # Internal pipes to retrieve error/return
245 215 $self->_internal_err($internal_err);
246 215 $self->_internal_return($internal_return);
247 }
248  
249 # Defered collect of return status
250  
251 219 $self->on(collect_status => \&_fork_collect_status);
252  
253 219 $self->_diag("Fork: " . $self->_deparse->coderef2text($code)) if DEBUG;
254  
255 219 my $pid = fork;
256 218 ▶ die "Cannot fork: $!" unless defined $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid 0 219 219
257  
258 218 ▶ if ($pid == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid == 0) 20 203 223
259 21 local $SIG{CHLD};
260 21 ▶ local $SIG{TERM} = sub { $self->emit('SIG_TERM')->_exit(1) };
Subroutine __ANON__: called CC=1 SCAR=0.0
261  
262 21 my $return;
263 my $internal_err;
264  
265 25 ▶ if ($self->internal_pipes) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->internal_pipes) 25 0 25
266 25 ▶ if ($self->_internal_err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_internal_err) 24 1 25
267 24 ▶ $internal_err
Branch: 1 branch
BranchTrueFalseTotal
$self->_internal_err->isa('IO::Pipe::End') ? : 0 24 24
268 = $self->_internal_err->isa("IO::Pipe::End")
269 ? $self->_internal_err
270 : $self->_internal_err->writer();
271 24 $internal_err->autoflush(1);
272 }
273  
274 25 ▶ if ($self->_internal_return) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_internal_return) { } 19 6 25
275 24 ▶ $return
Branch: 1 branch
BranchTrueFalseTotal
$self->_internal_return->isa('IO::Pipe::End') ? : 1 23 24
276 = $self->_internal_return->isa("IO::Pipe::End")
277 ? $self->_internal_return
278 : $self->_internal_return->writer();
279 24 $return->autoflush(1);
280 }
281 else {
282 6 eval { $internal_err->write("Can't setup return status pipe") };
283 }
284 }
285  
286 # Set pipes to redirect STDIN/STDOUT/STDERR + channels if desired
287 25 ▶ if ($self->set_pipes()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->set_pipes) 19 6 25
288 24 my $stdout;
289 my $stderr;
290 24 my $stdin;
291  
292 24 ▶ $stdout = $output_pipe->writer() if $output_pipe;
Branch: 1 branch
BranchTrueFalseTotal
if $output_pipe 19 5 24
293 19 ▶ $stderr
Branch: 2 branches
BranchTrueFalseTotal
!$self->separate_err ? : 6 18 24
$output_err_pipe ? : 17 6 23
294 = (!$self->separate_err) ? $stdout
295 : $output_err_pipe ? $output_err_pipe->writer()
296 : undef;
297 24 ▶ $stdin = $input_pipe->reader() if $input_pipe;
Branch: 1 branch
BranchTrueFalseTotal
if $input_pipe 18 6 24
298 24 ▶ open STDERR, ">&", $stderr
Branch: 1 branch
BranchTrueFalseTotal
if $stderr 18 6 24
Condition44%4/9
$stderr && (open(STDERR, '>&', $stderr) || !!$internal_err->write($!) || $self->_diag($!))
!ll&&!rl&&r
2180
open STDERR, ">&", $stderr or not !$internal_err->write($!) or $self->_diag($!)
l!l&&r!l&&!r
1900
open STDERR, ">&", $stderr or not !$internal_err->write($!)
l!l&&r!l&&!r
1800
MC/DC0%0/4
$stderr && (open(STDERR, '>&', $stderr) || !!$internal_err->write($!) || $self->_diag($!))
$stderr open STDERR, ">&", $stderr not !$internal_err->write($!) $self->_diag($!)
Truth table0%0/5
A && B or C or D
A= $stderrB= open STDERR, ">&", $stderrC= not !$internal_err->write($!)D= $self->_diag($!)
ABCDresult
0XXX0
10000
11XX1
101X1
10011
299 or !!$internal_err->write($!)
300 or $self->_diag($!)
301 if $stderr;
302 24 ▶ open STDOUT, ">&", $stdout
Branch: 1 branch
BranchTrueFalseTotal
if $stdout 18 1 19
Condition44%4/9
$stdout && (open(STDOUT, '>&', $stdout) || !!$internal_err->write($!) || $self->_diag($!))
!ll&&!rl&&r
2180
open STDOUT, ">&", $stdout or not !$internal_err->write($!) or $self->_diag($!)
l!l&&r!l&&!r
1800
open STDOUT, ">&", $stdout or not !$internal_err->write($!)
l!l&&r!l&&!r
2300
MC/DC0%0/4
$stdout && (open(STDOUT, '>&', $stdout) || !!$internal_err->write($!) || $self->_diag($!))
$stdout open STDOUT, ">&", $stdout not !$internal_err->write($!) $self->_diag($!)
Truth table0%0/5
A && B or C or D
A= $stdoutB= open STDOUT, ">&", $stdoutC= not !$internal_err->write($!)D= $self->_diag($!)
ABCDresult
0XXX0
10000
11XX1
101X1
10011
303 or !!$internal_err->write($!)
304 or $self->_diag($!)
305 if $stdout;
306 24 ▶ open STDIN, ">&", $stdin
Branch: 1 branch
BranchTrueFalseTotal
if $stdin 18 1 19
Condition44%4/9
$stdin && (open(STDIN, '>&', $stdin) || !!$internal_err->write($!) || $self->_diag($!))
!ll&&!rl&&r
6180
open STDIN, ">&", $stdin or not !$internal_err->write($!) or $self->_diag($!)
l!l&&r!l&&!r
1800
open STDIN, ">&", $stdin or not !$internal_err->write($!)
l!l&&r!l&&!r
2300
MC/DC0%0/4
$stdin && (open(STDIN, '>&', $stdin) || !!$internal_err->write($!) || $self->_diag($!))
$stdin open STDIN, ">&", $stdin not !$internal_err->write($!) $self->_diag($!)
Truth table0%0/5
A && B or C or D
A= $stdinB= open STDIN, ">&", $stdinC= not !$internal_err->write($!)D= $self->_diag($!)
ABCDresult
0XXX0
10000
11XX1
101X1
10011
307 or !!$internal_err->write($!)
308 or $self->_diag($!)
309 if $stdin;
310  
311 24 $self->read_stream($stdin);
312 19 $self->error_stream($stderr);
313 19 $self->write_stream($stdout);
314 24 ▶ if ($self->channels) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->channels) 19 0 19
315  
316 24 ▶ $self->channel_in($channel_in->reader) if $channel_in;
Branch: 1 branch
BranchTrueFalseTotal
if $channel_in 18 1 19
317 24 ▶ $self->channel_out($channel_out->writer) if $channel_out;
Branch: 1 branch
BranchTrueFalseTotal
if $channel_out 18 1 19
318 38 eval { $self->$_->autoflush($self->autoflush) }
319 19 for qw( channel_in channel_out );
320 }
321 57 eval { $self->$_->autoflush($self->autoflush) }
322 19 for qw(read_stream error_stream write_stream );
323 }
324 20 $self->session->reset;
325 20 $self->session->subreaper(0); # Subreaper bit does not persist in fork
326 20 $self->process_id($$);
327 20 $! = 0;
328 20 my $rt;
329 20 eval { $rt = [$code->($self, @args)]; };
330 0 ▶ if ($internal_err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($internal_err) 0 0 0
331 0 ▶ $internal_err->write($@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
332 0 ▶ $internal_err->write($!) if !$@ && $!;
Branch: 1 branch
BranchTrueFalseTotal
if not $@ and $! 0 0 0
Condition0%0/3
not $@ and $!
!ll&&!rl&&r
000
MC/DC0%0/2
not $@ and $!
not $@ $!
Truth table0%0/3
not(A) and B
A= not $@B= $!
ABresult
0X0
100
111
333 }
334 0 ▶ $rt = @$rt[0]
Branch: 1 branch
BranchTrueFalseTotal
if not $self->serialize and ref $rt eq "ARRAY" and scalar @$rt == 1 0 0 0
Condition0%0/6
not $self->serialize and ref $rt eq "ARRAY" and scalar @$rt == 1
!ll&&!rl&&r
000
not $self->serialize and ref $rt eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/3
not $self->serialize and ref $rt eq "ARRAY" and scalar @$rt == 1
not $self->serialize ref $rt eq "ARRAY" scalar @$rt == 1
Truth table0%0/4
not(A) and B and C
A= not $self->serializeB= ref $rt eq "ARRAY"C= scalar @$rt == 1
ABCresult
0XX0
10X0
1100
1111
335 if !$self->serialize && ref $rt eq 'ARRAY' && scalar @$rt == 1;
336 0 ▶ $rt = b64_encode(eval { $self->_serialize->($rt) })
Branch: 1 branch
BranchTrueFalseTotal
if $self->serialize and $return 0 0 0
Condition0%0/3
$self->serialize and $return
!ll&&!rl&&r
000
MC/DC0%0/2
$self->serialize and $return
$self->serialize $return
Truth table0%0/3
A and B
A= $self->serializeB= $return
ABresult
0X0
100
111
337 if $self->serialize && $return;
338 0 ▶ $return->write($rt) if $return;
Branch: 1 branch
BranchTrueFalseTotal
if $return 0 0 0
339 0 ▶ $self->_exit($@ // $!);
Condition0%0/3
$@ // $!
l!l&&r!l&&!r
000
MC/DC0%0/2
$@ // $!
$@ $!
Truth table0%0/3
A // B
A= $@B= $!
ABresult
1X1
011
000
340 }
341 198 $self->process_id($pid);
342  
343 198 ▶ return $self unless $self->set_pipes();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->set_pipes 49 149 198
344  
345 149 ▶ $self->read_stream($output_pipe->reader) if $output_pipe;
Branch: 1 branch
BranchTrueFalseTotal
if $output_pipe 148 1 149
346 149 ▶ $self->error_stream((!$self->separate_err) ? $self->read_stream()
Branch: 2 branches
BranchTrueFalseTotal
!$self->separate_err ? : 10 139 149
$output_err_pipe ? : 138 1 139
347 : $output_err_pipe ? $output_err_pipe->reader()
348 : undef);
349 149 ▶ $self->write_stream($input_pipe->writer) if $input_pipe;
Branch: 1 branch
BranchTrueFalseTotal
if $input_pipe 148 1 149
350  
351 149 ▶ if ($self->set_pipes) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->set_pipes) 149 0 149
352 149 ▶ if ($self->channels) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->channels) 149 0 149
353 149 ▶ $self->channel_in($channel_in->writer) if $channel_in;
Branch: 1 branch
BranchTrueFalseTotal
if $channel_in 148 1 149
354 149 ▶ $self->channel_out($channel_out->reader) if $channel_out;
Branch: 1 branch
BranchTrueFalseTotal
if $channel_out 148 1 149
355 298 eval { $self->$_->autoflush($self->autoflush) }
356 149 for qw( channel_in channel_out );
357 }
358 447 eval { $self->$_->autoflush($self->autoflush) }
359 149 for qw(read_stream error_stream write_stream );
360 }
361  
362 149 return $self;
363 }
364  
365 30 ▶sub _new_err {
Subroutine _new_err: called CC=1 SCAR=0.0
366 26 my $self = shift;
367 26 my $err = Mojo::IOLoop::ReadWriteProcess::Exception->new(@_);
368 26 push(@{$self->error}, $err);
369  
370 # XXX: Need to switch, we should emit one error at the time, and _shutdown
371 # should emit just the ones wasn't emitted
372 26 return $self->emit(process_error => [$err]);
373 }
374  
375 0 ▶sub _exit {
Subroutine _exit: not called CC=2 SCAR=8.4
376 1 ▶ my $code = shift // 0;
Condition0%0/3
shift() // 0
l!l
000
MC/DC0%0/1
shift() // 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
377 1 eval { POSIX::_exit($code); };
378 1 exit($code);
379 }
380  
381 115 ▶sub wait {
Subroutine wait: called CC=2 SCAR=6.9
Pod documented
382 116 my $self = shift;
383 116 ▶ sleep $self->sleeptime_during_kill while ($self->is_running);
Branch: 1 branch
BranchTrueFalseTotal
$self->is_running and do { &Mojo::IOLoop::ReadWriteProcess::sleep(scalar $self->sleeptime_during_kill) } 169 116 285
384 116 return $self;
385 }
386  
387 99 ▶sub wait_stop { shift->wait->stop }
Subroutine wait_stop: called CC=1 SCAR=0.0
Pod documented
388 27 ▶sub errored { !!@{shift->error} ? 1 : 0 }
Subroutine errored: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
!!@{(shift())->error;} ? : 18 10 28
389  
390 # PPC64: Treat msb on neg (different cpu/perl interpreter version)
391 4 ▶sub _st { my $st = shift >> 8; ($st & 0x80) ? (0x100 - ($st & 0xFF)) : $st }
Subroutine _st: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$st & 128 ? : 1 3 4
392  
393 37 ▶sub exit_status {
Subroutine exit_status: called CC=4 SCAR=14.5
Pod documented
394 38 ▶ defined $_[0]->_status && $_[0]->quirkiness ? _st(shift->_status)
Branch: 2 branches
BranchTrueFalseTotal
defined $_[0]->_status && $_[0]->quirkiness ? : 4 34 38
defined $_[0]->_status ? : 35 0 35
Condition66%2/3
defined $_[0]->_status and $_[0]->quirkiness
!ll&&!rl&&r
0343
MC/DC50%1/2
defined $_[0]->_status and $_[0]->quirkiness
defined $_[0]->_status $_[0]->quirkiness
Truth table66%2/3
A and B
A= defined $_[0]->_statusB= $_[0]->quirkiness
ABresult
0X0
100
111
395 : defined $_[0]->_status ? shift->_status >> 8
396 : undef;
397 }
398  
399 20 ▶sub restart {
Subroutine restart: called CC=2 SCAR=6.9
Pod documented
400 21 ▶ $_[0]->is_running ? $_[0]->stop->start : $_[0]->start;
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->is_running ? : 7 14 21
401 }
402  
403 1443 ▶sub is_running {
Subroutine is_running: called CC=3 SCAR=11.0
Pod documented
404 1444 my ($self) = shift;
405 1428 $self->session->consume_collected_info;
406 1428 ▶ return 0 unless my $pid = $self->process_id;
Branch: 1 branch
BranchTrueFalseTotal
unless my $pid = $self->process_id 243 1187 1430
407 1187 ▶ kill(0, ($self->kill_whole_group ? (-$pid, $pid) : ($pid)));
Branch: 1 branch
BranchTrueFalseTotal
$self->kill_whole_group ? : 17 1186 1203
408 }
409  
410 252 ▶sub write_pidfile {
Subroutine write_pidfile: called CC=4 SCAR=13.9
Pod documented
411 252 my ($self, $pidfile) = @_;
412 252 ▶ $self->pidfile($pidfile) if $pidfile;
Branch: 1 branch
BranchTrueFalseTotal
if $pidfile 18 250 268
413 252 ▶ return unless $self->pid;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->pid 3 249 252
414 249 ▶ return unless $self->pidfile;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->pidfile 251 32 283
415  
416 42 path($self->pidfile)->spew($self->pid);
417 42 return $self;
418 }
419  
420 # Convenience functions
421 54 ▶sub _syswrite {
Subroutine _syswrite: called CC=3 SCAR=11.2
422 54 my $stream = shift;
423 44 ▶ return unless $stream;
Branch: 1 branch
BranchTrueFalseTotal
unless $stream 0 45 45
424 18 $stream->syswrite($_ . "\n") for @_;
425 }
426  
427 85 ▶sub _getline {
Subroutine _getline: called CC=2 SCAR=6.9
428 77 ▶ return unless IO::Select->new($_[0])->can_read(10);
Branch: 1 branch
BranchTrueFalseTotal
unless "IO::Select"->new($_[0])->can_read(10) 7 71 78
429 69 shift->getline;
430 }
431  
432 7 ▶sub _getlines {
Subroutine _getlines: called CC=3 SCAR=11.0
433 7 ▶ return unless IO::Select->new($_[0])->can_read(10);
Branch: 1 branch
BranchTrueFalseTotal
unless "IO::Select"->new($_[0])->can_read(10) 1 7 8
434 16 ▶ wantarray ? shift->getlines : join '', @{[shift->getlines]};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 14 16
435 }
436  
437 # Write to the controlled-process STDIN
438 21 ▶sub write_stdin {
Subroutine write_stdin: called CC=1 SCAR=0.0
Pod documented
439 30 my ($self, @data) = @_;
440 30 _syswrite($self->write_stream, @data);
441 21 return $self;
442 }
443  
444 2 ▶sub write_channel {
Subroutine write_channel: called CC=1 SCAR=0.0
Pod documented
445 2 my ($self, @data) = @_;
446 2 _syswrite($self->channel_in, @data);
447 2 return $self;
448 }
449  
450 # Get all lines from the current process output stream
451 6 ▶sub read_all_stdout { _getlines(shift->read_stream) }
Subroutine read_all_stdout: called CC=1 SCAR=0.0
Pod documented
452  
453 # Get all lines from the process channel
454 6 ▶sub read_all_channel { _getlines(shift->channel_out); }
Subroutine read_all_channel: called CC=1 SCAR=0.0
Pod documented
455 60 ▶sub read_stdout { _getline(shift->read_stream) }
Subroutine read_stdout: called CC=1 SCAR=0.0
Pod documented
456 2 ▶sub read_channel { _getline(shift->channel_out) }
Subroutine read_channel: called CC=1 SCAR=0.0
Pod documented
457  
458 2 ▶sub read_all_stderr {
Subroutine read_all_stderr: called CC=2 SCAR=6.9
Pod documented
459 2 ▶ return $_[0]->getline unless $_[0]->separate_err;
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0]->separate_err 1 16 17
460 1 _getlines(shift->error_stream);
461 }
462  
463 # Get a line from the current process output stream
464 14 ▶sub read_stderr {
Subroutine read_stderr: called CC=2 SCAR=6.9
Pod documented
465 14 ▶ return $_[0]->getline unless $_[0]->separate_err;
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0]->separate_err 9 20 29
466 14 _getline(shift->error_stream);
467 }
468  
469 263 ▶sub start {
Subroutine start: called CC=9 SCAR=22.0
Pod documented
470 263 my $self = shift;
471 263 ▶ return $self if $self->is_running;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_running 0 263 263
472 263 ▶ die "Nothing to do" unless !!$self->execute || !!$self->code;
Branch: 1 branch
BranchTrueFalseTotal
if not $self->execute and not $self->code 3 260 263
Condition100%3/3
not $self->execute and not $self->code
!ll&&!rl&&r
422193
MC/DC100%2/2
not $self->execute and not $self->code
not $self->execute not $self->code
Truth table100%3/3
not(A) and not(B)
A= not $self->executeB= not $self->code
ABresult
0X0
100
111
473  
474 my @args
475 = defined($self->args)
476 ? ref($self->args) eq "ARRAY"
477 260 ▶ ? @{$self->args}
Branch: 2 branches
BranchTrueFalseTotal
defined $self->args ? : 52 208 260
ref $self->args eq 'ARRAY' ? : 6 46 52
478 : $self->args
479 : ();
480  
481 260 ▶ $self->session->enable_subreaper if $self->subreaper;
Branch: 1 branch
BranchTrueFalseTotal
if $self->subreaper 45 215 260
482 260 $self->_status(undef);
483 260 $self->session->enable;
484  
485 {
486 260 my $old_emit_from_sigchld = $self->session->emit_from_sigchld;
487 260 $self->session->emit_from_sigchld(0);
488 250 ▶ my $scope_guard = scope_guard sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
489 251 $self->session->emit_from_sigchld($old_emit_from_sigchld);
490 251 ▶ $self->session->consume_collected_info if ($old_emit_from_sigchld);
Branch: 1 branch
BranchTrueFalseTotal
if $old_emit_from_sigchld 248 2 250
491 260 };
492  
493 260 ▶ if ($self->code) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->code) { } 218 42 260
elsif ($self->execute) { } 42 0 42
494 218 $self->_fork($self->code, @args);
495 }
496 elsif ($self->execute) {
497 42 $self->_open($self->execute, @args);
498 }
499  
500 241 $self->write_pidfile;
501 241 $self->emit('start');
502 241 $self->session->register($self->pid() => $self);
503 }
504 241 return $self;
505 }
506  
507 261 ▶sub send_signal {
Subroutine send_signal: called CC=5 SCAR=16.2
Pod documented
508 253 my $self = shift;
509 253 ▶ my $signal = shift // $self->_default_kill_signal;
Condition66%2/3
shift() // $self->_default_kill_signal
l!l&&r!l&&!r
243140
MC/DC0%0/2
shift() // $self->_default_kill_signal
shift() $self->_default_kill_signal
Truth table33%1/3
A // B
A= shift()B= $self->_default_kill_signal
ABresult
1X1
011
000
510 242 ▶ my $pid = shift // $self->process_id;
Condition66%2/3
shift() // $self->process_id
l!l&&r!l&&!r
24380
MC/DC0%0/2
shift() // $self->process_id
shift() $self->process_id
Truth table66%2/3
A // B
A= shift()B= $self->process_id
ABresult
1X1
011
000
511 243 ▶ return unless $self->kill_whole_group || $self->is_running;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->kill_whole_group or $self->is_running 38 214 252
Condition100%3/3
$self->kill_whole_group or $self->is_running
l!l&&r!l&&!r
221128
MC/DC100%2/2
$self->kill_whole_group or $self->is_running
$self->kill_whole_group $self->is_running
Truth table100%3/3
A or B
A= $self->kill_whole_groupB= $self->is_running
ABresult
1X1
011
000
512 216 $self->_diag("Sending signal '$signal' to $pid") if DEBUG;
513 215 kill $signal => $pid;
514 217 return $self;
515 }
516  
517 232 ▶sub stop {
Subroutine stop: called CC=17 SCAR=28.4
Pod documented
518 234 my $self = shift;
519  
520 238 my $pid = $self->pid;
521 238 ▶ return $self unless defined $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid 3 230 233
522 236 ▶ return $self->_shutdown(1) unless $self->is_running;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_running 179 51 230
523  
524 57 my $ret;
525 56 my $attempt = 1;
526 57 ▶ my $timeout = $self->total_sleeptime_during_kill // 0;
Condition33%1/3
$self->total_sleeptime_during_kill // 0
l!l
5200
MC/DC0%0/1
$self->total_sleeptime_during_kill // 0
$self->total_sleeptime_during_kill
Truth table50%1/2
A
A= $self->total_sleeptime_during_kill
Aresult
11
00
527 58 my $sleep_time = $self->sleeptime_during_kill;
528 58 my $max_attempts = $self->max_kill_attempts;
529 58 my $signal = $self->_default_kill_signal;
530 67 ▶ $pid = -$pid if $self->kill_whole_group;
Branch: 1 branch
BranchTrueFalseTotal
if $self->kill_whole_group 1 50 51
531 66 $self->_diag("Stopping $pid") if DEBUG;
532  
533 66 ▶ until ((defined $ret && ($ret == $pid || $ret == -1))
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ret and $ret == $pid || $ret == -1 or $attempt > $max_attempts and $timeout <= 0) 227 50 277
Condition100%12/12
defined $ret and $ret == $pid || $ret == -1 or $attempt > $max_attempts and $timeout <= 0
l!l&&r!l&&!r
3022226
defined $ret and $ret == $pid || $ret == -1
!ll&&!rl&&r
5019929
$ret == $pid or $ret == -1
l!l&&r!l&&!r
228198
$attempt > $max_attempts and $timeout <= 0
!ll&&!rl&&r
224422
MC/DC80%4/5
defined $ret and $ret == $pid || $ret == -1 or $attempt > $max_attempts and $timeout <= 0
defined $ret $ret == $pid $ret == -1 $attempt > $max_attempts $timeout <= 0
Truth table75%6/8
A and B or C or D and E
A= defined $retB= $ret == $pidC= $ret == -1D= $attempt > $max_attemptsE= $timeout <= 0
ABCDEresult
11XXX1
101XX1
0XX111
100111
0XX0X0
0XX100
1000X0
100100
534 || ($attempt > $max_attempts && $timeout <= 0))
535 {
536 242 ▶ my $send_signal = $attempt == 1 || $timeout <= 0;
Condition100%3/3
$attempt == 1 || $timeout <= 0
l!l&&r!l&&!r
351885
MC/DC100%2/2
$attempt == 1 || $timeout <= 0
$attempt == 1 $timeout <= 0
Truth table100%3/3
A || B
A= $attempt == 1B= $timeout <= 0
ABresult
1X1
011
000
537 235 $self->_diag(
538 "attempt $attempt/$max_attempts to kill process: $pid, timeout: $timeout")
539 if DEBUG && $send_signal;
540 $self->session->_protect(
541 229 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
542 227 local $?;
543 227 ▶ if ($send_signal) {
Branch: 1 branch
BranchTrueFalseTotal
if ($send_signal) 222 16 238
544 223 $self->send_signal($signal, $pid);
545 223 ++$attempt;
546 }
547 227 $ret = waitpid($pid, WNOHANG);
548 227 ▶ $self->_status($?) if $ret == $pid || $ret == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $ret == $pid or $ret == -1 34 204 238
Condition100%3/3
$ret == $pid or $ret == -1
l!l&&r!l&&!r
128198
MC/DC50%1/2
$ret == $pid or $ret == -1
$ret == $pid $ret == -1
Truth table66%2/3
A or B
A= $ret == $pidB= $ret == -1
ABresult
1X1
011
000
549 241 });
550 241 ▶ if ($sleep_time) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sleep_time) 226 0 226
551 241 sleep $sleep_time;
552 241 $timeout -= $sleep_time;
553 }
554 }
555 65 ▶ return $self->_shutdown if defined $self->_status;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->_status 28 32 60
556  
557 37 ▶ sleep $self->kill_sleeptime if $self->kill_sleeptime;
Branch: 1 branch
BranchTrueFalseTotal
if $self->kill_sleeptime 22 10 32
558  
559 37 ▶ if ($self->blocking_stop) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->blocking_stop) { } 22 10 32
560 26 $self->_diag("Could not kill process id: $pid, blocking attempt") if DEBUG;
561 21 $self->emit('process_stuck');
562  
563 ### XXX: avoid to protect on blocking.
564 21 $self->send_signal($self->_default_blocking_signal, $pid);
565 25 $ret = waitpid($pid, 0);
566 25 ▶ $self->_status($?) if $ret == $pid || $ret == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $ret == $pid or $ret == -1 19 3 22
Condition66%2/3
$ret == $pid or $ret == -1
l!l&&r!l&&!r
1501
MC/DC50%1/2
$ret == $pid or $ret == -1
$ret == $pid $ret == -1
Truth table66%2/3
A or B
A= $ret == $pidB= $ret == -1
ABresult
1X1
011
000
567 25 return $self->_shutdown;
568 }
569 else {
570 15 $self->_diag("Could not kill process id: $pid") if DEBUG;
571 6 $self->_new_err('Could not kill process');
572 }
573 6 return $self;
574 }
575  
576 232 ▶sub _shutdown {
Subroutine _shutdown: called CC=7 SCAR=19.5
577 225 my ($self, $wait) = @_;
578 225 ▶ return $self unless $self->pid;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->pid 7 224 231
579  
580 225 $self->_diag("Shutdown " . $self->pid) if DEBUG;
581 $self->session->_protect(
582 56 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
583 58 local $?;
584 58 waitpid $self->pid, 0;
585 58 $self->emit('collect_status');
586 225 ▶ }) if $wait && !defined $self->_status;
Branch: 1 branch
BranchTrueFalseTotal
if $wait and not defined $self->_status 59 169 228
Condition100%3/3
$wait and not defined $self->_status
!ll&&!rl&&r
4512458
MC/DC100%2/2
$wait and not defined $self->_status
$wait not defined $self->_status
Truth table100%3/3
A and not(B)
A= $waitB= not defined $self->_status
ABresult
0X0
100
111
587  
588 225 ▶ $self->emit('collect_status') unless defined $self->_status;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->_status 59 166 225
589 225 $self->_clean_pidfile;
590 223 ▶ $self->emit('process_error', $self->error)
Branch: 1 branch
BranchTrueFalseTotal
if $self->error and $self->error->size > 0 78 147 225
Condition100%3/3
$self->error and $self->error->size > 0
!ll&&!rl&&r
114876
MC/DC50%1/2
$self->error and $self->error->size > 0
$self->error $self->error->size > 0
Truth table66%2/3
A and B
A= $self->errorB= $self->error->size > 0
ABresult
0X0
100
111
591 if $self->error && $self->error->size > 0;
592 224 $self->unsubscribe('collect_status');
593  
594 224 return $self->emit('stop');
595 }
596  
597 # General alias
598 47 *pid = \&process_id;
599 47 *died = \&_errored;
600 40 *failed = \&_errored;
601 40 *diag = \&_diag;
602 45 *pool = \&batch;
603 45 *signal = \&send_signal;
604 45 *prctl = \&Mojo::IOLoop::ReadWriteProcess::Session::_prctl;
605 45 *subreaper = \&Mojo::IOLoop::ReadWriteProcess::Session::subreaper;
606  
607 45 *enable_subreaper = \&Mojo::IOLoop::ReadWriteProcess::Session::enable_subreaper;
608 45 *disable_subreaper
609 = \&Mojo::IOLoop::ReadWriteProcess::Session::disable_subreaper;
610 45 *_get_prctl_syscall
611 = \&Mojo::IOLoop::ReadWriteProcess::Session::_get_prctl_syscall;
612  
613 # Aliases - write
614 46 *write = \&write_stdin;
615 46 *stdin = \&write_stdin;
616 46 *channel_write = \&write_channel;
617  
618 # Aliases - read
619 51 *read = \&read_stdout;
620 46 *stdout = \&read_stdout;
621 56 *getline = \&read_stdout;
622 45 *stderr = \&read_stderr;
623 39 *err_getline = \&read_stderr;
624 39 *channel_read = \&read_channel;
625 39 *read_all = \&read_all_stdout;
626 39 *getlines = \&read_all_stdout;
627 39 *stderr_all = \&read_all_stderr;
628 39 *err_getlines = \&read_all_stderr;
629 39 *channel_read_all = \&read_all_channel;
630  
631 # Aliases - IO::Handle
632 39 *stdin_handle = \&write_stream;
633 39 *stdout_handle = \&read_stream;
634 39 *stderr_handle = \&error_stream;
635 39 *channe_write_handle = \&channel_in;
636 39 *channel_read_handle = \&channel_out;
637  
638 39 1;
639  
640  
641 =encoding utf-8
642  
643 =head1 NAME
644  
645 Mojo::IOLoop::ReadWriteProcess - Execute external programs or internal code blocks as separate process.
646  
647 =head1 SYNOPSIS
648  
649 use Mojo::IOLoop::ReadWriteProcess;
650  
651 # Code fork
652 my $process = Mojo::IOLoop::ReadWriteProcess->new(sub { print "Hello\n" });
653 $process->start();
654 print "Running\n" if $process->is_running();
655 $process->getline(); # Will return "Hello\n"
656 $process->pid(); # Process id
657 $process->stop();
658 $process->wait_stop(); # if you intend to wait its lifespan
659  
660 # Methods can be chained, thus this is valid:
661 use Mojo::IOLoop::ReadWriteProcess qw(process);
662 my $output = process( sub { print "Hello\n" } )->start()->wait_stop->getline;
663  
664 # Handles seamelessy also external processes:
665 my $process = process(execute=> '/path/to/bin' )->args([qw(foo bar baz)]);
666 $process->start();
667 my $line_output = $process->getline();
668 my $pid = $process->pid();
669 $process->stop();
670 my @errors = $process->error;
671  
672 # To help when debugging Mojo::Collections
673 use Mojo::Util qw(dumper);
674 my $errors = dumper $process->error->to_array;
675  
676 # Get process return value
677 $process = process( sub { return "256"; } )->start()->wait_stop;
678 # We need to stop it to retrieve the exit status
679 my $return = $process->return_status;
680  
681 # We can access directly to handlers from the object:
682 my $stdout = $process->read_stream;
683 my $stdin = $process->write_stream;
684 my $stderr = $process->error_stream;
685  
686 # So this works:
687 print $stdin "foo bar\n";
688 my @lines = <$stdout>;
689  
690 # There is also an alternative channel of communication (just for forked processes):
691 my $channel_in = $process->channel_in; # write to the child process
692 my $channel_out = $process->channel_out; # read from the child process
693 $process->channel_write("PING"); # convenience function
694  
695 =head1 DESCRIPTION
696  
697 Mojo::IOLoop::ReadWriteProcess is yet another process manager.
698  
699 =head1 EVENTS
700  
701 L<Mojo::IOLoop::ReadWriteProcess> inherits all events from L<Mojo::EventEmitter> and can emit
702 the following new ones.
703  
704 =head2 start
705  
706 $process->on(start => sub {
707 my ($process) = @_;
708 $process->is_running();
709 });
710  
711 Emitted when the process starts.
712  
713 =head2 stop
714  
715 $process->on(stop => sub {
716 my ($process) = @_;
717 $process->restart();
718 });
719  
720 Emitted when the process stops.
721  
722 =head2 process_error
723  
724 $process->on(process_error => sub {
725 my ($e) = @_;
726 my @errors = @{$e};
727 });
728  
729 Emitted when the process produce errors.
730  
731 =head2 process_stuck
732  
733 $process->on(process_stuck => sub {
734 my ($self) = @_;
735 ...
736 });
737  
738 Emitted when C<blocking_stop> is set and all attempts for killing the process
739 in C<max_kill_attempts> have been exhausted.
740 The event is emitted before attempting to kill it with SIGKILL and becoming blocking.
741  
742 =head2 SIG_CHLD
743  
744 $process->on(SIG_CHLD => sub {
745 my ($self) = @_;
746 ...
747 });
748  
749 Emitted when we receive SIG_CHLD.
750  
751 =head2 SIG_TERM
752  
753 $process->on(SIG_TERM => sub {
754 my ($self) = @_;
755 ...
756 });
757  
758 Emitted when the child forked process receives SIG_TERM, before exiting.
759  
760 =head2 collected
761  
762 $process->on(collected => sub {
763 my ($self) = @_;
764 ...
765 });
766  
767 Emitted right after status collection.
768  
769 =head2 collect_status
770  
771 $process->on(collect_status => sub {
772 my ($self) = @_;
773 ...
774 });
775  
776 Emitted when on child process waitpid.
777 It is used internally to get the child process status.
778 Note: events attached to it are wiped when process has been stopped.
779  
780 =head1 ATTRIBUTES
781  
782 L<Mojo::IOLoop::ReadWriteProcess> inherits all attributes from L<Mojo::EventEmitter> and implements
783 the following new ones.
784  
785 =head2 execute
786  
787 use Mojo::IOLoop::ReadWriteProcess;
788 my $process = Mojo::IOLoop::ReadWriteProcess->new(execute => "/usr/bin/perl");
789 $process->start();
790 $process->on( stop => sub { print "Process: ".(+shift()->pid)." finished"; } );
791 $process->stop();
792  
793 C<execute> should contain the external program that you wish to run.
794  
795 =head2 code
796  
797 use Mojo::IOLoop::ReadWriteProcess;
798 my $process = Mojo::IOLoop::ReadWriteProcess->new(code => sub { print "Hello" } );
799 $process->start();
800 $process->on( stop => sub { print "Process: ".(+shift()->pid)." finished"; } );
801 $process->stop();
802  
803 It represent the code you want to run in background.
804  
805 You do not need to specify C<code>, it is implied if no arguments is given.
806  
807 my $process = Mojo::IOLoop::ReadWriteProcess->new(sub { print "Hello" });
808 $process->start();
809 $process->on( stop => sub { print "Process: ".(+shift()->pid)." finished"; } );
810 $process->stop();
811  
812 =head2 args
813  
814 use Mojo::IOLoop::ReadWriteProcess;
815 my $process = Mojo::IOLoop::ReadWriteProcess->new(code => sub { print "Hello ".$_[1] }, args => "User" );
816 $process->start();
817 $process->on( stop => sub { print "Process: ".(+shift()->pid)." finished"; } );
818 $process->stop();
819  
820 # The process will print "Hello User"
821  
822 Arguments pass to the external binary or the code block. Use arrayref to pass many.
823  
824 =head2 blocking_stop
825  
826 use Mojo::IOLoop::ReadWriteProcess;
827 my $process = Mojo::IOLoop::ReadWriteProcess->new(code => sub { print "Hello" }, blocking_stop => 1 );
828 $process->start();
829 $process->on( stop => sub { print "Process: ".(+shift()->pid)." finished"; } );
830 $process->stop(); # Will wait indefinitely until the process is stopped
831  
832 Set it to 1 if you want to do blocking stop of the process.
833  
834  
835 =head2 channels
836  
837 use Mojo::IOLoop::ReadWriteProcess;
838 my $process = Mojo::IOLoop::ReadWriteProcess->new(code => sub { print "Hello" }, channels => 0 );
839 $process->start();
840 $process->on( stop => sub { print "Process: ".(+shift()->pid)." finished"; } );
841 $process->stop(); # Will wait indefinitely until the process is stopped
842  
843 Set it to 0 if you want to disable internal channels.
844  
845 =head2 session
846  
847 use Mojo::IOLoop::ReadWriteProcess;
848 my $process = Mojo::IOLoop::ReadWriteProcess->new(sub { print "Hello" });
849 my $session = $process->session;
850 $session->enable_subreaper;
851  
852 Returns the current L<Mojo::IOLoop::ReadWriteProcess::Session> singleton.
853  
854 =head2 subreaper
855  
856 use Mojo::IOLoop::ReadWriteProcess;
857 my $process = Mojo::IOLoop::ReadWriteProcess->new(code => sub { print "Hello ".$_[1] }, args => "User" );
858 $process->subreaper(1)->start();
859 $process->on( stop => sub { shift()->disable_subreaper } );
860 $process->stop();
861  
862 # The process will print "Hello User"
863  
864 Mark the current process (not the child) as subreaper on start.
865 It's on invoker behalf to disable subreaper when process stops, as it marks the current process and not the
866 child.
867  
868 =head2 ioloop
869  
870 my $loop = $process->ioloop;
871 $subprocess = $process->ioloop(Mojo::IOLoop->new);
872  
873 Event loop object to control, defaults to the global L<Mojo::IOLoop> singleton.
874  
875 =head2 max_kill_attempts
876  
877 use Mojo::IOLoop::ReadWriteProcess;
878 my $process = Mojo::IOLoop::ReadWriteProcess->new(code => sub { print "Hello" }, max_kill_attempts => 50 );
879 $process->start();
880 $process->on( stop => sub { print "Process: ".(+shift()->pid)." finished"; } );
881 $process->stop(); # It will attempt to send SIGTERM 50 times.
882  
883 Defaults to C<5>, is the number of attempts before bailing out.
884  
885 It can be used with blocking_stop, so if the number of attempts are exhausted,
886 a SIGKILL and waitpid will be tried at the end.
887  
888 =head2 kill_whole_group
889  
890 use Mojo::IOLoop::ReadWriteProcess;
891 my $process = Mojo::IOLoop::ReadWriteProcess->new(code => sub { setpgrp(0, 0); exec(...); }, kill_whole_group => 1 );
892 $process->start();
893 $process->send_signal(...); # Will skip the usual check whether $process->pid is running
894 $process->stop(); # Kills the entire process group and waits for all processes in the group to finish
895  
896 Defaults to C<0>, whether to send signals (e.g. to stop) to the entire process group.
897  
898 This is useful when the sub process creates further sub processes and creates a new process
899 group as shown in the example. In this case it might be useful to take care of the entire process
900 group when stopping and wait for every process in the group to finish.
901  
902 =head2 collect_status
903  
904 Defaults to C<1>, If enabled it will automatically collect the status of the children process.
905 Disable it in case you want to manage your process child directly, and do not want to rely on
906 automatic collect status. If you won't overwrite your C<SIGCHLD> handler,
907 the C<SIG_CHLD> event will be still emitted.
908  
909 =head2 serialize
910  
911 Defaults to C<0>, If enabled data returned from forked process will be serialized with Storable.
912  
913 =head2 kill_sleeptime
914  
915 Defaults to C<1>, it's the seconds to wait before attempting SIGKILL when blocking_stop is set to 1.
916  
917 =head2 separate_err
918  
919 Defaults to C<1>, it will create a separate channel to intercept process STDERR,
920 otherwise it will be redirected to STDOUT.
921  
922 =head2 verbose
923  
924 Defaults to C<1>, it indicates message verbosity.
925  
926 =head2 set_pipes
927  
928 Defaults to C<1>, If enabled, additional pipes for process communication are automatically set up.
929  
930  
931 =head2 internal_pipes
932  
933 Defaults to C<1>, If enabled, additional pipes for retreiving process return and errors are set up.
934 Note: If you disable that, the only information provided by the process will be the exit_status.
935  
936 =head2 autoflush
937  
938 Defaults to C<1>, If enabled autoflush of handlers is enabled automatically.
939  
940 =head2 error
941  
942 Returns a L<Mojo::Collection> of errors.
943 Note: errors that can be captured only at the end of the process
944  
945 =head1 METHODS
946  
947 L<Mojo::IOLoop::ReadWriteProcess> inherits all methods from L<Mojo::EventEmitter> and implements
948 the following new ones.
949  
950 =head2 start()
951  
952 use Mojo::IOLoop::ReadWriteProcess qw(process);
953 my $p = process(sub {
954 print STDERR "Boo\n"
955 } )->start;
956  
957 Starts the process
958  
959 =head2 stop()
960  
961 use Mojo::IOLoop::ReadWriteProcess qw(process);
962 my $p = process( execute => "/path/to/bin" )->start->stop;
963  
964 Stop the process. Unless you use C<wait_stop()>, it will attempt to kill the process
965 without waiting the process to finish. By defaults it send C<SIGTERM> to the child.
966 You can change that by defining the internal attribute C<_default_kill_signal>.
967 Note, if you want to be *sure* that the process gets killed, you can enable the
968 C<blocking_stop> attribute, that will attempt to send C<SIGKILL> after C<max_kill_attempts>
969 is reached.
970  
971 =head2 restart()
972  
973 use Mojo::IOLoop::ReadWriteProcess qw(process);
974 my $p = process( execute => "/path/to/bin" )->restart;
975  
976 It restarts the process if stopped, or if already running, it stops it first.
977  
978 =head2 is_running()
979  
980 use Mojo::IOLoop::ReadWriteProcess qw(process);
981 my $p = process( execute => "/path/to/bin" )->start;
982 $p->is_running;
983  
984 Boolean, it inspect if the process is currently running or not.
985  
986 =head2 exit_status()
987  
988 use Mojo::IOLoop::ReadWriteProcess qw(process);
989 my $p = process( execute => "/path/to/bin" )->start;
990  
991 $p->wait_stop->exit_status;
992  
993 Inspect the process exit status, it does the shifting magic, to access to the real value
994 call C<_status()>.
995  
996 =head2 return_status()
997  
998 use Mojo::IOLoop::ReadWriteProcess qw(process);
999 my $p = process( sub { return 42 } )->start;
1000  
1001 my $s = $p->wait_stop->return_status; # 42
1002  
1003 Inspect the codeblock return.
1004  
1005 =head2 enable_subreaper()
1006  
1007 use Mojo::IOLoop::ReadWriteProcess qw(process);
1008 my $p = process()->enable_subreaper;
1009  
1010 Mark the current process (not the child) as subreaper.
1011 This is used typically if you want to mark further children as subreapers inside other forks.
1012  
1013 my $master_p = process(
1014 sub {
1015 my $p = shift;
1016 $p->enable_subreaper;
1017  
1018 process(sub { sleep 4; exit 1 })->start();
1019 process(
1020 sub {
1021 sleep 4;
1022 process(sub { sleep 1; })->start();
1023 })->start();
1024 process(sub { sleep 4; exit 0 })->start();
1025 process(sub { sleep 4; die })->start();
1026 my $manager
1027 = process(sub { sleep 2 })->subreaper(1)->start();
1028 sleep 1 for (0 .. 10);
1029 $manager->stop;
1030 return $manager->session->all->size;
1031 });
1032  
1033 $master_p->subreaper(1);
1034  
1035 $master_p->on(collected => sub { $status++ });
1036  
1037 # On start we setup the current process as subreaper
1038 # So it's up on us to disable it after process is done.
1039 $master_p->on(stop => sub { shift()->disable_subreaper });
1040 $master_p->start();
1041  
1042 =head2 disable_subreaper()
1043  
1044 use Mojo::IOLoop::ReadWriteProcess qw(process);
1045 my $p = process()->disable_subreaper;
1046  
1047 Unset the current process (not the child) as subreaper.
1048  
1049 =head2 prctl()
1050  
1051 use Mojo::IOLoop::ReadWriteProcess qw(process);
1052 my $p = process();
1053 $p->prctl($option, $arg2, $arg3, $arg4, $arg5);
1054  
1055 Internal function to execute and wrap the prctl syscall, accepts the same arguments as prctl.
1056  
1057 =head2 diag()
1058  
1059 use Mojo::IOLoop::ReadWriteProcess qw(process);
1060 my $p = process(sub { print "Hello\n" });
1061 $p->on( stop => sub { shift->diag("Done!") } );
1062 $p->start->wait_stop;
1063  
1064 Internal function to print information to STDERR if verbose attribute is set or either DEBUG mode enabled.
1065 You can use it if you wish to display information on the process status.
1066  
1067 =head2 to_ioloop()
1068  
1069 use Mojo::IOLoop::ReadWriteProcess qw(process);
1070  
1071 my $p = process(sub { print "Hello from first process\n"; sleep 1 });
1072  
1073 $p->start(); # Start and sets the handlers
1074 my $stream = $p->to_ioloop; # Get the stream and demand to IOLoop
1075 my $output;
1076  
1077 # Hook on Mojo::IOLoop::Stream events
1078 $stream->on(read => sub { $output .= pop; $p->is_running ... });
1079  
1080 Mojo::IOLoop->singleton->start() unless Mojo::IOLoop->singleton->is_running;
1081  
1082 Returns a L<Mojo::IOLoop::Stream> object and demand the wait operation to L<Mojo::IOLoop>.
1083 It needs C<set_pipes> enabled. Default IOLoop can be overridden in C<ioloop()>.
1084  
1085 =head2 wait()
1086  
1087 use Mojo::IOLoop::ReadWriteProcess qw(process);
1088 my $p = process(sub { print "Hello\n" })->wait;
1089 # ... here now you can mangle $p handlers and such
1090  
1091 Waits until the process finishes, but does not performs cleanup operations (until stop is called).
1092  
1093 =head2 wait_stop()
1094  
1095 use Mojo::IOLoop::ReadWriteProcess qw(process);
1096 my $p = process(sub { print "Hello\n" })->start->wait_stop;
1097 # $p is not running anymore, and all possible events have been granted to be emitted.
1098  
1099 Waits until the process finishes, and perform cleanup operations.
1100  
1101 =head2 errored()
1102  
1103 use Mojo::IOLoop::ReadWriteProcess qw(process);
1104 my $p = process(sub { die "Nooo" })->start->wait_stop;
1105 $p->errored; # will return "1"
1106  
1107 Returns a boolean indicating if the process had errors or not.
1108  
1109 =head2 write_pidfile()
1110  
1111 use Mojo::IOLoop::ReadWriteProcess qw(process);
1112 my $p = process(sub { die "Nooo" } );
1113 $p->pidfile("foobar");
1114 $p->start();
1115 $p->write_pidfile();
1116  
1117 Forces writing PID of process to specified pidfile in the attributes of the object.
1118 Useful only if the process have been already started, otherwise if a pidfile it's supplied
1119 as attribute, it will be done automatically.
1120  
1121 =head2 write_stdin()
1122  
1123 use Mojo::IOLoop::ReadWriteProcess qw(process);
1124 my $p = process(sub { my $a = <STDIN>; print STDERR "Hello my name is $a\n"; } )->start;
1125 $p->write_stdin("Larry");
1126 $p->read_stderr; # process STDERR will contain: "Hello my name is Larry\n"
1127  
1128 Write data to process STDIN.
1129  
1130 =head2 write_channel()
1131  
1132 use Mojo::IOLoop::ReadWriteProcess qw(process);
1133 my $p = process(sub {
1134 my $self = shift;
1135 my $parent_output = $self->channel_out;
1136 my $parent_input = $self->channel_in;
1137  
1138 while(defined(my $line = <$parent_input>)) {
1139 print $parent_output "PONG\n" if $line =~ /PING/i;
1140 }
1141 } )->start;
1142 $p->write_channel("PING");
1143 my $out = $p->read_channel;
1144 # $out is PONG
1145 my $child_output = $p->channel_out;
1146 while(defined(my $line = <$child_output>)) {
1147 print "Process is replying back with $line!\n";
1148 $p->write_channel("PING");
1149 }
1150  
1151 Write data to process channel. Note, it's not STDIN, neither STDOUT, it's a complete separate channel
1152 dedicated to parent-child communication.
1153 In the parent process, you can access to the same pipes (but from the opposite direction):
1154  
1155 my $child_output = $self->channel_out;
1156 my $child_input = $self->channel_in;
1157  
1158 =head2 read_stdout()
1159  
1160 use Mojo::IOLoop::ReadWriteProcess qw(process);
1161 my $p = process(sub {
1162 print "Boo\n"
1163 } )->start;
1164 $p->read_stdout;
1165  
1166 Gets a single line from process STDOUT.
1167  
1168 =head2 read_channel()
1169  
1170 use Mojo::IOLoop::ReadWriteProcess qw(process);
1171 my $p = process(sub {
1172 my $self = shift;
1173 my $parent_output = $self->channel_out;
1174 my $parent_input = $self->channel_in;
1175  
1176 print $parent_output "PONG\n";
1177 } )->start;
1178 $p->read_channel;
1179  
1180 Gets a single line from process channel.
1181  
1182 =head2 read_stderr()
1183  
1184 use Mojo::IOLoop::ReadWriteProcess qw(process);
1185 my $p = process(sub {
1186 print STDERR "Boo\n"
1187 } )->start;
1188 $p->read_stderr;
1189  
1190 Gets a single line from process STDERR.
1191  
1192 =head2 read_all_stdout()
1193  
1194 use Mojo::IOLoop::ReadWriteProcess qw(process);
1195 my $p = process(sub {
1196 print "Boo\n"
1197 } )->start;
1198 $p->read_all_stdout;
1199  
1200 Gets all the STDOUT output of the process.
1201  
1202 =head2 read_all_channel()
1203  
1204 use Mojo::IOLoop::ReadWriteProcess qw(process);
1205 my $p = process(sub {
1206 shift->channel_out->write("Ping")
1207 } )->start;
1208 $p->read_all_channel;
1209  
1210 Gets all the channel output of the process.
1211  
1212 =head2 read_all_stderr()
1213  
1214 use Mojo::IOLoop::ReadWriteProcess qw(process);
1215 my $p = process(sub {
1216 print STDERR "Boo\n"
1217 } )->start;
1218 $p->read_all_stderr;
1219  
1220 Gets all the STDERR output of the process.
1221  
1222 =head2 send_signal()
1223  
1224 use Mojo::IOLoop::ReadWriteProcess qw(process);
1225 use POSIX;
1226 my $p = process( execute => "/path/to/bin" )->start;
1227  
1228 $p->send_signal(POSIX::SIGKILL);
1229  
1230 Send a signal to the process
1231  
1232 =head1 EXPORTS
1233  
1234 =head2 parallel()
1235  
1236 use Mojo::IOLoop::ReadWriteProcess qw(parallel);
1237 my $pool = parallel sub { print "Hello\n" } => 5;
1238 $pool->start();
1239 $pool->on( stop => sub { print "Process: ".(+shift()->pid)." finished"; } );
1240 $pool->stop();
1241  
1242 Returns a L<Mojo::IOLoop::ReadWriteProcess::Pool> object that represent a group of processes.
1243  
1244 It accepts the same arguments as L<Mojo::IOLoop::ReadWriteProcess>, and the last one represent the number of processes to generate.
1245  
1246 =head2 batch()
1247  
1248 use Mojo::IOLoop::ReadWriteProcess qw(batch);
1249 my $pool = batch;
1250 $pool->add(sub { print "Hello\n" });
1251 $pool->on(stop => sub { shift->_diag("Done!") })->start->wait_stop;
1252  
1253 Returns a L<Mojo::IOLoop::ReadWriteProcess::Pool> object generated from supplied arguments.
1254 It accepts as input the same parameter of L<Mojo::IOLoop::ReadWriteProcess::Pool> constructor ( see parallel() ).
1255  
1256 =head2 process()
1257  
1258 use Mojo::IOLoop::ReadWriteProcess qw(process);
1259 my $p = process sub { print "Hello\n" };
1260 $p->start()->wait_stop;
1261  
1262 or even:
1263  
1264 process(sub { print "Hello\n" })->start->wait_stop;
1265  
1266 Returns a L<Mojo::IOLoop::ReadWriteProcess> object that represent a process.
1267  
1268 It accepts the same arguments as L<Mojo::IOLoop::ReadWriteProcess>.
1269  
1270 =head2 queue()
1271  
1272 use Mojo::IOLoop::ReadWriteProcess qw(queue);
1273 my $q = queue;
1274 $q->add(sub { return 42 } );
1275 $q->consume;
1276  
1277 Returns a L<Mojo::IOLoop::ReadWriteProcess::Queue> object that represent a queue.
1278  
1279 =head1 DEBUGGING
1280  
1281 You can set the MOJO_EVENTEMITTER_DEBUG environment variable to get some advanced diagnostics information printed to STDERR.
1282  
1283 MOJO_EVENTEMITTER_DEBUG=1
1284  
1285 Also, you can set MOJO_PROCESS_DEBUG environment variable to get diagnostics about the process execution.
1286  
1287 MOJO_PROCESS_DEBUG=1
1288  
1289 =head1 LICENSE
1290  
1291 Copyright (C) Ettore Di Giacinto.
1292  
1293 This library is free software; you can redistribute it and/or modify
1294 it under the same terms as Perl itself.
1295  
1296 =head1 AUTHOR
1297  
1298 Ettore Di Giacinto E<lt>edigiacinto@suse.comE<gt>
1299  
1300 =cut
« lib/Mojo/IOLoop/ReadWriteProcess/Shared/Memory.pm ↑ Summary lib/Mojo/IOLoop/ReadWriteProcess/Session.pm »