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