1 package Object::Remote::Role::Connector::PerlInterpreter;
7 use Object::Remote::Logging qw( :log :dlog router );
8 use Object::Remote::ModuleSender;
9 use Object::Remote::Handle;
10 use Object::Remote::Future;
11 use Scalar::Util qw(blessed weaken);
14 with 'Object::Remote::Role::Connector';
16 has module_sender => (is => 'lazy');
17 has ulimit => ( is => 'ro');
18 has nice => ( is => 'ro');
19 has watchdog_timeout => ( is => 'ro', required => 1, default => sub { undef });
20 has perl_command => (is => 'lazy');
21 has pid => (is => 'rwp');
22 has connection_id => (is => 'rwp');
24 #if no child_stderr file handle is specified then stderr
25 #of the child will be connected to stderr of the parent
26 has stderr => ( is => 'rw', default => sub { undef } );
28 sub _build_module_sender {
30 grep {blessed($_) && $_->isa('Object::Remote::ModuleLoader::Hook') }
32 return $hook ? $hook->sender : Object::Remote::ModuleSender->new;
35 sub _build_perl_command {
37 my $nice = $self->nice;
38 my $ulimit = $self->ulimit;
39 my $perl_path = 'perl';
42 if (defined($ulimit)) {
43 $shell_code .= "ulimit $ulimit || exit 1; ";
47 $shell_code .= "nice -n $nice ";
50 if (defined($ENV{OBJECT_REMOTE_PERL_BIN})) {
51 log_debug { "Using OBJECT_REMOTE_PERL_BIN environment variable as perl path" };
52 $perl_path = $ENV{OBJECT_REMOTE_PERL_BIN};
55 $shell_code .= $perl_path . ' -';
57 return [ 'bash', '-c', $shell_code ];
60 around connect => sub {
61 my ($orig, $self) = (shift, shift);
62 my $f = $self->$start::start($orig => @_);
66 $self->_setup_watchdog_reset($conn);
67 my $sub = $conn->remote_sub('Object::Remote::Logging::init_remote_logging');
68 $sub->('Object::Remote::Logging', router => router(), connection_id => $conn->_id);
69 Object::Remote::Handle->new(
71 class => 'Object::Remote::ModuleLoader',
72 args => { module_sender => $self->module_sender }
74 require Object::Remote::Prompt;
75 Object::Remote::Prompt::maybe_set_prompt_command_on($conn);
81 sub final_perl_command { shift->perl_command }
85 my $given_stderr = $self->stderr;
89 s/\n/ /g; "invoking connection to perl interpreter using command line: $_"
90 } @{$self->final_perl_command};
92 if (defined($given_stderr)) {
93 #if the stderr data goes to an existing file handle
94 #an anonymous file handle is required
95 #as the other half of a pipe style file handle pair
96 #so the file handles can go into the run loop
97 $foreign_stderr = gensym();
99 #if no file handle has been specified
100 #for the child's stderr then connect
101 #the child stderr to the parent stderr
102 $foreign_stderr = ">&STDERR";
109 @{$self->final_perl_command},
110 ) or die "Failed to run perl at '$_[0]': $!";
112 $self->_set_pid($pid);
114 if (defined($given_stderr)) {
115 Dlog_debug { "Child process STDERR is being handled via run loop" };
117 Object::Remote->current_loop
119 handle => $foreign_stderr,
120 on_read_ready => sub {
122 my $len = sysread($foreign_stderr, $buf, 32768);
123 if (!defined($len) or $len == 0) {
124 log_trace { "Got EOF or error on child stderr, removing from watcher" };
125 $self->stderr(undef);
126 Object::Remote->current_loop->unwatch_io(
127 handle => $foreign_stderr,
131 Dlog_trace { "got $len characters of stderr data for connection" };
132 print $given_stderr $buf or die "could not send stderr data: $!";
138 return ($foreign_stdin, $foreign_stdout, $pid);
143 my ($foreign_stdin, $foreign_stdout, $pid) = $self->_start_perl(@_);
144 my $to_send = $self->fatnode_text;
145 log_debug { my $len = length($to_send); "Sending contents of fat node to remote node; size is '$len' characters" };
146 Object::Remote->current_loop
148 handle => $foreign_stdin,
149 on_write_ready => sub {
150 my $len = syswrite($foreign_stdin, $to_send, 32768);
152 substr($to_send, 0, $len) = '';
154 # if the stdin went away, we'll never get Shere
155 # so it's not a big deal to simply give up on !defined
156 if (!defined($len) or 0 == length($to_send)) {
157 log_trace { "Got EOF or error when writing fatnode data to filehandle, unwatching it" };
158 Object::Remote->current_loop
160 handle => $foreign_stdin,
164 log_trace { "Sent $len bytes of fatnode data to remote side" };
168 return ($foreign_stdin, $foreign_stdout, $pid);
171 sub _setup_watchdog_reset {
172 my ($self, $conn) = @_;
175 return unless $self->watchdog_timeout;
177 Dlog_trace { "Creating Watchdog management timer for connection id $_" } $conn->_id;
181 $timer_id = Object::Remote->current_loop->watch_time(
182 every => $self->watchdog_timeout / 3,
184 unless(defined($conn)) {
185 log_trace { "Weak reference to connection in Watchdog was lost, terminating update timer $timer_id" };
186 Object::Remote->current_loop->unwatch_time($timer_id);
190 Dlog_trace { "Reseting Watchdog for connection id $_" } $conn->_id;
191 #we do not want to block in the run loop so send the
192 #update off and ignore any result, we don't need it
194 $conn->send_class_call(0, 'Object::Remote::WatchDog', 'reset');
201 my $connection_timeout = $self->timeout;
202 my $watchdog_timeout = $self->watchdog_timeout;
205 require Object::Remote::FatNode;
207 if (defined($connection_timeout)) {
208 $text .= "alarm($connection_timeout);\n";
211 if (defined($watchdog_timeout)) {
212 $text .= "my \$WATCHDOG_TIMEOUT = $watchdog_timeout;\n";
214 $text .= "my \$WATCHDOG_TIMEOUT = undef;\n";
217 $text .= '$Object::Remote::FatNode::REMOTE_NODE = "1";' . "\n";
220 $INC{'Object/Remote/FatNode.pm'} = __FILE__;
221 $Object::Remote::FatNode::DATA = <<'ENDFAT';
223 $text .= do { no warnings 'once'; $Object::Remote::FatNode::DATA };
226 eval $Object::Remote::FatNode::DATA;
230 $text .= "__END__\n";