Remote/Role/Connector/PerlInterpreter.pm

filter: Statementstmt 76.0% 124 / 163 Branchbran 52.7% 19 / 36 Conditioncond 25.0% 3 / 12 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 65.1% 28 / 43 Podpod 0.0% 0 / 2 totaltotal 66.2% 175 / 264 CC 41 SCAR 15.5
CC
41
Cov
69%
__ANON__
20.0
__ANON__
20.0
_start_perl
8.1
CRAP 4.7
« Remote/Proxy.pm ↑ Summary Remote/ConnectionServer.pm »
1 package Object::Remote::Role::Connector::PerlInterpreter;
2  
3 28 ▶use IPC::Open3;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 28 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 28 ▶use Symbol;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 14 ▶use Object::Remote::Logging qw(:log :dlog router);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 14 ▶use Object::Remote::ModuleSender;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 14 ▶use Object::Remote::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 14 ▶use Object::Remote::Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 14 ▶use Scalar::Util qw(blessed weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 14 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 with 'Object::Remote::Role::Connector';
14  
15 has module_sender => (is => 'lazy');
16 23 ▶has watchdog_timeout => ( is => 'ro', required => 1, default => sub { undef });
Subroutine __ANON__: called CC=1 SCAR=0.0
17 has forward_env => (is => 'ro', required => 1, builder => 1);
18 has perl_command => (is => 'lazy');
19 has pid => (is => 'rwp');
20 has connection_id => (is => 'rwp');
21  
22 #if no child_stderr file handle is specified then stderr
23 #of the child will be connected to stderr of the parent
24 24 ▶has stderr => ( is => 'rw', default => sub { undef } );
Subroutine __ANON__: called CC=1 SCAR=0.0
25  
26 14 ▶BEGIN { router()->exclude_forwarding; }
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 18 ▶sub _build_module_sender {
Subroutine _build_module_sender: called CC=3 SCAR=17.8
29 my ($hook) =
30 0 ▶ grep {blessed($_) && $_->isa('Object::Remote::ModuleLoader::Hook') }
Subroutine _build_module_sender: not called CC=3 SCAR=17.8
Condition16%1/6
builtin::blessed($_) && $_->isa('Object::Remote::ModuleLoader::Hook')
!ll&&!rl&&r
000
builtin::blessed($_) && $_->isa('Object::Remote::ModuleLoader::Hook')
!ll&&!rl&&r
14400
MC/DC0%0/2
builtin::blessed($_) && $_->isa('Object::Remote::ModuleLoader::Hook')
builtin::blessed($_) $_->isa('Object::Remote::ModuleLoader::Hook')
MC/DC0%0/2
builtin::blessed($_) && $_->isa('Object::Remote::ModuleLoader::Hook')
builtin::blessed($_) $_->isa('Object::Remote::ModuleLoader::Hook')
Truth table0%0/3
A && B
A= builtin::blessed($_)B= $_->isa('Object::Remote::ModuleLoader::Hook')
ABresult
0X0
100
111
Truth table33%1/3
A && B
A= builtin::blessed($_)B= $_->isa('Object::Remote::ModuleLoader::Hook')
ABresult
0X0
100
111
31 @INC;
32 0 ▶ return $hook ? $hook->sender : Object::Remote::ModuleSender->new;
Branch: 2 branches
BranchTrueFalseTotal
$hook ? : 0 0 0
$hook ? : 0 18 18
33 }
34  
35 #By policy object-remote does not invoke a shell
36 22 ▶sub _build_perl_command {
Subroutine _build_perl_command: called CC=2 SCAR=9.2
37 0 ▶ my $perl_bin = 'perl';
Subroutine _build_perl_command: not called CC=2 SCAR=9.2
38  
39 0 ▶ if (exists $ENV{OBJECT_REMOTE_PERL_BIN}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $ENV{"OBJECT_REMOTE_PERL_BIN"}) 0 0 0
if (exists $ENV{"OBJECT_REMOTE_PERL_BIN"}) 1 21 22
40 0 $perl_bin = $ENV{OBJECT_REMOTE_PERL_BIN};
41 }
42 0 return [$perl_bin, '-'];
43 }
44  
45 24 ▶sub _build_forward_env {
Subroutine _build_forward_env: called CC=1 SCAR=1.2
46 0 ▶ return [qw(
Subroutine _build_forward_env: not called CC=1 SCAR=1.2
47 OBJECT_REMOTE_PERL_BIN
48 OBJECT_REMOTE_LOG_LEVEL OBJECT_REMOTE_LOG_FORMAT OBJECT_REMOTE_LOG_SELECTIONS
49 OBJECT_REMOTE_LOG_FORWARDING
50 )];
51 }
52  
53 20 ▶around connect => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
54 20 my ($orig, $self) = (shift, shift);
55 20 my $f = $self->$start::start($orig => @_);
56 20 ▶ return future {
Subroutine __ANON__: called CC=1 SCAR=0.0
57 19 ▶ $f->on_done(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
58 19 my ($conn) = $f->get;
59 19 $self->_setup_watchdog_reset($conn);
60 19 my $sub = $conn->remote_sub('Object::Remote::Logging::init_remote_logging');
61 19 $sub->('Object::Remote::Logging', router => router(), connection_id => $conn->_id);
62 19 Object::Remote::Handle->new(
63 connection => $conn,
64 class => 'Object::Remote::ModuleLoader',
65 args => { module_sender => $self->module_sender }
66 )->disarm_free;
67 19 require Object::Remote::Prompt;
68 19 Object::Remote::Prompt::maybe_set_prompt_command_on($conn);
69 20 });
70 20 $f;
71 20 } 2;
72 };
73  
74 42 ▶sub final_perl_command { shift->perl_command }
Subroutine final_perl_command: called CC=1 SCAR=0.0
Pod undocumented
75  
76 sub _start_perl {
77 20 ▶ my $self = shift;
Subroutine _start_perl: called CC=4 SCAR=20.9
78 20 my $given_stderr = $self->stderr;
79 20 my $foreign_stderr;
80  
81 Dlog_verbose {
82 0 ▶ s/\n/ /g; "invoking connection to perl interpreter using command line: $_"
Subroutine __ANON__: not called CC=1 SCAR=6.9
83 20 } @{$self->final_perl_command};
84  
85 20 ▶ if (defined($given_stderr)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $given_stderr) { } 0 20 20
86 #if the stderr data goes to an existing file handle
87 #an anonymous file handle is required
88 #as the other half of a pipe style file handle pair
89 #so the file handles can go into the run loop
90 0 $foreign_stderr = gensym();
91 } else {
92 #if no file handle has been specified
93 #for the child's stderr then connect
94 #the child stderr to the parent stderr
95 20 $foreign_stderr = ">&STDERR";
96 }
97  
98 my $pid = open3(
99 my $foreign_stdin,
100 my $foreign_stdout,
101 $foreign_stderr,
102 20 ▶ @{$self->final_perl_command},
Branch: 1 branch
BranchTrueFalseTotal
my $pid = open3(my $foreign_stdin, my $foreign_stdout, $foreign_stderr, @{$self->final_perl_command;}) or die "Failed to run perl at '$_[0]': $!" 20 0 20
103 ) or die "Failed to run perl at '$_[0]': $!";
104  
105 20 $self->_set_pid($pid);
106  
107 20 ▶ if (defined($given_stderr)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $given_stderr) 0 20 20
108 0 ▶ Dlog_debug { "Child process STDERR is being handled via run loop" };
Subroutine __ANON__: not called CC=1 SCAR=6.9
109  
110 Object::Remote->current_loop
111 ->watch_io(
112 handle => $foreign_stderr,
113 on_read_ready => sub {
114 0 ▶ my $buf = '';
Subroutine __ANON__: not called CC=4 SCAR=30.0
115 0 my $len = sysread($foreign_stderr, $buf, 32768);
116 0 ▶ if (!defined($len) or $len == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $len or $len == 0) { } 0 0 0
Condition0%0/3
not defined $len or $len == 0
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $len or $len == 0
not defined $len $len == 0
Truth table0%0/3
not(A) or B
A= not defined $lenB= $len == 0
ABresult
1X1
011
000
117 0 ▶ log_trace { "Got EOF or error on child stderr, removing from watcher" };
Subroutine __ANON__: not called CC=1 SCAR=6.9
118 0 $self->stderr(undef);
119 0 Object::Remote->current_loop->unwatch_io(
120 handle => $foreign_stderr,
121 on_read_ready => 1
122 );
123 } else {
124 0 ▶ Dlog_trace { "got $len characters of stderr data for connection" };
Subroutine __ANON__: not called CC=1 SCAR=6.9
125 0 ▶ print $given_stderr $buf or die "could not send stderr data: $!";
Branch: 1 branch
BranchTrueFalseTotal
print $given_stderr $buf or die "could not send stderr data: $!" 0 0 0
126 }
127 }
128 0 );
129 }
130  
131 20 return ($foreign_stdin, $foreign_stdout, $pid);
132 }
133  
134 sub _open2_for {
135 20 ▶ my $self = shift;
Subroutine _open2_for: called CC=1 SCAR=0.2
136 20 my ($foreign_stdin, $foreign_stdout, $pid) = $self->_start_perl(@_);
137 20 my $to_send = $self->fatnode_text;
138 20 ▶ log_debug { my $len = length($to_send); "Sending contents of fat node to remote node; size is '$len' characters" };
Subroutine __ANON__: not called CC=1 SCAR=2.6
139 Object::Remote->current_loop
140 ->watch_io(
141 handle => $foreign_stdin,
142 on_write_ready => sub {
143 1009 ▶ my $len = syswrite($foreign_stdin, $to_send, 32768);
Subroutine __ANON__: called CC=4 SCAR=14.5
144 1009 ▶ if (defined $len) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $len) 1009 0 1009
145 1009 substr($to_send, 0, $len) = '';
146 }
147 # if the stdin went away, we'll never get Shere
148 # so it's not a big deal to simply give up on !defined
149 1009 ▶ if (!defined($len) or 0 == length($to_send)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $len or 0 == length $to_send) { } 19 990 1009
Condition66%2/3
not defined $len or 0 == length $to_send
l!l&&r!l&&!r
019990
MC/DC50%1/2
not defined $len or 0 == length $to_send
not defined $len 0 == length $to_send
Truth table66%2/3
not(A) or B
A= not defined $lenB= 0 == length $to_send
ABresult
1X1
011
000
150 19 ▶ log_trace { "Got EOF or error when writing fatnode data to filehandle, unwatching it" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
151 19 Object::Remote->current_loop
152 ->unwatch_io(
153 handle => $foreign_stdin,
154 on_write_ready => 1
155 );
156 } else {
157 990 ▶ log_trace { "Sent $len bytes of fatnode data to remote side" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
158 }
159 }
160 20 );
161 20 return ($foreign_stdin, $foreign_stdout, $pid);
162 }
163  
164 sub _setup_watchdog_reset {
165 19 ▶ my ($self, $conn) = @_;
Subroutine _setup_watchdog_reset: called CC=2 SCAR=8.1
166 19 my $timer_id;
167  
168 19 ▶ return unless $self->watchdog_timeout;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->watchdog_timeout 18 1 19
169  
170 1 ▶ Dlog_trace { "Creating Watchdog management timer for connection id $_" } $conn->_id;
Subroutine __ANON__: not called CC=1 SCAR=1.2
171  
172 1 weaken($conn);
173  
174 $timer_id = Object::Remote->current_loop->watch_time(
175 every => $self->watchdog_timeout / 3,
176 code => sub {
177 4 ▶ unless(defined($conn)) {
Subroutine __ANON__: called CC=3 SCAR=16.9
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $conn) 0 4 4
178 0 ▶ log_warn { "Weak reference to connection in Watchdog was lost, terminating update timer $timer_id" };
Subroutine __ANON__: not called CC=1 SCAR=6.9
179 0 Object::Remote->current_loop->unwatch_time($timer_id);
180 0 return;
181 }
182  
183 4 ▶ unless($conn->is_valid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($conn->is_valid) 0 4 4
184 0 ▶ log_warn { "Watchdog timer found an invalid connection, removing the timer" };
Subroutine __ANON__: not called CC=1 SCAR=6.9
185 0 Object::Remote->current_loop->unwatch_time($timer_id);
186 0 return;
187 }
188  
189 4 ▶ Dlog_trace { "Reseting Watchdog for connection id $_" } $conn->_id;
Subroutine __ANON__: not called CC=1 SCAR=1.2
190 #we do not want to block in the run loop so send the
191 #update off and ignore any result, we don't need it
192 #anyway
193 4 $conn->send_class_call(0, 'Object::Remote::WatchDog', 'reset');
194 }
195 1 );
196  
197 $conn->on_close->on_ready(sub {
198 1 ▶ log_debug { "Removing watchdog for connection that is now closed" };
Subroutine __ANON__: called CC=1 SCAR=0.0
199 22 Object::Remote->current_loop->unwatch_time($timer_id);
200 1 });
201 }
202  
203 sub fatnode_text {
204 21 ▶ my ($self) = @_;
Subroutine fatnode_text: called CC=3 SCAR=11.0
Pod undocumented
205 21 my $connection_timeout = $self->timeout;
206 21 my $watchdog_timeout = $self->watchdog_timeout;
207 21 my $text = '';
208  
209 21 require Object::Remote::FatNode;
210  
211 21 ▶ if (defined($connection_timeout)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $connection_timeout) 21 0 21
212 1 $text .= "alarm($connection_timeout);\n";
213 }
214  
215 20 ▶ if (defined($watchdog_timeout)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $watchdog_timeout) { } 1 20 21
216 21 $text .= "my \$WATCHDOG_TIMEOUT = $watchdog_timeout;\n";
217 } else {
218 21 $text .= "my \$WATCHDOG_TIMEOUT = undef;\n";
219 }
220  
221 21 $text .= $self->_create_env_forward(@{$self->forward_env});
222  
223 #Action at a distance but at least it's not spooky - the logging
224 #system needs to know if a node is remote but there is a period
225 #during init where the remote connection information has not been
226 #setup on the remote side yet so this flag allows a graceful
227 #degredation to happen
228 21 $text .= '$Object::Remote::FatNode::REMOTE_NODE = "1";' . "\n";
229  
230 21 $text .= <<'END';
231 $INC{'Object/Remote/FatNode.pm'} = __FILE__;
232 $Object::Remote::FatNode::DATA = <<'ENDFAT';
233 END
234 21 ▶ $text .= do { no warnings 'once'; $Object::Remote::FatNode::DATA };
Subroutine BEGIN: called CC=1 SCAR=0.0
235 21 $text .= "ENDFAT\n";
236 21 $text .= <<'END';
237 eval $Object::Remote::FatNode::DATA;
238 die $@ if $@;
239 END
240  
241 21 $text .= "__END__\n";
242 21 return $text;
243 }
244  
245 sub _create_env_forward {
246 21 ▶ my ($self, @env_names) = @_;
Subroutine _create_env_forward: called CC=4 SCAR=13.9
247 105 my $code = '';
248  
249 21 foreach my $name (@env_names) {
250 21 ▶ next unless exists $ENV{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $ENV{$name} 84 21 105
251 21 my $value = $ENV{$name};
252 21 $name =~ s/'/\\'/g;
253 21 ▶ if(defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) { } 21 0 21
254 0 $value =~ s/'/\\'/g;
255 21 $value = "'$value'";
256 } else {
257 21 $value = 'undef';
258 }
259 14 $code .= "\$ENV{'$name'} = $value;\n";
260 }
261  
262 14 return $code;
263 }
264  
265 1;
266  
267 =head1 NAME
268  
269 Object::Remote::Role::Connector::PerlInterpreter - Role for connections to a Perl interpreter
270  
271 =head1 SYNOPSIS
272  
273 use Object::Remote;
274  
275 my %opts = (
276 perl_command => [qw(nice -n 10 perl -)],
277 watchdog_timeout => 120, stderr => \*STDERR,
278 );
279  
280 my $local_connection = Object::Remote->connect('-', %opts);
281 my $hostname = Sys::Hostname->can::on($remote, 'hostname');
282  
283 =head1 DESCRIPTION
284  
285 This is the role that supports connections to a Perl interpreter that is executed in a
286 different process. The new Perl interpreter can be either on the local or a remote machine
287 and is configurable via arguments passed to the constructor.
288  
289 =head1 ARGUMENTS
290  
291 =over 4
292  
293 =item perl_command
294  
295 By default the Perl interpeter will be executed as "perl -" but this can be changed by
296 providing an array reference as the value to the perl_command attribute during construction.
297  
298 =item stderr
299  
300 If this value is defined then it will be used as the file handle that receives the output
301 of STDERR from the Perl interpreter process and I/O will be performed by the run loop in a
302 non-blocking way. If the value is undefined then STDERR of the remote process will be connected
303 directly to STDERR of the local process with out the run loop managing I/O. The default value
304 is undefined.
305  
306 There are a few ways to use this feature. By default the behavior is to form one unified STDERR
307 across all of the Perl interpreters including the local one. For small scale and quick operation
308 this offers a predictable and easy to use way to get at error messages generated anywhere. If
309 the local Perl interpreter crashes then the remote Perl interpreters still have an active STDERR
310 and it is possible to still receive output from them. This is generally a good thing but can
311 cause issues.
312  
313 When using a file handle as the output for STDERR once the local Perl interpreter is no longer
314 running there is no longer a valid STDERR for the remote interpreters to send data to. This means
315 that it is no longer possible to receive error output from the remote interpreters and that the
316 shell will start to kill off the child processes. Passing a reference to STDERR for the local
317 interpreter (as the SYNOPSIS shows) causes the run loop to manage I/O, one unified STDERR for
318 all Perl interpreters that ends as soon as the local interpreter process does, and the shell will
319 start killing children when the local interpreter exits.
320  
321 It is also possible to pass in a file handle that has been opened for writing. This would be
322 useful for logging the output of the remote interpreter directly into a dedicated file.
323  
324 =item watchdog_timeout
325  
326 If this value is defined then it will be used as the number of seconds the watchdog will wait
327 for an update before it terminates the Perl interpreter process. The default value is undefined
328 and will not use the watchdog. See C<Object::Remote::Watchdog> for more information.
329  
330 =back
331  
332 =head1 SEE ALSO
333  
334 =over 4
335  
336 =item C<Object::Remote>
337  
338 =back
339  
340 =cut
« Remote/Proxy.pm ↑ Summary Remote/ConnectionServer.pm »