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; |
a9fdb55e |
6 | use Object::Remote::ModuleSender; |
7 | use Object::Remote::Handle; |
fbd3b8ec |
8 | use Object::Remote::Future; |
07105aca |
9 | use Object::Remote::Logging qw( :log :dlog ); |
18e789ab |
10 | use Scalar::Util qw(blessed); |
f9ca8099 |
11 | use POSIX ":sys_wait_h"; |
a9fdb55e |
12 | use Moo::Role; |
13 | |
14 | with 'Object::Remote::Role::Connector'; |
15 | |
b51a8453 |
16 | #TODO ugh breaks some of the stuff in System::Introspector::Util by |
17 | #screwing with status value of child |
f9ca8099 |
18 | BEGIN { |
19 | $SIG{CHLD} = sub { |
20 | my $kid; |
21 | do { |
22 | $kid = waitpid(-1, WNOHANG); |
23 | } while $kid > 0; |
24 | } |
25 | } |
26 | |
03f41c0e |
27 | has module_sender => (is => 'lazy'); |
5953edf6 |
28 | #if no child_stderr file handle is specified then stderr |
29 | #of the child will be connected to stderr of the parent |
30 | has stderr => ( is => 'rw', default => sub { \*STDERR } ); |
b51a8453 |
31 | #has stderr => ( is => 'rw' ); |
03f41c0e |
32 | |
33 | sub _build_module_sender { |
18e789ab |
34 | my ($hook) = |
35 | grep {blessed($_) && $_->isa('Object::Remote::ModuleLoader::Hook') } |
36 | @INC; |
03f41c0e |
37 | return $hook ? $hook->sender : Object::Remote::ModuleSender->new; |
38 | } |
39 | |
498c4ad5 |
40 | has perl_command => (is => 'lazy'); |
41 | |
07105aca |
42 | #TODO convert nice value into optional feature enabled by |
43 | #setting value of attribute |
44 | #ulimit of ~500 megs of v-ram |
45 | #TODO only works with ssh with quotes but only works locally |
46 | #with out quotes |
5953edf6 |
47 | #sub _build_perl_command { [ 'sh', '-c', '"ulimit -v 80000; nice -n 15 perl -"' ] } |
f9ca8099 |
48 | sub _build_perl_command { [ 'sh', '-c', '"ulimit -v 200000; nice -n 15 perl -"' ] } |
07105aca |
49 | #sub _build_perl_command { [ 'perl', '-' ] } |
498c4ad5 |
50 | |
03f41c0e |
51 | around connect => sub { |
52 | my ($orig, $self) = (shift, shift); |
fbd3b8ec |
53 | my $f = $self->$start::start($orig => @_); |
54 | return future { |
55 | $f->on_done(sub { |
56 | my ($conn) = $f->get; |
23591f5f |
57 | my $sub = $conn->remote_sub('Object::Remote::Logging::init_logging_forwarding'); |
58 | $sub->('Object::Remote::Logging', Object::Remote::Logging->arg_router); |
fbd3b8ec |
59 | Object::Remote::Handle->new( |
60 | connection => $conn, |
61 | class => 'Object::Remote::ModuleLoader', |
62 | args => { module_sender => $self->module_sender } |
63 | )->disarm_free; |
64 | require Object::Remote::Prompt; |
65 | Object::Remote::Prompt::maybe_set_prompt_command_on($conn); |
66 | }); |
67 | $f; |
68 | } 2; |
a9fdb55e |
69 | }; |
70 | |
498c4ad5 |
71 | sub final_perl_command { shift->perl_command } |
a9fdb55e |
72 | |
7efea51f |
73 | sub _start_perl { |
a9fdb55e |
74 | my $self = shift; |
5953edf6 |
75 | my $given_stderr = $self->stderr; |
76 | my $foreign_stderr; |
77 | |
07105aca |
78 | Dlog_debug { "invoking connection to perl interpreter using command line: $_" } @{$self->final_perl_command}; |
90a3a7f2 |
79 | |
5953edf6 |
80 | use Symbol; |
81 | |
82 | if (defined($given_stderr)) { |
83 | $foreign_stderr = gensym(); |
84 | } else { |
85 | $foreign_stderr = ">&STDERR"; |
86 | } |
87 | |
88 | my $pid = open3( |
89 | my $foreign_stdin, |
90 | my $foreign_stdout, |
91 | $foreign_stderr, |
92 | @{$self->final_perl_command}, |
93 | ) or die "Failed to run perl at '$_[0]': $!"; |
94 | |
95 | if (defined($given_stderr)) { |
96 | log_warn { "using experimental cat for child stderr" }; |
97 | |
98 | #TODO refactor if this solves the problem |
99 | Object::Remote->current_loop |
100 | ->watch_io( |
101 | handle => $foreign_stderr, |
102 | on_read_ready => sub { |
103 | my $buf = ''; |
104 | my $len = sysread($foreign_stderr, $buf, 32768); |
105 | if (!defined($len) or $len == 0) { |
106 | log_trace { "Got EOF or error on child stderr, removing from watcher" }; |
107 | $self->stderr(undef); |
108 | Object::Remote->current_loop |
109 | ->unwatch_io( |
110 | handle => $foreign_stderr, |
111 | on_read_ready => 1 |
112 | ); |
113 | } else { |
114 | Dlog_trace { "got $len characters of stderr data for connection" }; |
115 | print $given_stderr $buf or die "could not send stderr data: $!"; |
116 | } |
b51a8453 |
117 | } |
5953edf6 |
118 | ); |
119 | } |
120 | |
90a3a7f2 |
121 | #TODO open2() dupes the child stderr into the calling |
122 | #process stderr which means if this process exits the |
123 | #child is still attached to the shell - using open3() |
124 | #and having the run loop manage the stderr means this |
125 | #won't happen BUT if the run loop just sends the remote |
126 | #stderr data to the local stderr the logs will interleave |
127 | #for sure - a simple test would be to use open3() and just |
128 | #close the remote stderr and see what happens - a longer |
129 | #term solution would be for Object::Remote to offer a feature |
130 | #where the user of a connection species a destination for output |
131 | #either a file name or their own file handle and the node output |
132 | #is dumped to it |
5953edf6 |
133 | # my $pid = open2( |
134 | # my $foreign_stdout, |
135 | # my $foreign_stdin, |
136 | # @{$self->final_perl_command}, |
137 | # ) or die "Failed to run perl at '$_[0]': $!"; |
b51a8453 |
138 | # |
139 | # Dlog_trace { "Connection to remote side successful; remote stdin and stdout: $_" } [ $foreign_stdin, $foreign_stdout ]; |
90a3a7f2 |
140 | |
b51a8453 |
141 | |
142 | return ($foreign_stdin, $foreign_stdout, $pid); |
7efea51f |
143 | } |
144 | |
0511910e |
145 | #TODO open2() forks off a child and I have not been able to locate |
146 | #a mechanism for reaping dead children so they don't become zombies |
90a3a7f2 |
147 | #CONFIRMED there is no reaping of children being done, find a safe |
148 | #way to do it |
7efea51f |
149 | sub _open2_for { |
150 | my $self = shift; |
151 | my ($foreign_stdin, $foreign_stdout, $pid) = $self->_start_perl(@_); |
fbd3b8ec |
152 | my $to_send = $self->fatnode_text; |
0511910e |
153 | log_debug { my $len = length($to_send); "Sending contents of fat node to remote node; size is '$len' characters" }; |
fbd3b8ec |
154 | Object::Remote->current_loop |
155 | ->watch_io( |
156 | handle => $foreign_stdin, |
157 | on_write_ready => sub { |
07105aca |
158 | my $len = syswrite($foreign_stdin, $to_send, 32768); |
fbd3b8ec |
159 | if (defined $len) { |
160 | substr($to_send, 0, $len) = ''; |
161 | } |
162 | # if the stdin went away, we'll never get Shere |
163 | # so it's not a big deal to simply give up on !defined |
164 | if (!defined($len) or 0 == length($to_send)) { |
0511910e |
165 | log_trace { "Got EOF or error when writing fatnode data to filehandle, unwatching it" }; |
fbd3b8ec |
166 | Object::Remote->current_loop |
167 | ->unwatch_io( |
168 | handle => $foreign_stdin, |
169 | on_write_ready => 1 |
170 | ); |
0511910e |
171 | } else { |
172 | log_trace { "Sent $len bytes of fatnode data to remote side" }; |
fbd3b8ec |
173 | } |
174 | } |
175 | ); |
a9fdb55e |
176 | return ($foreign_stdin, $foreign_stdout, $pid); |
177 | } |
178 | |
b1cbd5be |
179 | sub fatnode_text { |
180 | my ($self) = @_; |
181 | require Object::Remote::FatNode; |
182 | my $text = ''; |
183 | $text .= 'BEGIN { $ENV{OBJECT_REMOTE_DEBUG} = 1 }'."\n" |
184 | if $ENV{OBJECT_REMOTE_DEBUG}; |
185 | $text .= <<'END'; |
186 | $INC{'Object/Remote/FatNode.pm'} = __FILE__; |
187 | $Object::Remote::FatNode::DATA = <<'ENDFAT'; |
188 | END |
fbd3b8ec |
189 | $text .= do { no warnings 'once'; $Object::Remote::FatNode::DATA }; |
b1cbd5be |
190 | $text .= "ENDFAT\n"; |
191 | $text .= <<'END'; |
192 | eval $Object::Remote::FatNode::DATA; |
1a2d795f |
193 | die $@ if $@; |
b1cbd5be |
194 | END |
195 | $text .= "__END__\n"; |
196 | return $text; |
197 | } |
198 | |
a9fdb55e |
199 | 1; |