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