1 package Object::Remote::Role::Connector::PerlInterpreter;
5 use Object::Remote::ModuleSender;
6 use Object::Remote::Handle;
7 use Object::Remote::Future;
8 use Object::Remote::Logging qw( :log :dlog );
9 use Scalar::Util qw(blessed);
12 with 'Object::Remote::Role::Connector';
14 has module_sender => (is => 'lazy');
16 sub _build_module_sender {
18 grep {blessed($_) && $_->isa('Object::Remote::ModuleLoader::Hook') }
20 return $hook ? $hook->sender : Object::Remote::ModuleSender->new;
23 has perl_command => (is => 'lazy');
25 #TODO convert nice value into optional feature enabled by
26 #setting value of attribute
27 #ulimit of ~500 megs of v-ram
28 #TODO only works with ssh with quotes but only works locally
30 sub _build_perl_command { [ 'sh', '-c', '"ulimit -v 500000; nice -n 15 perl -"' ] }
31 #sub _build_perl_command { [ 'perl', '-' ] }
33 around connect => sub {
34 my ($orig, $self) = (shift, shift);
35 my $f = $self->$start::start($orig => @_);
39 my $sub = $conn->remote_sub('Object::Remote::Logging::init_logging_forwarding');
40 $sub->('Object::Remote::Logging', Object::Remote::Logging->arg_router);
41 Object::Remote::Handle->new(
43 class => 'Object::Remote::ModuleLoader',
44 args => { module_sender => $self->module_sender }
46 require Object::Remote::Prompt;
47 Object::Remote::Prompt::maybe_set_prompt_command_on($conn);
53 sub final_perl_command { shift->perl_command }
57 Dlog_debug { "invoking connection to perl interpreter using command line: $_" } @{$self->final_perl_command};
61 @{$self->final_perl_command},
62 ) or die "Failed to run perl at '$_[0]': $!";
63 Dlog_trace { "Connection to remote side successful; remote stdin and stdout: $_" } [ $foreign_stdin, $foreign_stdout ];
64 return ($foreign_stdin, $foreign_stdout, $pid);
67 #TODO open2() forks off a child and I have not been able to locate
68 #a mechanism for reaping dead children so they don't become zombies
71 my ($foreign_stdin, $foreign_stdout, $pid) = $self->_start_perl(@_);
72 my $to_send = $self->fatnode_text;
73 log_debug { my $len = length($to_send); "Sending contents of fat node to remote node; size is '$len' characters" };
74 Object::Remote->current_loop
76 handle => $foreign_stdin,
77 on_write_ready => sub {
78 my $len = syswrite($foreign_stdin, $to_send, 32768);
80 substr($to_send, 0, $len) = '';
82 # if the stdin went away, we'll never get Shere
83 # so it's not a big deal to simply give up on !defined
84 if (!defined($len) or 0 == length($to_send)) {
85 log_trace { "Got EOF or error when writing fatnode data to filehandle, unwatching it" };
86 Object::Remote->current_loop
88 handle => $foreign_stdin,
92 log_trace { "Sent $len bytes of fatnode data to remote side" };
96 return ($foreign_stdin, $foreign_stdout, $pid);
101 require Object::Remote::FatNode;
103 $text .= 'BEGIN { $ENV{OBJECT_REMOTE_DEBUG} = 1 }'."\n"
104 if $ENV{OBJECT_REMOTE_DEBUG};
106 $INC{'Object/Remote/FatNode.pm'} = __FILE__;
107 $Object::Remote::FatNode::DATA = <<'ENDFAT';
109 $text .= do { no warnings 'once'; $Object::Remote::FatNode::DATA };
112 eval $Object::Remote::FatNode::DATA;
115 $text .= "__END__\n";