Simple.pm

filter: Statementstmt 88.2% 157 / 178 Branchbran 63.8% 23 / 36 Conditioncond 33.3% 4 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 85.7% 42 / 49 Podpod 66.6% 12 / 18 totaltotal 79.3% 238 / 300 CC 24 SCAR 9.9
CC
24
Cov
81%
DESTROY
12.0
debug
6.5
new
4.2
CRAP 2.7
« Simple/Group.pm ↑ Summary scratch.pl »
1 package IPC::Simple;
2 # ABSTRACT: simple, non-blocking IPC
3 4 $IPC::Simple::VERSION = '0.08';
4  
5 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use AnyEvent qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use AnyEvent::Handle qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use IPC::Open3 qw(open3);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use POSIX qw(:sys_wait_h);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use Symbol qw(gensym);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 4 ▶use IPC::Simple::Channel qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 ▶use IPC::Simple::Group qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 4 ▶use IPC::Simple::Message;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 4 ▶use constant STATE_READY => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 4 ▶use constant STATE_RUNNING => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 4 ▶use constant STATE_STOPPING => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 BEGIN{
24 4 ▶ use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
25 4 ▶ our @EXPORT_OK = qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
26 spawn
27 process_group
28 );
29 }
30  
31 #-------------------------------------------------------------------------------
32 # Convenience constructor
33 #-------------------------------------------------------------------------------
34 sub spawn ($;%) {
35 5 ▶ my ($cmd, @args) = @_;
Subroutine spawn: called CC=1 SCAR=0.0
Pod documented
36 5 return IPC::Simple->new(cmd => $cmd, @args);
37 }
38  
39 sub process_group {
40 1 ▶ return IPC::Simple::Group->new(@_);
Subroutine process_group: called CC=1 SCAR=0.0
Pod documented
41 }
42  
43 #-------------------------------------------------------------------------------
44 # Constructor
45 #-------------------------------------------------------------------------------
46 sub new {
47 5 ▶ my ($class, %param) = @_;
Subroutine new: called CC=4 SCAR=14.2
Pod undocumented
48 5 ▶ my $cmd = ref $param{cmd} ? $param{cmd} : [ $param{cmd} ];
Branch: 1 branch
BranchTrueFalseTotal
ref $param{'cmd'} ? : 5 0 5
49 5 ▶ my $eol = defined $param{eol} ? $param{eol} : "\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $param{'eol'} ? : 0 5 5
50 5 ▶ my $name = $param{name} || "@$cmd";
Condition66%2/3
$param{'name'} || CORE::join($", @$cmd)
l!l&&r!l&&!r
230
MC/DC0%0/2
$param{'name'} || CORE::join($", @$cmd)
$param{'name'} CORE::join($", @$cmd)
Truth table66%2/3
A || B
A= $param{'name'}B= CORE::join($", @$cmd)
ABresult
1X1
011
000
51 5 my $recv_cb = $param{recv_cb};
52 5 my $term_cb = $param{term_cb};
53  
54 5 bless{
55 name => $name,
56 cmd => $cmd,
57 eol => $eol,
58 recv_cb => $recv_cb,
59 term_cb => $term_cb,
60 run_state => STATE_READY,
61 pid => undef,
62 handle_in => undef,
63 handle_out => undef,
64 handle_err => undef,
65 exit_status => undef,
66 exit_code => undef,
67 messages => undef,
68 kill_timer => undef,
69 }, $class;
70 }
71  
72 #-------------------------------------------------------------------------------
73 # State accessor and predicates
74 #-------------------------------------------------------------------------------
75 sub run_state {
76 47 ▶ my $self = shift;
Subroutine run_state: called CC=2 SCAR=6.9
Pod undocumented
77  
78 47 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 13 34 47
79 13 my $new_state = shift;
80 13 $self->debug('run state changed to %d', $new_state);
81 13 $self->{run_state} = $new_state;
82 }
83  
84 47 return $self->{run_state};
85 }
86  
87 11 ▶sub is_ready { $_[0]->run_state == STATE_READY }
Subroutine is_ready: called CC=1 SCAR=0.0
Pod undocumented
88 17 ▶sub is_running { $_[0]->run_state == STATE_RUNNING }
Subroutine is_running: called CC=1 SCAR=0.0
Pod undocumented
89 6 ▶sub is_stopping { $_[0]->run_state == STATE_STOPPING }
Subroutine is_stopping: called CC=1 SCAR=0.0
Pod undocumented
90  
91 #-------------------------------------------------------------------------------
92 # Other accessors
93 #-------------------------------------------------------------------------------
94 8 ▶sub name { $_[0]->{name} }
Subroutine name: called CC=1 SCAR=0.0
Pod documented
95 0 ▶sub pid { $_[0]->{pid} }
Subroutine pid: not called CC=1 SCAR=6.9
Pod documented
96 0 ▶sub exit_status { $_[0]->{exit_status} }
Subroutine exit_status: not called CC=1 SCAR=6.9
Pod documented
97 1 ▶sub exit_code { $_[0]->{exit_code} }
Subroutine exit_code: called CC=1 SCAR=0.0
Pod documented
98  
99 #-------------------------------------------------------------------------------
100 # Ensure the process is cleaned up when the object is garbage collected.
101 #-------------------------------------------------------------------------------
102 sub DESTROY {
103 0 ▶ my $self = shift;
Subroutine DESTROY: not called CC=3 SCAR=24.8
104  
105 # Localize globals to avoid affecting global state during shutdown
106 0 local ($., $@, $!, $^E, $?);
107  
108 0 ▶ if ($self->{pid} && waitpid($self->{pid}, WNOHANG) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"pid"} and waitpid($self->{"pid"}, 1) == 0) 0 0 0
Condition0%0/3
$self->{"pid"} and waitpid($self->{"pid"}, 1) == 0
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"pid"} and waitpid($self->{"pid"}, 1) == 0
$self->{"pid"} waitpid($self->{"pid"}, 1) == 0
Truth table0%0/3
A and B
A= $self->{"pid"}B= waitpid($self->{"pid"}, 1) == 0
ABresult
0X0
100
111
109 0 kill 'KILL', $self->{pid};
110 0 waitpid $self->{pid}, 0;
111 }
112 }
113  
114 #-------------------------------------------------------------------------------
115 # Logs debug messages
116 #-------------------------------------------------------------------------------
117 sub debug {
118 43 ▶ my $self = shift;
Subroutine debug: called CC=3 SCAR=18.7
Pod undocumented
119  
120 43 ▶ if ($ENV{IPC_SIMPLE_DEBUG}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"IPC_SIMPLE_DEBUG"}) 0 43 43
121 0 my $msg = sprintf shift, @_;
122  
123 0 my ($pkg, $file, $line) = caller;
124 0 ▶ my $pid = $self->{pid} || '(ready)';
Condition0%0/2
$self->{'pid'} || '(ready)'
l!l
00
MC/DC0%0/1
$self->{'pid'} || '(ready)'
$self->{'pid'}
Truth table0%0/2
A
A= $self->{'pid'}
Aresult
11
00
125 0 my $ts = time;
126  
127 0 warn "<$pkg:$line | $ts | pid:$pid> $msg\n";
128 }
129 }
130  
131 #-------------------------------------------------------------------------------
132 # Launch and helpers
133 #-------------------------------------------------------------------------------
134 sub launch {
135 5 ▶ my $self = shift;
Subroutine launch: called CC=4 SCAR=14.2
Pod documented
136  
137 5 ▶ if ($self->is_running) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_running) 0 5 5
138 0 croak 'already running';
139 }
140  
141 5 ▶ if ($self->is_stopping) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_stopping) 0 5 5
142 0 croak 'process is terminating';
143 }
144  
145 5 my $cmd = $self->{cmd};
146  
147 5 $self->debug('launching: %s', "@$cmd");
148  
149 5 ▶ my $pid = open3(my $in, my $out, my $err = gensym, @$cmd)
Branch: 1 branch
BranchTrueFalseTotal
my $pid = open3(my $in, my $out, my $err = &gensym(), @$cmd) or croak($!) 0 5 5
150 or croak $!;
151  
152 5 $self->debug('process launched with pid %d', $pid);
153  
154 5 $self->run_state(STATE_RUNNING);
155  
156 5 $self->{exit_status} = undef;
157 5 $self->{exit_code} = undef;
158 5 $self->{kill_timer} = undef;
159 5 $self->{pid} = $pid;
160 5 $self->{handle_err} = $self->_build_input_handle($err, IPC_STDERR);
161 5 $self->{handle_out} = $self->_build_input_handle($out, IPC_STDOUT);
162 5 $self->{handle_in} = $self->_build_output_handle($in);
163 5 $self->{messages} = IPC::Simple::Channel->new;
164  
165 5 return 1;
166 }
167  
168 sub _build_output_handle {
169 5 ▶ my ($self, $fh) = @_;
Subroutine _build_output_handle: called CC=1 SCAR=0.1
170  
171 # set non-blocking
172 5 AnyEvent::fh_unblock($fh);
173  
174 my $handle = AnyEvent::Handle->new(
175 fh => $fh,
176 0 ▶ on_error => sub{ $self->_on_error(IPC_STDIN, @_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
177 5 );
178  
179 5 return $handle;
180 }
181  
182 sub _build_input_handle {
183 10 ▶ my ($self, $fh, $type) = @_;
Subroutine _build_input_handle: called CC=1 SCAR=0.0
184  
185 # set non-blocking
186 10 AnyEvent::fh_unblock($fh);
187  
188 my $handle = AnyEvent::Handle->new(
189 fh => $fh,
190 3 ▶ on_eof => sub{ $self->terminate },
Subroutine __ANON__: called CC=1 SCAR=0.0
191 2 ▶ on_error => sub{ $self->_on_error($type, @_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
192 0 ▶ on_read => sub{ $self->_on_read($type, @_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
193 10 );
194  
195 # push an initial read to prime the queue
196 10 $self->_push_read($handle, $type);
197  
198 10 return $handle;
199 }
200  
201 sub _on_error {
202 2 ▶ my ($self, $type, $handle, $fatal, $msg) = @_;
Subroutine _on_error: called CC=2 SCAR=7.0
203 2 $self->_queue_message(IPC_ERROR, $msg);
204  
205 2 ▶ if ($fatal) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fatal) 2 0 2
206 2 $self->terminate;
207 }
208 }
209  
210 sub _on_exit {
211 3 ▶ my ($self, $status) = @_;
Subroutine _on_exit: called CC=4 SCAR=14.2
212 3 undef $self->{kill_timer};
213 3 $self->run_state(STATE_READY);
214 3 ▶ $self->{exit_status} = $status || 0;
Condition50%1/2
$status || 0
l!l
30
MC/DC0%0/1
$status || 0
$status
Truth table50%1/2
A
A= $status
Aresult
11
00
215 3 $self->{exit_code} = $self->{exit_status} >> 8;
216  
217 $self->debug('child (pid %s) exited with status %d (exit code: %d)',
218 $self->{pid} || '(no pid)',
219 $self->{exit_status},
220 $self->{exit_code},
221 3 ▶ );
Condition50%1/2
$self->{'pid'} || '(no pid)'
l!l
30
MC/DC0%0/1
$self->{'pid'} || '(no pid)'
$self->{'pid'}
Truth table50%1/2
A
A= $self->{'pid'}
Aresult
11
00
222  
223 # May not be set yet if launch fails early enough
224 3 ▶ if ($self->{messages}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"messages"}) 3 0 3
225 3 $self->{messages}->shutdown;
226 }
227 }
228  
229 sub _on_read {
230 0 ▶ my ($self, $type, $handle) = @_;
Subroutine _on_read: not called CC=1 SCAR=6.9
231 0 $self->debug('read event type=%s', $type);
232 0 $self->_push_read($handle, $type);
233 }
234  
235 sub _push_read {
236 10 ▶ my ($self, $handle, $type) = @_;
Subroutine _push_read: called CC=1 SCAR=0.0
237 $handle->push_read(line => $self->{eol}, sub{
238 4 ▶ my ($handle, $line) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
239 4 chomp $line;
240 4 $self->_queue_message($type, $line);
241 10 });
242 }
243  
244 sub _queue_message {
245 6 ▶ my ($self, $type, $msg) = @_;
Subroutine _queue_message: called CC=2 SCAR=6.9
246 6 $self->debug('buffered type=%s, msg="%s"', $type, $msg);
247  
248 6 my $message = IPC::Simple::Message->new(
249 source => $self,
250 type => $type,
251 message => $msg,
252 );
253  
254 6 ▶ if ($self->{recv_cb}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'recv_cb'}) { } 4 2 6
255 4 $self->{recv_cb}->($message);
256 } else {
257 2 $self->{messages}->put($message);
258 }
259 }
260  
261 #-------------------------------------------------------------------------------
262 # Send a signal to the process
263 #-------------------------------------------------------------------------------
264 sub signal {
265 5 ▶ my ($self, $signal) = @_;
Subroutine signal: called CC=2 SCAR=7.0
Pod documented
266 5 ▶ if ($self->{pid}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"pid"}) 5 0 5
267 5 $self->debug('sending %s to pid %d', $signal, $self->{pid});
268 5 kill $signal, $self->{pid};
269 }
270 }
271  
272 #-------------------------------------------------------------------------------
273 # Stopping the process and waiting on it to complete
274 #-------------------------------------------------------------------------------
275 sub terminate {
276 10 ▶ my $self = shift;
Subroutine terminate: called CC=4 SCAR=13.9
Pod documented
277 10 my $timeout = shift;
278  
279 10 ▶ if ($self->is_running) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_running) 5 5 10
280 5 $self->signal('TERM');
281 5 $self->run_state(STATE_STOPPING);
282  
283 5 $self->{handle_in}->push_shutdown;
284 5 $self->{handle_out}->push_shutdown;
285 5 $self->{handle_err}->push_shutdown;
286  
287 5 ▶ if (defined $timeout) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $timeout) 3 2 5
288 $self->{kill_timer} = AnyEvent->timer(
289 after => $timeout,
290 cb => sub{
291 0 ▶ $self->signal('KILL');
Subroutine __ANON__: not called CC=1 SCAR=6.9
292 0 undef $self->{kill_timer};
293 },
294 3 );
295 }
296  
297 5 ▶ if ($self->{term_cb}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"term_cb"}) 2 3 5
298 2 $self->{term_cb}->($self);
299 }
300 }
301 }
302  
303 sub join {
304 3 ▶ my $self = shift;
Subroutine join: called CC=2 SCAR=7.0
Pod documented
305  
306 3 ▶ return if $self->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_ready 0 3 3
307  
308 3 $self->debug('waiting for process to exit, pid %d', $self->{pid});
309  
310 3 my $done = AnyEvent->condvar;
311  
312 3 my $timer; $timer = AnyEvent->timer(
313 after => 0,
314 interval => 0.01,
315 cb => sub{
316 # non-blocking waitpid returns 0 if the pid is still alive
317 4 ▶ if (waitpid($self->{pid}, WNOHANG) != 0) {
Subroutine __ANON__: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
if (waitpid($self->{"pid"}, 1) != 0) 3 1 4
318 3 my $status = $?;
319  
320 # another waiter might have already called _on_exit
321 3 ▶ unless ($self->is_ready) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_ready) 3 0 3
322 3 $self->_on_exit($?);
323 }
324  
325 3 $done->send;
326 }
327 },
328 3 );
329  
330 3 $done->recv;
331 }
332  
333 #-------------------------------------------------------------------------------
334 # Messages
335 #-------------------------------------------------------------------------------
336 sub send {
337 1 ▶ my ($self, $msg) = @_;
Subroutine send: called CC=1 SCAR=0.0
Pod documented
338 1 $self->debug('sending "%s"', $msg);
339 1 $self->{handle_in}->push_write($msg . $self->{eol});
340 1 1;
341 }
342  
343 sub recv {
344 2 ▶ my ($self, $type) = @_;
Subroutine recv: called CC=1 SCAR=0.0
Pod documented
345 2 $self->debug('waiting on message from pid %d', $self->{pid});
346 2 $self->{messages}->get;
347 }
348  
349 4 1;
350  
351 __END__
« Simple/Group.pm ↑ Summary scratch.pl »