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