change logging bootstrap to report uninit remote logging metadata; set flag that...
[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;
f4a85080 7use Object::Remote::Logging qw( :log :dlog router );
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');
f1d70835 17has ulimit => ( is => 'ro');
18has nice => ( is => 'ro');
19has watchdog_timeout => ( is => 'ro', required => 1, default => sub { undef });
f129bfaf 20has perl_command => (is => 'lazy');
f1d70835 21has pid => (is => 'rwp');
22has connection_id => (is => 'rwp');
c824fdf3 23
6b7b2732 24#if no child_stderr file handle is specified then stderr
25#of the child will be connected to stderr of the parent
c824fdf3 26has stderr => ( is => 'rw', default => sub { undef } );
03f41c0e 27
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
37efeb68 35sub _build_perl_command {
0fe333eb 36 my ($self) = @_;
37efeb68 37 my $nice = $self->nice;
8506bc08 38 my $ulimit = $self->ulimit;
0fe333eb 39 my $perl_path = 'perl';
40 my $shell_code = '';
41
37efeb68 42 if (defined($ulimit)) {
0fe333eb 43 $shell_code .= "ulimit $ulimit || exit 1; ";
37efeb68 44 }
0fe333eb 45
37efeb68 46 if (defined($nice)) {
47 $shell_code .= "nice -n $nice ";
48 }
0fe333eb 49
29fb87a4 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};
0fe333eb 53 }
54
55 $shell_code .= $perl_path . ' -';
56
57 return [ 'bash', '-c', $shell_code ];
37efeb68 58}
498c4ad5 59
03f41c0e 60around connect => sub {
61 my ($orig, $self) = (shift, shift);
fbd3b8ec 62 my $f = $self->$start::start($orig => @_);
63 return future {
64 $f->on_done(sub {
65 my ($conn) = $f->get;
f1d70835 66 $self->_setup_watchdog_reset($conn);
67 my $sub = $conn->remote_sub('Object::Remote::Logging::init_remote_logging');
f4a85080 68 $sub->('Object::Remote::Logging', router => router(), connection_id => $conn->_id);
fbd3b8ec 69 Object::Remote::Handle->new(
70 connection => $conn,
71 class => 'Object::Remote::ModuleLoader',
72 args => { module_sender => $self->module_sender }
73 )->disarm_free;
74 require Object::Remote::Prompt;
75 Object::Remote::Prompt::maybe_set_prompt_command_on($conn);
76 });
77 $f;
78 } 2;
a9fdb55e 79};
80
498c4ad5 81sub final_perl_command { shift->perl_command }
a9fdb55e 82
7efea51f 83sub _start_perl {
a9fdb55e 84 my $self = shift;
6b7b2732 85 my $given_stderr = $self->stderr;
86 my $foreign_stderr;
87
088218cf 88 Dlog_verbose {
89 s/\n/ /g; "invoking connection to perl interpreter using command line: $_"
90 } @{$self->final_perl_command};
f8080c1c 91
6b7b2732 92 if (defined($given_stderr)) {
09130cd0 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();
6b7b2732 98 } else {
09130cd0 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";
6b7b2732 103 }
104
105 my $pid = open3(
106 my $foreign_stdin,
107 my $foreign_stdout,
108 $foreign_stderr,
109 @{$self->final_perl_command},
110 ) or die "Failed to run perl at '$_[0]': $!";
111
f1d70835 112 $self->_set_pid($pid);
113
6b7b2732 114 if (defined($given_stderr)) {
09130cd0 115 Dlog_debug { "Child process STDERR is being handled via run loop" };
6b7b2732 116
09130cd0 117 Object::Remote->current_loop
118 ->watch_io(
119 handle => $foreign_stderr,
120 on_read_ready => sub {
121 my $buf = '';
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,
128 on_read_ready => 1
129 );
6b7b2732 130 } else {
09130cd0 131 Dlog_trace { "got $len characters of stderr data for connection" };
132 print $given_stderr $buf or die "could not send stderr data: $!";
6b7b2732 133 }
7790ca36 134 }
6b7b2732 135 );
136 }
137
c824fdf3 138 return ($foreign_stdin, $foreign_stdout, $pid);
7efea51f 139}
140
141sub _open2_for {
142 my $self = shift;
143 my ($foreign_stdin, $foreign_stdout, $pid) = $self->_start_perl(@_);
fbd3b8ec 144 my $to_send = $self->fatnode_text;
f1d70835 145 log_debug { my $len = length($to_send); "Sending contents of fat node to remote node; size is '$len' characters" };
fbd3b8ec 146 Object::Remote->current_loop
147 ->watch_io(
148 handle => $foreign_stdin,
149 on_write_ready => sub {
9031635d 150 my $len = syswrite($foreign_stdin, $to_send, 32768);
fbd3b8ec 151 if (defined $len) {
152 substr($to_send, 0, $len) = '';
153 }
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
353556c4 156 if (!defined($len) or 0 == length($to_send)) {
5d59cb98 157 log_trace { "Got EOF or error when writing fatnode data to filehandle, unwatching it" };
fbd3b8ec 158 Object::Remote->current_loop
159 ->unwatch_io(
160 handle => $foreign_stdin,
161 on_write_ready => 1
09130cd0 162 );
5d59cb98 163 } else {
164 log_trace { "Sent $len bytes of fatnode data to remote side" };
fbd3b8ec 165 }
166 }
167 );
a9fdb55e 168 return ($foreign_stdin, $foreign_stdout, $pid);
169}
170
c824fdf3 171sub _setup_watchdog_reset {
09130cd0 172 my ($self, $conn) = @_;
173 my $timer_id;
c824fdf3 174
09130cd0 175 return unless $self->watchdog_timeout;
c824fdf3 176
09130cd0 177 Dlog_trace { "Creating Watchdog management timer for connection id $_" } $conn->_id;
b7a853b3 178
09130cd0 179 weaken($conn);
b7a853b3 180
09130cd0 181 $timer_id = Object::Remote->current_loop->watch_time(
182 every => $self->watchdog_timeout / 3,
183 code => sub {
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);
187 return;
188 }
c824fdf3 189
09130cd0 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
193 #anyway
194 $conn->send_class_call(0, 'Object::Remote::WatchDog', 'reset');
195 }
196 );
c824fdf3 197}
198
b1cbd5be 199sub fatnode_text {
200 my ($self) = @_;
8faf2a28 201 my $connection_timeout = $self->timeout;
202 my $watchdog_timeout = $self->watchdog_timeout;
b1cbd5be 203 my $text = '';
c824fdf3 204
205 require Object::Remote::FatNode;
206
8faf2a28 207 if (defined($connection_timeout)) {
208 $text .= "alarm($connection_timeout);\n";
209 }
210
211 if (defined($watchdog_timeout)) {
f1d70835 212 $text .= "my \$WATCHDOG_TIMEOUT = $watchdog_timeout;\n";
b7a853b3 213 } else {
8faf2a28 214 $text .= "my \$WATCHDOG_TIMEOUT = undef;\n";
c824fdf3 215 }
b7a853b3 216
f1d70835 217 $text .= '$Object::Remote::FatNode::REMOTE_NODE = "1";' . "\n";
218
b1cbd5be 219 $text .= <<'END';
220$INC{'Object/Remote/FatNode.pm'} = __FILE__;
221$Object::Remote::FatNode::DATA = <<'ENDFAT';
222END
fbd3b8ec 223 $text .= do { no warnings 'once'; $Object::Remote::FatNode::DATA };
b1cbd5be 224 $text .= "ENDFAT\n";
225 $text .= <<'END';
226eval $Object::Remote::FatNode::DATA;
1a2d795f 227die $@ if $@;
b1cbd5be 228END
c824fdf3 229
b1cbd5be 230 $text .= "__END__\n";
231 return $text;
232}
233
a9fdb55e 2341;