got all general logging done, start of adding ids to objects and incorporating ids...
[scpubgit/Object-Remote.git] / lib / Object / Remote / Role / Connector / PerlInterpreter.pm
1 package Object::Remote::Role::Connector::PerlInterpreter;
2
3 use IPC::Open2;
4 use IO::Handle;
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);
10 use Moo::Role;
11
12 with 'Object::Remote::Role::Connector';
13
14 has module_sender => (is => 'lazy');
15
16 sub _build_module_sender {
17   my ($hook) =
18     grep {blessed($_) && $_->isa('Object::Remote::ModuleLoader::Hook') }
19       @INC;
20   return $hook ? $hook->sender : Object::Remote::ModuleSender->new;
21 }
22
23 has perl_command => (is => 'lazy');
24
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
29 #with out quotes
30 sub _build_perl_command { [ 'sh', '-c', '"ulimit -v 500000; nice -n 15 perl -"' ] }
31 #sub _build_perl_command { [ 'perl', '-' ] }
32
33 around connect => sub {
34   my ($orig, $self) = (shift, shift);
35   my $f = $self->$start::start($orig => @_);
36   return future {
37     $f->on_done(sub {
38       my ($conn) = $f->get;
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(
42         connection => $conn,
43         class => 'Object::Remote::ModuleLoader',
44         args => { module_sender => $self->module_sender }
45       )->disarm_free;
46       require Object::Remote::Prompt;
47       Object::Remote::Prompt::maybe_set_prompt_command_on($conn);
48     });
49     $f;
50   } 2;
51 };
52
53 sub final_perl_command { shift->perl_command }
54
55 sub _start_perl {
56   my $self = shift;
57   Dlog_debug { "invoking connection to perl interpreter using command line: $_" } @{$self->final_perl_command};
58   my $pid = open2(
59     my $foreign_stdout,
60     my $foreign_stdin,
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);
65 }
66
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
69 sub _open2_for {
70   my $self = shift;
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
75                 ->watch_io(
76                     handle => $foreign_stdin,
77                     on_write_ready => sub {
78                       my $len = syswrite($foreign_stdin, $to_send, 32768);
79                       if (defined $len) {
80                         substr($to_send, 0, $len) = '';
81                       }
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
87                                       ->unwatch_io(
88                                           handle => $foreign_stdin,
89                                           on_write_ready => 1
90                                         );
91                       } else {
92                           log_trace { "Sent $len bytes of fatnode data to remote side" };
93                       }
94                     }
95                   );
96   return ($foreign_stdin, $foreign_stdout, $pid);
97 }
98
99 sub fatnode_text {
100   my ($self) = @_;
101   require Object::Remote::FatNode;
102   my $text = '';
103   $text .= 'BEGIN { $ENV{OBJECT_REMOTE_DEBUG} = 1 }'."\n"
104     if $ENV{OBJECT_REMOTE_DEBUG};
105   $text .= <<'END';
106 $INC{'Object/Remote/FatNode.pm'} = __FILE__;
107 $Object::Remote::FatNode::DATA = <<'ENDFAT';
108 END
109   $text .= do { no warnings 'once'; $Object::Remote::FatNode::DATA };
110   $text .= "ENDFAT\n";
111   $text .= <<'END';
112 eval $Object::Remote::FatNode::DATA;
113 die $@ if $@;
114 END
115   $text .= "__END__\n";
116   return $text;
117 }
118
119 1;