Subprocess/Running.pm

filter: Statementstmt 100% 58 / 58 Branchbran 50.0% 2 / 4 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 100% 17 / 17 Podpod 50.0% 1 / 2 totaltotal 96.4% 81 / 84 CC 5 SCAR 3.3
CC
5
Cov
97%
__ANON__
2.1
__ANON__
2.0
__ANON__
2.0
CRAP 1.4
« Subprocess/Types.pm ↑ Summary Subprocess/Running/Delegate/Callback.pm »
1 package AnyEvent::Subprocess::Running;
2 47 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
3 47 $AnyEvent::Subprocess::Running::VERSION = '1.102912';
4 }
5 # ABSTRACT: represents a running subprocess
6 47 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 47 ▶use Event::Join;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 47 ▶use AnyEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 47 ▶use AnyEvent::Subprocess::Done;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 47 ▶use AnyEvent::Subprocess::Types qw(RunDelegate);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 46 with 'AnyEvent::Subprocess::Role::WithDelegates' => {
14 type => RunDelegate,
15 };
16  
17 # we have to set this "later"
18 has 'child_pid' => (
19 is => 'rw',
20 isa => 'Int',
21 50 ▶ trigger => sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
22 50 my ($self, $pid) = @_;
23 50 ▶ $self->child_listener if defined $pid;
Branch: 1 branch
BranchTrueFalseTotal
if defined $pid 50 0 50
24 },
25 46 );
26  
27 has 'child_listener' => (
28 is => 'ro',
29 lazy => 1,
30 clearer => 'cleanup_child_watcher',
31 50 ▶ default => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
32 50 my $self = shift;
33 50 ▶ confess 'child_listener being built too early'
Branch: 1 branch
BranchTrueFalseTotal
unless $self->child_pid 0 50 50
34 unless $self->child_pid;
35  
36 my $child_listener = AnyEvent->child(
37 pid => $self->child_pid,
38 37 ▶ cb => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
39 37 my ($pid, $status) = @_;
40 37 $self->child_event_joiner->send_event( child => $status );
41 37 $self->cleanup_child_watcher;
42 },
43 50 );
44 50 return $child_listener;
45 }
46 46 );
47  
48 has 'on_completion' => (
49 is => 'ro',
50 isa => 'CodeRef',
51 29 ▶ default => sub { sub {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
52 46 required => 1,
53 );
54  
55 46 has 'child_events' => (
56 is => 'ro',
57 isa => 'ArrayRef[Str]',
58 lazy => 1,
59 builder => '_build_child_events',
60 );
61  
62 78 ▶sub _build_child_events {
Subroutine _build_child_events: called CC=1 SCAR=0.0
63 78 my $self = shift;
64 78 return [qw/child/, $self->_invoke_delegates('build_events')];
65 }
66  
67 has 'child_event_joiner' => (
68 is => 'ro',
69 isa => 'Event::Join',
70 lazy => 1,
71 78 ▶ default => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
72 78 my $self = shift;
73 my $joiner = Event::Join->new(
74 events => $self->child_events,
75 37 ▶ on_completion => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
76 37 my $events = shift;
77 37 my $status = $events->{child};
78  
79 37 $self->_completion_hook(
80 events => $events,
81 status => $status,
82 );
83 }
84 78 );
85  
86 78 for my $d ($self->_delegates){
87 206 my %events = map { $_ => $joiner->event_sender_for($_) } $d->build_events;
88 206 $d->event_senders(\%events);
89 }
90  
91 78 return $joiner;
92 },
93 46 );
94  
95 37 ▶sub _completion_hook {
Subroutine _completion_hook: called CC=1 SCAR=0.0
96 37 my ($self, %args) = @_;
97 37 my $status = $args{status};
98  
99 37 my $done = AnyEvent::Subprocess::Done->new(
100 delegates => [$self->_invoke_delegates('build_done_delegates')],
101 exit_status => $status,
102 );
103  
104 37 $args{done} = $done;
105 37 $args{run} = $self;
106 37 $self->_invoke_delegates('completion_hook', \%args);
107 37 $self->on_completion->($done);
108 }
109  
110 3 ▶sub kill {
Subroutine kill: called CC=2 SCAR=6.9
Pod documented
111 3 my $self = shift;
112 3 ▶ my $signal = shift || 9;
Condition100%2/2
shift() || 9
l!l
21
MC/DC100%1/1
shift() || 9
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
113  
114 3 kill $signal, $self->child_pid; # BAI
115 }
116  
117 78 ▶sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
118 78 my $self = shift;
119 78 $self->child_event_joiner; # vivify
120 }
121  
122 46 __PACKAGE__->meta->make_immutable;
123  
124 46 1;
125  
126  
127  
128 =pod
129  
130 =head1 NAME
131  
132 AnyEvent::Subprocess::Running - represents a running subprocess
133  
134 =head1 VERSION
135  
136 version 1.102912
137  
138 =head1 SYNOPSIS
139  
140 We are C<$run> in a sequence like:
141  
142 my $job = AnyEvent::Subprocess->new ( ... );
143 my $run = $job->run;
144 $run->delegate('stdin')->push_write('Hello, my child!');
145 say "Running child as ", $run->child_pid;
146 $run->kill(11) if $you_enjoy_that_sort_of_thing;
147 my $done = $job->delegate('completion_condvar')->recv;
148 say "Child exited with signal ", $done->exit_signal;
149  
150 =head1 DESCRIPTION
151  
152 An instance of this class is returned when you start a subprocess. It
153 contains the child pid, any delegates that operate on the running
154 subprocess (handles, captures, etc.), and some control methods.
155  
156 =head1 METHODS
157  
158 =head2 child_pid
159  
160 Returns the pid of the child
161  
162 =head2 kill($signal)
163  
164 Kills the child with signal number C<$signal>
165  
166 =head2 delegate($name)
167  
168 Returns the delegate named C<$name>
169  
170 =head1 SEE ALSO
171  
172 L<AnyEvent::Subprocess>
173  
174 L<AnyEvent::Subprocess::Role::WithDelegates>
175  
176 =head1 AUTHOR
177  
178 Jonathan Rockway <jrockway@cpan.org>
179  
180 =head1 COPYRIGHT AND LICENSE
181  
182 This software is copyright (c) 2011 by Jonathan Rockway.
183  
184 This is free software; you can redistribute it and/or modify it under
185 the same terms as the Perl 5 programming language system itself.
186  
187 =cut
188  
189  
190 __END__
« Subprocess/Types.pm ↑ Summary Subprocess/Running/Delegate/Callback.pm »