1 package Object::Remote::Role::Connector::PerlInterpreter;
7 use Object::Remote::Logging qw( :log :dlog );
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');
18 #if no child_stderr file handle is specified then stderr
19 #of the child will be connected to stderr of the parent
20 has stderr => ( is => 'rw', default => sub { undef } );
22 sub _build_module_sender {
24 grep {blessed($_) && $_->isa('Object::Remote::ModuleLoader::Hook') }
26 return $hook ? $hook->sender : Object::Remote::ModuleSender->new;
29 has perl_command => (is => 'lazy');
30 has watchdog_timeout => ( is => 'ro', required => 1, default => sub { 0 } );
32 #TODO convert the ulimit and nice values into configurable attributes
33 sub _build_perl_command {[ 'sh -c "ulimit -v 200000; nice -n 15 perl -"' ] }
35 around connect => sub {
36 my ($orig, $self) = (shift, shift);
37 my $f = $self->$start::start($orig => @_);
41 $self->_setup_watchdog_reset($conn);
42 my $sub = $conn->remote_sub('Object::Remote::Logging::init_logging_forwarding');
43 $sub->('Object::Remote::Logging', Object::Remote::Logging->arg_router);
44 Object::Remote::Handle->new(
46 class => 'Object::Remote::ModuleLoader',
47 args => { module_sender => $self->module_sender }
49 require Object::Remote::Prompt;
50 Object::Remote::Prompt::maybe_set_prompt_command_on($conn);
56 sub final_perl_command { shift->perl_command }
60 my $given_stderr = $self->stderr;
63 Dlog_debug { "invoking connection to perl interpreter using command line: $_" } @{$self->final_perl_command};
65 if (defined($given_stderr)) {
66 #if the stderr data goes to an existing file handle
67 #an need an anonymous file handle is required
68 #as the other half of a pipe style file handle pair
69 #so the file handles can go into the run loop
70 $foreign_stderr = gensym();
72 #if no file handle has been specified
73 #for the child's stderr then connect
74 #the child stderr to the parent stderr
75 $foreign_stderr = ">&STDERR";
82 @{$self->final_perl_command},
83 ) or die "Failed to run perl at '$_[0]': $!";
85 if (defined($given_stderr)) {
86 Dlog_debug { "Child process STDERR is being handled via run loop" };
88 Object::Remote->current_loop
90 handle => $foreign_stderr,
91 on_read_ready => sub {
93 my $len = sysread($foreign_stderr, $buf, 32768);
94 if (!defined($len) or $len == 0) {
95 log_trace { "Got EOF or error on child stderr, removing from watcher" };
97 Object::Remote->current_loop
99 handle => $foreign_stderr,
103 Dlog_trace { "got $len characters of stderr data for connection" };
104 print $given_stderr $buf or die "could not send stderr data: $!";
110 return ($foreign_stdin, $foreign_stdout, $pid);
115 my ($foreign_stdin, $foreign_stdout, $pid) = $self->_start_perl(@_);
116 my $to_send = $self->fatnode_text;
117 log_debug { my $len = length($to_send); "Sending contents of fat node to remote node; size is '$len' characters" };
118 Object::Remote->current_loop
120 handle => $foreign_stdin,
121 on_write_ready => sub {
122 my $len = syswrite($foreign_stdin, $to_send, 32768);
124 substr($to_send, 0, $len) = '';
126 # if the stdin went away, we'll never get Shere
127 # so it's not a big deal to simply give up on !defined
128 if (!defined($len) or 0 == length($to_send)) {
129 log_trace { "Got EOF or error when writing fatnode data to filehandle, unwatching it" };
130 Object::Remote->current_loop
132 handle => $foreign_stdin,
136 log_trace { "Sent $len bytes of fatnode data to remote side" };
140 return ($foreign_stdin, $foreign_stdout, $pid);
143 sub _setup_watchdog_reset {
144 my ($self, $conn) = @_;
147 return unless $self->watchdog_timeout;
149 Dlog_trace { "Creating Watchdog management timer for connection id $_" } $conn->_id;
153 $timer_id = Object::Remote->current_loop->watch_time(
154 every => $self->watchdog_timeout / 3,
156 unless(defined($conn)) {
157 log_trace { "Weak reference to connection in Watchdog was lost, terminating update timer $timer_id" };
158 Object::Remote->current_loop->unwatch_time($timer_id);
162 Dlog_trace { "Reseting Watchdog for connection id $_" } $conn->_id;
163 #we do not want to block in the run loop so send the
164 #update off and ignore any result, we don't need it
166 $conn->send_class_call(0, 'Object::Remote::WatchDog', 'reset');
175 require Object::Remote::FatNode;
177 if (defined($self->watchdog_timeout)) {
178 $text = "my \$WATCHDOG_TIMEOUT = '" . $self->watchdog_timeout . "';\n";
179 $text .= "alarm(\$WATCHDOG_TIMEOUT);\n";
181 $text = "my \$WATCHDOG_TIMEOUT = undef;\n";
184 $text .= 'BEGIN { $ENV{OBJECT_REMOTE_DEBUG} = 1 }'."\n"
185 if $ENV{OBJECT_REMOTE_DEBUG};
187 $INC{'Object/Remote/FatNode.pm'} = __FILE__;
188 $Object::Remote::FatNode::DATA = <<'ENDFAT';
190 $text .= do { no warnings 'once'; $Object::Remote::FatNode::DATA };
193 eval $Object::Remote::FatNode::DATA;
197 $text .= "__END__\n";