experimental move to non-blocking reads in ReadChannel; fix log bugs; annotate fixes...
[scpubgit/Object-Remote.git] / lib / Object / Remote / Role / Connector / PerlInterpreter.pm
CommitLineData
a9fdb55e 1package Object::Remote::Role::Connector::PerlInterpreter;
2
90115979 3#use IPC::Open2;
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');
16
17sub _build_module_sender {
18e789ab 18 my ($hook) =
19 grep {blessed($_) && $_->isa('Object::Remote::ModuleLoader::Hook') }
20 @INC;
03f41c0e 21 return $hook ? $hook->sender : Object::Remote::ModuleSender->new;
22}
23
498c4ad5 24has perl_command => (is => 'lazy');
25
9031635d 26#TODO convert nice value into optional feature enabled by
27#setting value of attribute
28#ulimit of ~500 megs of v-ram
29#TODO only works with ssh with quotes but only works locally
30#with out quotes
31sub _build_perl_command { [ 'sh', '-c', '"ulimit -v 500000; nice -n 15 perl -"' ] }
32#sub _build_perl_command { [ 'perl', '-' ] }
498c4ad5 33
03f41c0e 34around connect => sub {
35 my ($orig, $self) = (shift, shift);
fbd3b8ec 36 my $f = $self->$start::start($orig => @_);
37 return future {
38 $f->on_done(sub {
39 my ($conn) = $f->get;
4a9fa1a5 40 my $sub = $conn->remote_sub('Object::Remote::Logging::init_logging_forwarding');
41 $sub->('Object::Remote::Logging', Object::Remote::Logging->arg_router);
fbd3b8ec 42 Object::Remote::Handle->new(
43 connection => $conn,
44 class => 'Object::Remote::ModuleLoader',
45 args => { module_sender => $self->module_sender }
46 )->disarm_free;
47 require Object::Remote::Prompt;
48 Object::Remote::Prompt::maybe_set_prompt_command_on($conn);
49 });
50 $f;
51 } 2;
a9fdb55e 52};
53
498c4ad5 54sub final_perl_command { shift->perl_command }
a9fdb55e 55
7efea51f 56sub _start_perl {
a9fdb55e 57 my $self = shift;
9031635d 58 Dlog_debug { "invoking connection to perl interpreter using command line: $_" } @{$self->final_perl_command};
90115979 59
60 #TODO open2() dupes the child stderr into the calling
61 #process stderr which means if this process exits the
62 #child is still attached to the shell - using open3()
63 #and having the run loop manage the stderr means this
64 #won't happen BUT if the run loop just sends the remote
65 #stderr data to the local stderr the logs will interleave
66 #for sure - a simple test would be to use open3() and just
67 #close the remote stderr and see what happens - a longer
68 #term solution would be for Object::Remote to offer a feature
69 #where the user of a connection species a destination for output
70 #either a file name or their own file handle and the node output
71 #is dumped to it
a9fdb55e 72 my $pid = open2(
73 my $foreign_stdout,
74 my $foreign_stdin,
498c4ad5 75 @{$self->final_perl_command},
a9fdb55e 76 ) or die "Failed to run perl at '$_[0]': $!";
90115979 77
9031635d 78 Dlog_trace { "Connection to remote side successful; remote stdin and stdout: $_" } [ $foreign_stdin, $foreign_stdout ];
7efea51f 79 return ($foreign_stdin, $foreign_stdout, $pid);
80}
81
5d59cb98 82#TODO open2() forks off a child and I have not been able to locate
83#a mechanism for reaping dead children so they don't become zombies
90115979 84#CONFIRMED there is no reaping of children being done, find a safe
85#way to do it
7efea51f 86sub _open2_for {
87 my $self = shift;
88 my ($foreign_stdin, $foreign_stdout, $pid) = $self->_start_perl(@_);
fbd3b8ec 89 my $to_send = $self->fatnode_text;
5d59cb98 90 log_debug { my $len = length($to_send); "Sending contents of fat node to remote node; size is '$len' characters" };
fbd3b8ec 91 Object::Remote->current_loop
92 ->watch_io(
93 handle => $foreign_stdin,
94 on_write_ready => sub {
9031635d 95 my $len = syswrite($foreign_stdin, $to_send, 32768);
fbd3b8ec 96 if (defined $len) {
97 substr($to_send, 0, $len) = '';
98 }
99 # if the stdin went away, we'll never get Shere
100 # so it's not a big deal to simply give up on !defined
101 if (!defined($len) or 0 == length($to_send)) {
5d59cb98 102 log_trace { "Got EOF or error when writing fatnode data to filehandle, unwatching it" };
fbd3b8ec 103 Object::Remote->current_loop
104 ->unwatch_io(
105 handle => $foreign_stdin,
106 on_write_ready => 1
107 );
5d59cb98 108 } else {
109 log_trace { "Sent $len bytes of fatnode data to remote side" };
fbd3b8ec 110 }
111 }
112 );
a9fdb55e 113 return ($foreign_stdin, $foreign_stdout, $pid);
114}
115
b1cbd5be 116sub fatnode_text {
117 my ($self) = @_;
118 require Object::Remote::FatNode;
119 my $text = '';
120 $text .= 'BEGIN { $ENV{OBJECT_REMOTE_DEBUG} = 1 }'."\n"
121 if $ENV{OBJECT_REMOTE_DEBUG};
122 $text .= <<'END';
123$INC{'Object/Remote/FatNode.pm'} = __FILE__;
124$Object::Remote::FatNode::DATA = <<'ENDFAT';
125END
fbd3b8ec 126 $text .= do { no warnings 'once'; $Object::Remote::FatNode::DATA };
b1cbd5be 127 $text .= "ENDFAT\n";
128 $text .= <<'END';
129eval $Object::Remote::FatNode::DATA;
1a2d795f 130die $@ if $@;
b1cbd5be 131END
132 $text .= "__END__\n";
133 return $text;
134}
135
a9fdb55e 1361;