Commit | Line | Data |
a9fdb55e |
1 | package Object::Remote::Role::Connector::PerlInterpreter; |
2 | |
3 | use IPC::Open2; |
4c8c83d7 |
4 | use IO::Handle; |
a9fdb55e |
5 | use Object::Remote::ModuleSender; |
6 | use Object::Remote::Handle; |
fbd3b8ec |
7 | use Object::Remote::Future; |
9031635d |
8 | use Object::Remote::Logging qw( :log :dlog ); |
18e789ab |
9 | use Scalar::Util qw(blessed); |
a9fdb55e |
10 | use Moo::Role; |
11 | |
12 | with 'Object::Remote::Role::Connector'; |
13 | |
03f41c0e |
14 | has module_sender => (is => 'lazy'); |
15 | |
16 | sub _build_module_sender { |
18e789ab |
17 | my ($hook) = |
18 | grep {blessed($_) && $_->isa('Object::Remote::ModuleLoader::Hook') } |
19 | @INC; |
03f41c0e |
20 | return $hook ? $hook->sender : Object::Remote::ModuleSender->new; |
21 | } |
22 | |
498c4ad5 |
23 | has perl_command => (is => 'lazy'); |
24 | |
9031635d |
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', '-' ] } |
498c4ad5 |
32 | |
03f41c0e |
33 | around connect => sub { |
34 | my ($orig, $self) = (shift, shift); |
fbd3b8ec |
35 | my $f = $self->$start::start($orig => @_); |
36 | return future { |
37 | $f->on_done(sub { |
38 | my ($conn) = $f->get; |
4a9fa1a5 |
39 | my $sub = $conn->remote_sub('Object::Remote::Logging::init_logging_forwarding'); |
40 | $sub->('Object::Remote::Logging', Object::Remote::Logging->arg_router); |
fbd3b8ec |
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; |
a9fdb55e |
51 | }; |
52 | |
498c4ad5 |
53 | sub final_perl_command { shift->perl_command } |
a9fdb55e |
54 | |
7efea51f |
55 | sub _start_perl { |
a9fdb55e |
56 | my $self = shift; |
9031635d |
57 | Dlog_debug { "invoking connection to perl interpreter using command line: $_" } @{$self->final_perl_command}; |
a9fdb55e |
58 | my $pid = open2( |
59 | my $foreign_stdout, |
60 | my $foreign_stdin, |
498c4ad5 |
61 | @{$self->final_perl_command}, |
a9fdb55e |
62 | ) or die "Failed to run perl at '$_[0]': $!"; |
9031635d |
63 | Dlog_trace { "Connection to remote side successful; remote stdin and stdout: $_" } [ $foreign_stdin, $foreign_stdout ]; |
7efea51f |
64 | return ($foreign_stdin, $foreign_stdout, $pid); |
65 | } |
66 | |
5d59cb98 |
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 |
7efea51f |
69 | sub _open2_for { |
70 | my $self = shift; |
71 | my ($foreign_stdin, $foreign_stdout, $pid) = $self->_start_perl(@_); |
fbd3b8ec |
72 | my $to_send = $self->fatnode_text; |
5d59cb98 |
73 | log_debug { my $len = length($to_send); "Sending contents of fat node to remote node; size is '$len' characters" }; |
fbd3b8ec |
74 | Object::Remote->current_loop |
75 | ->watch_io( |
76 | handle => $foreign_stdin, |
77 | on_write_ready => sub { |
9031635d |
78 | my $len = syswrite($foreign_stdin, $to_send, 32768); |
fbd3b8ec |
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)) { |
5d59cb98 |
85 | log_trace { "Got EOF or error when writing fatnode data to filehandle, unwatching it" }; |
fbd3b8ec |
86 | Object::Remote->current_loop |
87 | ->unwatch_io( |
88 | handle => $foreign_stdin, |
89 | on_write_ready => 1 |
90 | ); |
5d59cb98 |
91 | } else { |
92 | log_trace { "Sent $len bytes of fatnode data to remote side" }; |
fbd3b8ec |
93 | } |
94 | } |
95 | ); |
a9fdb55e |
96 | return ($foreign_stdin, $foreign_stdout, $pid); |
97 | } |
98 | |
b1cbd5be |
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 |
fbd3b8ec |
109 | $text .= do { no warnings 'once'; $Object::Remote::FatNode::DATA }; |
b1cbd5be |
110 | $text .= "ENDFAT\n"; |
111 | $text .= <<'END'; |
112 | eval $Object::Remote::FatNode::DATA; |
1a2d795f |
113 | die $@ if $@; |
b1cbd5be |
114 | END |
115 | $text .= "__END__\n"; |
116 | return $text; |
117 | } |
118 | |
a9fdb55e |
119 | 1; |