Commit | Line | Data |
a9fdb55e |
1 | package Object::Remote::Role::Connector::PerlInterpreter; |
2 | |
5953edf6 |
3 | use IPC::Open2; |
90a3a7f2 |
4 | use IPC::Open3; |
4c8c83d7 |
5 | use IO::Handle; |
b1f39f94 |
6 | use Symbol; |
29d4ad76 |
7 | use Object::Remote::Logging qw(:log :dlog router); |
a9fdb55e |
8 | use Object::Remote::ModuleSender; |
9 | use Object::Remote::Handle; |
fbd3b8ec |
10 | use Object::Remote::Future; |
69aaad21 |
11 | use Scalar::Util qw(blessed weaken); |
a9fdb55e |
12 | use Moo::Role; |
13 | |
14 | with 'Object::Remote::Role::Connector'; |
15 | |
03f41c0e |
16 | has module_sender => (is => 'lazy'); |
7985ed9e |
17 | has ulimit => ( is => 'ro'); |
18 | has nice => ( is => 'ro'); |
19 | has watchdog_timeout => ( is => 'ro', required => 1, default => sub { undef }); |
e5b3f03f |
20 | has perl_command => (is => 'lazy'); |
7985ed9e |
21 | has pid => (is => 'rwp'); |
22 | has connection_id => (is => 'rwp'); |
69aaad21 |
23 | |
5953edf6 |
24 | #if no child_stderr file handle is specified then stderr |
25 | #of the child will be connected to stderr of the parent |
69aaad21 |
26 | has stderr => ( is => 'rw', default => sub { undef } ); |
03f41c0e |
27 | |
29d4ad76 |
28 | BEGIN { router()->exclude_forwarding; } |
29 | |
03f41c0e |
30 | sub _build_module_sender { |
18e789ab |
31 | my ($hook) = |
32 | grep {blessed($_) && $_->isa('Object::Remote::ModuleLoader::Hook') } |
33 | @INC; |
03f41c0e |
34 | return $hook ? $hook->sender : Object::Remote::ModuleSender->new; |
35 | } |
36 | |
1a7f821f |
37 | sub _build_perl_command { |
c0d4da69 |
38 | my ($self) = @_; |
1a7f821f |
39 | my $nice = $self->nice; |
364015c9 |
40 | my $ulimit = $self->ulimit; |
c0d4da69 |
41 | my $perl_path = 'perl'; |
42 | my $shell_code = ''; |
43 | |
1a7f821f |
44 | if (defined($ulimit)) { |
c0d4da69 |
45 | $shell_code .= "ulimit $ulimit || exit 1; "; |
1a7f821f |
46 | } |
c0d4da69 |
47 | |
1a7f821f |
48 | if (defined($nice)) { |
49 | $shell_code .= "nice -n $nice "; |
50 | } |
c0d4da69 |
51 | |
1013b46a |
52 | if (defined($ENV{OBJECT_REMOTE_PERL_BIN})) { |
53 | log_debug { "Using OBJECT_REMOTE_PERL_BIN environment variable as perl path" }; |
54 | $perl_path = $ENV{OBJECT_REMOTE_PERL_BIN}; |
c0d4da69 |
55 | } |
56 | |
57 | $shell_code .= $perl_path . ' -'; |
58 | |
59 | return [ 'bash', '-c', $shell_code ]; |
1a7f821f |
60 | } |
498c4ad5 |
61 | |
03f41c0e |
62 | around connect => sub { |
63 | my ($orig, $self) = (shift, shift); |
fbd3b8ec |
64 | my $f = $self->$start::start($orig => @_); |
65 | return future { |
66 | $f->on_done(sub { |
67 | my ($conn) = $f->get; |
7985ed9e |
68 | $self->_setup_watchdog_reset($conn); |
69 | my $sub = $conn->remote_sub('Object::Remote::Logging::init_remote_logging'); |
c3d5ef8a |
70 | $sub->('Object::Remote::Logging', router => router(), connection_id => $conn->_id); |
fbd3b8ec |
71 | Object::Remote::Handle->new( |
72 | connection => $conn, |
73 | class => 'Object::Remote::ModuleLoader', |
74 | args => { module_sender => $self->module_sender } |
75 | )->disarm_free; |
76 | require Object::Remote::Prompt; |
77 | Object::Remote::Prompt::maybe_set_prompt_command_on($conn); |
78 | }); |
79 | $f; |
80 | } 2; |
a9fdb55e |
81 | }; |
82 | |
498c4ad5 |
83 | sub final_perl_command { shift->perl_command } |
a9fdb55e |
84 | |
7efea51f |
85 | sub _start_perl { |
a9fdb55e |
86 | my $self = shift; |
5953edf6 |
87 | my $given_stderr = $self->stderr; |
88 | my $foreign_stderr; |
89 | |
f7175e05 |
90 | Dlog_verbose { |
91 | s/\n/ /g; "invoking connection to perl interpreter using command line: $_" |
92 | } @{$self->final_perl_command}; |
8ed52376 |
93 | |
5953edf6 |
94 | if (defined($given_stderr)) { |
c023aad2 |
95 | #if the stderr data goes to an existing file handle |
96 | #an anonymous file handle is required |
97 | #as the other half of a pipe style file handle pair |
98 | #so the file handles can go into the run loop |
99 | $foreign_stderr = gensym(); |
5953edf6 |
100 | } else { |
c023aad2 |
101 | #if no file handle has been specified |
102 | #for the child's stderr then connect |
103 | #the child stderr to the parent stderr |
104 | $foreign_stderr = ">&STDERR"; |
5953edf6 |
105 | } |
106 | |
107 | my $pid = open3( |
108 | my $foreign_stdin, |
109 | my $foreign_stdout, |
110 | $foreign_stderr, |
111 | @{$self->final_perl_command}, |
112 | ) or die "Failed to run perl at '$_[0]': $!"; |
113 | |
7985ed9e |
114 | $self->_set_pid($pid); |
115 | |
5953edf6 |
116 | if (defined($given_stderr)) { |
c023aad2 |
117 | Dlog_debug { "Child process STDERR is being handled via run loop" }; |
5953edf6 |
118 | |
c023aad2 |
119 | Object::Remote->current_loop |
120 | ->watch_io( |
121 | handle => $foreign_stderr, |
122 | on_read_ready => sub { |
123 | my $buf = ''; |
124 | my $len = sysread($foreign_stderr, $buf, 32768); |
125 | if (!defined($len) or $len == 0) { |
126 | log_trace { "Got EOF or error on child stderr, removing from watcher" }; |
127 | $self->stderr(undef); |
128 | Object::Remote->current_loop->unwatch_io( |
129 | handle => $foreign_stderr, |
130 | on_read_ready => 1 |
131 | ); |
5953edf6 |
132 | } else { |
c023aad2 |
133 | Dlog_trace { "got $len characters of stderr data for connection" }; |
134 | print $given_stderr $buf or die "could not send stderr data: $!"; |
5953edf6 |
135 | } |
b51a8453 |
136 | } |
5953edf6 |
137 | ); |
138 | } |
139 | |
69aaad21 |
140 | return ($foreign_stdin, $foreign_stdout, $pid); |
7efea51f |
141 | } |
142 | |
143 | sub _open2_for { |
144 | my $self = shift; |
145 | my ($foreign_stdin, $foreign_stdout, $pid) = $self->_start_perl(@_); |
fbd3b8ec |
146 | my $to_send = $self->fatnode_text; |
7985ed9e |
147 | log_debug { my $len = length($to_send); "Sending contents of fat node to remote node; size is '$len' characters" }; |
fbd3b8ec |
148 | Object::Remote->current_loop |
149 | ->watch_io( |
150 | handle => $foreign_stdin, |
151 | on_write_ready => sub { |
07105aca |
152 | my $len = syswrite($foreign_stdin, $to_send, 32768); |
fbd3b8ec |
153 | if (defined $len) { |
154 | substr($to_send, 0, $len) = ''; |
155 | } |
156 | # if the stdin went away, we'll never get Shere |
157 | # so it's not a big deal to simply give up on !defined |
b1f39f94 |
158 | if (!defined($len) or 0 == length($to_send)) { |
0511910e |
159 | log_trace { "Got EOF or error when writing fatnode data to filehandle, unwatching it" }; |
fbd3b8ec |
160 | Object::Remote->current_loop |
161 | ->unwatch_io( |
162 | handle => $foreign_stdin, |
163 | on_write_ready => 1 |
c023aad2 |
164 | ); |
0511910e |
165 | } else { |
166 | log_trace { "Sent $len bytes of fatnode data to remote side" }; |
fbd3b8ec |
167 | } |
168 | } |
169 | ); |
a9fdb55e |
170 | return ($foreign_stdin, $foreign_stdout, $pid); |
171 | } |
172 | |
69aaad21 |
173 | sub _setup_watchdog_reset { |
c023aad2 |
174 | my ($self, $conn) = @_; |
175 | my $timer_id; |
69aaad21 |
176 | |
6aaa14ea |
177 | return unless $self->watchdog_timeout; |
69aaad21 |
178 | |
c023aad2 |
179 | Dlog_trace { "Creating Watchdog management timer for connection id $_" } $conn->_id; |
bd20b1bf |
180 | |
c023aad2 |
181 | weaken($conn); |
bd20b1bf |
182 | |
c023aad2 |
183 | $timer_id = Object::Remote->current_loop->watch_time( |
184 | every => $self->watchdog_timeout / 3, |
185 | code => sub { |
186 | unless(defined($conn)) { |
6aaa14ea |
187 | log_warn { "Weak reference to connection in Watchdog was lost, terminating update timer $timer_id" }; |
c023aad2 |
188 | Object::Remote->current_loop->unwatch_time($timer_id); |
6aaa14ea |
189 | return; |
c023aad2 |
190 | } |
6aaa14ea |
191 | |
192 | unless($conn->is_valid) { |
193 | log_warn { "Watchdog timer found an invalid connection, removing the timer" }; |
194 | Object::Remote->current_loop->unwatch_time($timer_id); |
195 | return; |
196 | } |
197 | |
c023aad2 |
198 | Dlog_trace { "Reseting Watchdog for connection id $_" } $conn->_id; |
199 | #we do not want to block in the run loop so send the |
200 | #update off and ignore any result, we don't need it |
201 | #anyway |
202 | $conn->send_class_call(0, 'Object::Remote::WatchDog', 'reset'); |
203 | } |
6aaa14ea |
204 | ); |
205 | |
206 | $conn->on_close->on_ready(sub { |
207 | log_debug { "Removing watchdog for connection that is now closed" }; |
208 | Object::Remote->current_loop->unwatch_time($timer_id); |
209 | }); |
69aaad21 |
210 | } |
211 | |
b1cbd5be |
212 | sub fatnode_text { |
213 | my ($self) = @_; |
2dc23780 |
214 | my $connection_timeout = $self->timeout; |
215 | my $watchdog_timeout = $self->watchdog_timeout; |
b1cbd5be |
216 | my $text = ''; |
69aaad21 |
217 | |
218 | require Object::Remote::FatNode; |
219 | |
2dc23780 |
220 | if (defined($connection_timeout)) { |
221 | $text .= "alarm($connection_timeout);\n"; |
222 | } |
223 | |
224 | if (defined($watchdog_timeout)) { |
7985ed9e |
225 | $text .= "my \$WATCHDOG_TIMEOUT = $watchdog_timeout;\n"; |
bd20b1bf |
226 | } else { |
2dc23780 |
227 | $text .= "my \$WATCHDOG_TIMEOUT = undef;\n"; |
69aaad21 |
228 | } |
bd20b1bf |
229 | |
7985ed9e |
230 | $text .= '$Object::Remote::FatNode::REMOTE_NODE = "1";' . "\n"; |
231 | |
b1cbd5be |
232 | $text .= <<'END'; |
233 | $INC{'Object/Remote/FatNode.pm'} = __FILE__; |
234 | $Object::Remote::FatNode::DATA = <<'ENDFAT'; |
235 | END |
fbd3b8ec |
236 | $text .= do { no warnings 'once'; $Object::Remote::FatNode::DATA }; |
b1cbd5be |
237 | $text .= "ENDFAT\n"; |
238 | $text .= <<'END'; |
239 | eval $Object::Remote::FatNode::DATA; |
1a2d795f |
240 | die $@ if $@; |
b1cbd5be |
241 | END |
69aaad21 |
242 | |
b1cbd5be |
243 | $text .= "__END__\n"; |
244 | return $text; |
245 | } |
246 | |
a9fdb55e |
247 | 1; |