all tests run at trace log level with a null log output; new tests for watchdog,...
[scpubgit/Object-Remote.git] / lib / Object / Remote / Role / Connector / PerlInterpreter.pm
CommitLineData
a9fdb55e 1package Object::Remote::Role::Connector::PerlInterpreter;
2
6b7b2732 3use IPC::Open2;
90115979 4use IPC::Open3;
4c8c83d7 5use IO::Handle;
353556c4 6use Symbol;
c824fdf3 7use Object::Remote::Logging qw( :log :dlog );
a9fdb55e 8use Object::Remote::ModuleSender;
9use Object::Remote::Handle;
fbd3b8ec 10use Object::Remote::Future;
c824fdf3 11use Scalar::Util qw(blessed weaken);
a9fdb55e 12use Moo::Role;
13
14with 'Object::Remote::Role::Connector';
15
03f41c0e 16has module_sender => (is => 'lazy');
37efeb68 17has ulimit => ( is => 'ro' );
18has nice => ( is => 'ro' );
f129bfaf 19has watchdog_timeout => ( is => 'ro', required => 1, default => sub { undef } );
20has perl_command => (is => 'lazy');
c824fdf3 21
6b7b2732 22#if no child_stderr file handle is specified then stderr
23#of the child will be connected to stderr of the parent
c824fdf3 24has stderr => ( is => 'rw', default => sub { undef } );
03f41c0e 25
26sub _build_module_sender {
18e789ab 27 my ($hook) =
28 grep {blessed($_) && $_->isa('Object::Remote::ModuleLoader::Hook') }
29 @INC;
03f41c0e 30 return $hook ? $hook->sender : Object::Remote::ModuleSender->new;
31}
32
37efeb68 33#SSH requires the entire remote command to be
34#given as one single argument to the ssh
35#command line program so this jumps through
36#some hoops
f129bfaf 37
38#TODO this is SSH's problem not perl's so move
39#this to the SSH connector
37efeb68 40sub _build_perl_command {
41 my ($self) = @_;
42 my $nice = $self->nice;
43 my $ulimit = $self->ulimit;
44 my $shell_code = 'sh -c "';
45
46 if (defined($ulimit)) {
47 $shell_code .= "ulimit -v $ulimit; ";
48 }
49
50 if (defined($nice)) {
51 $shell_code .= "nice -n $nice ";
52 }
53
54 $shell_code .= 'perl -"';
55
56 return [ $shell_code ];
57}
498c4ad5 58
03f41c0e 59around connect => sub {
60 my ($orig, $self) = (shift, shift);
fbd3b8ec 61 my $f = $self->$start::start($orig => @_);
62 return future {
63 $f->on_done(sub {
64 my ($conn) = $f->get;
c824fdf3 65 $self->_setup_watchdog_reset($conn);
4a9fa1a5 66 my $sub = $conn->remote_sub('Object::Remote::Logging::init_logging_forwarding');
67 $sub->('Object::Remote::Logging', Object::Remote::Logging->arg_router);
fbd3b8ec 68 Object::Remote::Handle->new(
69 connection => $conn,
70 class => 'Object::Remote::ModuleLoader',
71 args => { module_sender => $self->module_sender }
72 )->disarm_free;
73 require Object::Remote::Prompt;
74 Object::Remote::Prompt::maybe_set_prompt_command_on($conn);
75 });
76 $f;
77 } 2;
a9fdb55e 78};
79
498c4ad5 80sub final_perl_command { shift->perl_command }
a9fdb55e 81
7efea51f 82sub _start_perl {
a9fdb55e 83 my $self = shift;
6b7b2732 84 my $given_stderr = $self->stderr;
85 my $foreign_stderr;
86
9031635d 87 Dlog_debug { "invoking connection to perl interpreter using command line: $_" } @{$self->final_perl_command};
f8080c1c 88
6b7b2732 89 if (defined($given_stderr)) {
c824fdf3 90 #if the stderr data goes to an existing file handle
624072a8 91 #an anonymous file handle is required
c824fdf3 92 #as the other half of a pipe style file handle pair
93 #so the file handles can go into the run loop
6b7b2732 94 $foreign_stderr = gensym();
95 } else {
c824fdf3 96 #if no file handle has been specified
97 #for the child's stderr then connect
98 #the child stderr to the parent stderr
6b7b2732 99 $foreign_stderr = ">&STDERR";
100 }
101
102 my $pid = open3(
103 my $foreign_stdin,
104 my $foreign_stdout,
105 $foreign_stderr,
106 @{$self->final_perl_command},
107 ) or die "Failed to run perl at '$_[0]': $!";
108
109 if (defined($given_stderr)) {
353556c4 110 Dlog_debug { "Child process STDERR is being handled via run loop" };
6b7b2732 111
6b7b2732 112 Object::Remote->current_loop
113 ->watch_io(
114 handle => $foreign_stderr,
115 on_read_ready => sub {
116 my $buf = '';
117 my $len = sysread($foreign_stderr, $buf, 32768);
353556c4 118 if (!defined($len) or $len == 0) {
6b7b2732 119 log_trace { "Got EOF or error on child stderr, removing from watcher" };
120 $self->stderr(undef);
121 Object::Remote->current_loop
122 ->unwatch_io(
123 handle => $foreign_stderr,
124 on_read_ready => 1
125 );
126 } else {
127 Dlog_trace { "got $len characters of stderr data for connection" };
128 print $given_stderr $buf or die "could not send stderr data: $!";
129 }
7790ca36 130 }
6b7b2732 131 );
132 }
133
c824fdf3 134 return ($foreign_stdin, $foreign_stdout, $pid);
7efea51f 135}
136
137sub _open2_for {
138 my $self = shift;
139 my ($foreign_stdin, $foreign_stdout, $pid) = $self->_start_perl(@_);
fbd3b8ec 140 my $to_send = $self->fatnode_text;
5d59cb98 141 log_debug { my $len = length($to_send); "Sending contents of fat node to remote node; size is '$len' characters" };
fbd3b8ec 142 Object::Remote->current_loop
143 ->watch_io(
144 handle => $foreign_stdin,
145 on_write_ready => sub {
9031635d 146 my $len = syswrite($foreign_stdin, $to_send, 32768);
fbd3b8ec 147 if (defined $len) {
148 substr($to_send, 0, $len) = '';
149 }
150 # if the stdin went away, we'll never get Shere
151 # so it's not a big deal to simply give up on !defined
353556c4 152 if (!defined($len) or 0 == length($to_send)) {
5d59cb98 153 log_trace { "Got EOF or error when writing fatnode data to filehandle, unwatching it" };
fbd3b8ec 154 Object::Remote->current_loop
155 ->unwatch_io(
156 handle => $foreign_stdin,
157 on_write_ready => 1
158 );
5d59cb98 159 } else {
160 log_trace { "Sent $len bytes of fatnode data to remote side" };
fbd3b8ec 161 }
162 }
163 );
a9fdb55e 164 return ($foreign_stdin, $foreign_stdout, $pid);
165}
166
c824fdf3 167sub _setup_watchdog_reset {
168 my ($self, $conn) = @_;
169 my $timer_id;
170
171 return unless $self->watchdog_timeout;
172
173 Dlog_trace { "Creating Watchdog management timer for connection id $_" } $conn->_id;
b7a853b3 174
175 weaken($conn);
176
c824fdf3 177 $timer_id = Object::Remote->current_loop->watch_time(
b9baacc2 178 every => $self->watchdog_timeout / 3,
c824fdf3 179 code => sub {
180 unless(defined($conn)) {
181 log_trace { "Weak reference to connection in Watchdog was lost, terminating update timer $timer_id" };
182 Object::Remote->current_loop->unwatch_time($timer_id);
183 return;
184 }
185
b7a853b3 186 Dlog_trace { "Reseting Watchdog for connection id $_" } $conn->_id;
c824fdf3 187 #we do not want to block in the run loop so send the
188 #update off and ignore any result, we don't need it
189 #anyway
190 $conn->send_class_call(0, 'Object::Remote::WatchDog', 'reset');
191 }
b7a853b3 192 );
c824fdf3 193}
194
b1cbd5be 195sub fatnode_text {
196 my ($self) = @_;
b1cbd5be 197 my $text = '';
c824fdf3 198
199 require Object::Remote::FatNode;
200
b7a853b3 201 if (defined($self->watchdog_timeout)) {
202 $text = "my \$WATCHDOG_TIMEOUT = '" . $self->watchdog_timeout . "';\n";
c824fdf3 203 $text .= "alarm(\$WATCHDOG_TIMEOUT);\n";
b7a853b3 204 } else {
205 $text = "my \$WATCHDOG_TIMEOUT = undef;\n";
c824fdf3 206 }
b7a853b3 207
b1cbd5be 208 $text .= 'BEGIN { $ENV{OBJECT_REMOTE_DEBUG} = 1 }'."\n"
209 if $ENV{OBJECT_REMOTE_DEBUG};
210 $text .= <<'END';
211$INC{'Object/Remote/FatNode.pm'} = __FILE__;
212$Object::Remote::FatNode::DATA = <<'ENDFAT';
213END
fbd3b8ec 214 $text .= do { no warnings 'once'; $Object::Remote::FatNode::DATA };
b1cbd5be 215 $text .= "ENDFAT\n";
216 $text .= <<'END';
217eval $Object::Remote::FatNode::DATA;
1a2d795f 218die $@ if $@;
b1cbd5be 219END
c824fdf3 220
b1cbd5be 221 $text .= "__END__\n";
222 return $text;
223}
224
a9fdb55e 2251;