removed the baked in ulimit and nice code, use custom perl_command instead
[scpubgit/Object-Remote.git] / lib / Object / Remote / Role / Connector / PerlInterpreter.pm
CommitLineData
a9fdb55e 1package Object::Remote::Role::Connector::PerlInterpreter;
2
90115979 3use IPC::Open3;
4c8c83d7 4use IO::Handle;
353556c4 5use Symbol;
5add5e29 6use Object::Remote::Logging qw(:log :dlog router);
a9fdb55e 7use Object::Remote::ModuleSender;
8use Object::Remote::Handle;
fbd3b8ec 9use Object::Remote::Future;
c824fdf3 10use Scalar::Util qw(blessed weaken);
a9fdb55e 11use Moo::Role;
12
13with 'Object::Remote::Role::Connector';
14
03f41c0e 15has module_sender => (is => 'lazy');
f1d70835 16has watchdog_timeout => ( is => 'ro', required => 1, default => sub { undef });
90f5193d 17has forward_env => (is => 'ro', required => 1, builder => 1);
f129bfaf 18has perl_command => (is => 'lazy');
f1d70835 19has pid => (is => 'rwp');
20has connection_id => (is => 'rwp');
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
5add5e29 26BEGIN { router()->exclude_forwarding; }
27
03f41c0e 28sub _build_module_sender {
18e789ab 29 my ($hook) =
30 grep {blessed($_) && $_->isa('Object::Remote::ModuleLoader::Hook') }
31 @INC;
03f41c0e 32 return $hook ? $hook->sender : Object::Remote::ModuleSender->new;
33}
34
9cd7015d 35#By policy object-remote does not invoke a shell
37efeb68 36sub _build_perl_command {
9cd7015d 37 my $perl_bin = 'perl';
38
39 if (exists $ENV{OBJECT_REMOTE_PERL_BIN}) {
40 $perl_bin = $ENV{OBJECT_REMOTE_PERL_BIN};
8f43bcd9 41 }
9cd7015d 42 return [qw(perl -)];
37efeb68 43}
498c4ad5 44
90f5193d 45sub _build_forward_env {
46 return [qw(
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
03f41c0e 53around connect => sub {
54 my ($orig, $self) = (shift, shift);
fbd3b8ec 55 my $f = $self->$start::start($orig => @_);
56 return future {
57 $f->on_done(sub {
58 my ($conn) = $f->get;
f1d70835 59 $self->_setup_watchdog_reset($conn);
60 my $sub = $conn->remote_sub('Object::Remote::Logging::init_remote_logging');
f4a85080 61 $sub->('Object::Remote::Logging', router => router(), connection_id => $conn->_id);
fbd3b8ec 62 Object::Remote::Handle->new(
63 connection => $conn,
64 class => 'Object::Remote::ModuleLoader',
65 args => { module_sender => $self->module_sender }
66 )->disarm_free;
67 require Object::Remote::Prompt;
68 Object::Remote::Prompt::maybe_set_prompt_command_on($conn);
69 });
70 $f;
71 } 2;
a9fdb55e 72};
73
498c4ad5 74sub final_perl_command { shift->perl_command }
a9fdb55e 75
7efea51f 76sub _start_perl {
a9fdb55e 77 my $self = shift;
6b7b2732 78 my $given_stderr = $self->stderr;
79 my $foreign_stderr;
80
088218cf 81 Dlog_verbose {
82 s/\n/ /g; "invoking connection to perl interpreter using command line: $_"
83 } @{$self->final_perl_command};
f8080c1c 84
6b7b2732 85 if (defined($given_stderr)) {
09130cd0 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 $foreign_stderr = gensym();
6b7b2732 91 } else {
09130cd0 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 $foreign_stderr = ">&STDERR";
6b7b2732 96 }
97
98 my $pid = open3(
99 my $foreign_stdin,
100 my $foreign_stdout,
101 $foreign_stderr,
102 @{$self->final_perl_command},
103 ) or die "Failed to run perl at '$_[0]': $!";
104
f1d70835 105 $self->_set_pid($pid);
106
6b7b2732 107 if (defined($given_stderr)) {
09130cd0 108 Dlog_debug { "Child process STDERR is being handled via run loop" };
6b7b2732 109
09130cd0 110 Object::Remote->current_loop
111 ->watch_io(
112 handle => $foreign_stderr,
113 on_read_ready => sub {
114 my $buf = '';
115 my $len = sysread($foreign_stderr, $buf, 32768);
116 if (!defined($len) or $len == 0) {
117 log_trace { "Got EOF or error on child stderr, removing from watcher" };
118 $self->stderr(undef);
119 Object::Remote->current_loop->unwatch_io(
120 handle => $foreign_stderr,
121 on_read_ready => 1
122 );
6b7b2732 123 } else {
09130cd0 124 Dlog_trace { "got $len characters of stderr data for connection" };
125 print $given_stderr $buf or die "could not send stderr data: $!";
6b7b2732 126 }
7790ca36 127 }
6b7b2732 128 );
129 }
130
c824fdf3 131 return ($foreign_stdin, $foreign_stdout, $pid);
7efea51f 132}
133
134sub _open2_for {
135 my $self = shift;
136 my ($foreign_stdin, $foreign_stdout, $pid) = $self->_start_perl(@_);
fbd3b8ec 137 my $to_send = $self->fatnode_text;
f1d70835 138 log_debug { my $len = length($to_send); "Sending contents of fat node to remote node; size is '$len' characters" };
fbd3b8ec 139 Object::Remote->current_loop
140 ->watch_io(
141 handle => $foreign_stdin,
142 on_write_ready => sub {
9031635d 143 my $len = syswrite($foreign_stdin, $to_send, 32768);
fbd3b8ec 144 if (defined $len) {
145 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
353556c4 149 if (!defined($len) or 0 == length($to_send)) {
5d59cb98 150 log_trace { "Got EOF or error when writing fatnode data to filehandle, unwatching it" };
fbd3b8ec 151 Object::Remote->current_loop
152 ->unwatch_io(
153 handle => $foreign_stdin,
154 on_write_ready => 1
09130cd0 155 );
5d59cb98 156 } else {
157 log_trace { "Sent $len bytes of fatnode data to remote side" };
fbd3b8ec 158 }
159 }
160 );
a9fdb55e 161 return ($foreign_stdin, $foreign_stdout, $pid);
162}
163
c824fdf3 164sub _setup_watchdog_reset {
09130cd0 165 my ($self, $conn) = @_;
166 my $timer_id;
c824fdf3 167
867e4de5 168 return unless $self->watchdog_timeout;
c824fdf3 169
09130cd0 170 Dlog_trace { "Creating Watchdog management timer for connection id $_" } $conn->_id;
b7a853b3 171
09130cd0 172 weaken($conn);
b7a853b3 173
09130cd0 174 $timer_id = Object::Remote->current_loop->watch_time(
175 every => $self->watchdog_timeout / 3,
176 code => sub {
177 unless(defined($conn)) {
867e4de5 178 log_warn { "Weak reference to connection in Watchdog was lost, terminating update timer $timer_id" };
09130cd0 179 Object::Remote->current_loop->unwatch_time($timer_id);
867e4de5 180 return;
09130cd0 181 }
867e4de5 182
183 unless($conn->is_valid) {
184 log_warn { "Watchdog timer found an invalid connection, removing the timer" };
185 Object::Remote->current_loop->unwatch_time($timer_id);
186 return;
187 }
188
09130cd0 189 Dlog_trace { "Reseting Watchdog for connection id $_" } $conn->_id;
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 $conn->send_class_call(0, 'Object::Remote::WatchDog', 'reset');
194 }
867e4de5 195 );
196
197 $conn->on_close->on_ready(sub {
198 log_debug { "Removing watchdog for connection that is now closed" };
199 Object::Remote->current_loop->unwatch_time($timer_id);
200 });
c824fdf3 201}
202
b1cbd5be 203sub fatnode_text {
204 my ($self) = @_;
8faf2a28 205 my $connection_timeout = $self->timeout;
206 my $watchdog_timeout = $self->watchdog_timeout;
b1cbd5be 207 my $text = '';
c824fdf3 208
209 require Object::Remote::FatNode;
210
8faf2a28 211 if (defined($connection_timeout)) {
212 $text .= "alarm($connection_timeout);\n";
213 }
214
215 if (defined($watchdog_timeout)) {
f1d70835 216 $text .= "my \$WATCHDOG_TIMEOUT = $watchdog_timeout;\n";
b7a853b3 217 } else {
8faf2a28 218 $text .= "my \$WATCHDOG_TIMEOUT = undef;\n";
c824fdf3 219 }
b7a853b3 220
90f5193d 221 $text .= $self->_create_env_forward(@{$self->forward_env});
ffc9934e 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
f1d70835 228 $text .= '$Object::Remote::FatNode::REMOTE_NODE = "1";' . "\n";
229
b1cbd5be 230 $text .= <<'END';
231$INC{'Object/Remote/FatNode.pm'} = __FILE__;
232$Object::Remote::FatNode::DATA = <<'ENDFAT';
233END
fbd3b8ec 234 $text .= do { no warnings 'once'; $Object::Remote::FatNode::DATA };
b1cbd5be 235 $text .= "ENDFAT\n";
236 $text .= <<'END';
237eval $Object::Remote::FatNode::DATA;
1a2d795f 238die $@ if $@;
b1cbd5be 239END
c824fdf3 240
b1cbd5be 241 $text .= "__END__\n";
242 return $text;
243}
244
90f5193d 245sub _create_env_forward {
246 my ($self, @env_names) = @_;
247 my $code = '';
248
249 foreach my $name (@env_names) {
250 next unless exists $ENV{$name};
251 my $value = $ENV{$name};
252 $name =~ s/'/\\'/g;
253 if(defined($value)) {
254 $value =~ s/'/\\'/g;
255 $value = "'$value'";
256 } else {
257 $value = 'undef';
258 }
259 $code .= "\$ENV{'$name'} = $value;\n";
260 }
261
262 return $code;
263}
264
a9fdb55e 2651;