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