1 package Object::Remote::Role::Connector::PerlInterpreter;
6 use Object::Remote::ModuleSender;
7 use Object::Remote::Handle;
8 use Object::Remote::Future;
9 use Object::Remote::Logging qw( :log :dlog );
10 use Scalar::Util qw(blessed);
13 with 'Object::Remote::Role::Connector';
15 has module_sender => (is => 'lazy');
17 sub _build_module_sender {
19 grep {blessed($_) && $_->isa('Object::Remote::ModuleLoader::Hook') }
21 return $hook ? $hook->sender : Object::Remote::ModuleSender->new;
24 has perl_command => (is => 'lazy');
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
31 sub _build_perl_command { [ 'sh', '-c', '"ulimit -v 500000; nice -n 15 perl -"' ] }
32 #sub _build_perl_command { [ 'perl', '-' ] }
34 around connect => sub {
35 my ($orig, $self) = (shift, shift);
36 my $f = $self->$start::start($orig => @_);
40 my $sub = $conn->remote_sub('Object::Remote::Logging::init_logging_forwarding');
41 $sub->('Object::Remote::Logging', Object::Remote::Logging->arg_router);
42 Object::Remote::Handle->new(
44 class => 'Object::Remote::ModuleLoader',
45 args => { module_sender => $self->module_sender }
47 require Object::Remote::Prompt;
48 Object::Remote::Prompt::maybe_set_prompt_command_on($conn);
54 sub final_perl_command { shift->perl_command }
58 Dlog_debug { "invoking connection to perl interpreter using command line: $_" } @{$self->final_perl_command};
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
75 @{$self->final_perl_command},
76 ) or die "Failed to run perl at '$_[0]': $!";
78 Dlog_trace { "Connection to remote side successful; remote stdin and stdout: $_" } [ $foreign_stdin, $foreign_stdout ];
79 return ($foreign_stdin, $foreign_stdout, $pid);
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
84 #CONFIRMED there is no reaping of children being done, find a safe
88 my ($foreign_stdin, $foreign_stdout, $pid) = $self->_start_perl(@_);
89 my $to_send = $self->fatnode_text;
90 log_debug { my $len = length($to_send); "Sending contents of fat node to remote node; size is '$len' characters" };
91 Object::Remote->current_loop
93 handle => $foreign_stdin,
94 on_write_ready => sub {
95 my $len = syswrite($foreign_stdin, $to_send, 32768);
97 substr($to_send, 0, $len) = '';
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)) {
102 log_trace { "Got EOF or error when writing fatnode data to filehandle, unwatching it" };
103 Object::Remote->current_loop
105 handle => $foreign_stdin,
109 log_trace { "Sent $len bytes of fatnode data to remote side" };
113 return ($foreign_stdin, $foreign_stdout, $pid);
118 require Object::Remote::FatNode;
120 $text .= 'BEGIN { $ENV{OBJECT_REMOTE_DEBUG} = 1 }'."\n"
121 if $ENV{OBJECT_REMOTE_DEBUG};
123 $INC{'Object/Remote/FatNode.pm'} = __FILE__;
124 $Object::Remote::FatNode::DATA = <<'ENDFAT';
126 $text .= do { no warnings 'once'; $Object::Remote::FatNode::DATA };
129 eval $Object::Remote::FatNode::DATA;
132 $text .= "__END__\n";