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