X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=scpubgit%2FObject-Remote.git;a=blobdiff_plain;f=lib%2FObject%2FRemote%2FRole%2FConnector%2FPerlInterpreter.pm;h=b3e82d0ff60c8eee724c7003f683e1deda9aa446;hp=810c822e5a89687c4e8640d1aa2f18e4d9fd7f94;hb=b7a853b30e7397854a813f6a5878e50703a20079;hpb=7790ca36241acfa9379cb2b89b68c9f4b0da2371 diff --git a/lib/Object/Remote/Role/Connector/PerlInterpreter.pm b/lib/Object/Remote/Role/Connector/PerlInterpreter.pm index 810c822..b3e82d0 100644 --- a/lib/Object/Remote/Role/Connector/PerlInterpreter.pm +++ b/lib/Object/Remote/Role/Connector/PerlInterpreter.pm @@ -3,32 +3,22 @@ package Object::Remote::Role::Connector::PerlInterpreter; use IPC::Open2; use IPC::Open3; use IO::Handle; +use Object::Remote::Logging qw( :log :dlog ); use Object::Remote::ModuleSender; use Object::Remote::Handle; use Object::Remote::Future; -use Object::Remote::Logging qw( :log :dlog ); -use Scalar::Util qw(blessed); -use POSIX ":sys_wait_h"; +use Scalar::Util qw(blessed weaken); +use POSIX; use Moo::Role; +use Symbol; with 'Object::Remote::Role::Connector'; -#TODO ugh breaks some of the stuff in System::Introspector::Util by -#screwing with status value of child -BEGIN { - $SIG{CHLD} = sub { - my $kid; - do { - $kid = waitpid(-1, WNOHANG); - } while $kid > 0; - } -} - has module_sender => (is => 'lazy'); + #if no child_stderr file handle is specified then stderr #of the child will be connected to stderr of the parent -has stderr => ( is => 'rw', default => sub { \*STDERR } ); -#has stderr => ( is => 'rw' ); +has stderr => ( is => 'rw', default => sub { undef } ); sub _build_module_sender { my ($hook) = @@ -38,15 +28,16 @@ sub _build_module_sender { } has perl_command => (is => 'lazy'); +has watchdog_timeout => ( is => 'ro', required => 1, default => sub { 0 } ); #TODO convert nice value into optional feature enabled by #setting value of attribute #ulimit of ~500 megs of v-ram #TODO only works with ssh with quotes but only works locally #with out quotes -#sub _build_perl_command { [ 'sh', '-c', '"ulimit -v 80000; nice -n 15 perl -"' ] } -sub _build_perl_command { [ 'sh', '-c', '"ulimit -v 200000; nice -n 15 perl -"' ] } +sub _build_perl_command {[ 'sh -c "ulimit -v 200000; nice -n 15 perl -"' ] } #sub _build_perl_command { [ 'perl', '-' ] } +#sub _build_perl_command { [ 'cat' ] } around connect => sub { my ($orig, $self) = (shift, shift); @@ -54,6 +45,7 @@ around connect => sub { return future { $f->on_done(sub { my ($conn) = $f->get; + $self->_setup_watchdog_reset($conn); my $sub = $conn->remote_sub('Object::Remote::Logging::init_logging_forwarding'); $sub->('Object::Remote::Logging', Object::Remote::Logging->arg_router); Object::Remote::Handle->new( @@ -76,12 +68,17 @@ sub _start_perl { my $foreign_stderr; Dlog_debug { "invoking connection to perl interpreter using command line: $_" } @{$self->final_perl_command}; - - use Symbol; - + if (defined($given_stderr)) { + #if the stderr data goes to an existing file handle + #an need an anonymous file handle is required + #as the other half of a pipe style file handle pair + #so the file handles can go into the run loop $foreign_stderr = gensym(); } else { + #if no file handle has been specified + #for the child's stderr then connect + #the child stderr to the parent stderr $foreign_stderr = ">&STDERR"; } @@ -102,7 +99,7 @@ sub _start_perl { on_read_ready => sub { my $buf = ''; my $len = sysread($foreign_stderr, $buf, 32768); - if (!defined($len) or $len == 0) { + if ((!defined($len) && $! != EAGAIN) or $len == 0) { log_trace { "Got EOF or error on child stderr, removing from watcher" }; $self->stderr(undef); Object::Remote->current_loop @@ -118,34 +115,9 @@ sub _start_perl { ); } - #TODO open2() dupes the child stderr into the calling - #process stderr which means if this process exits the - #child is still attached to the shell - using open3() - #and having the run loop manage the stderr means this - #won't happen BUT if the run loop just sends the remote - #stderr data to the local stderr the logs will interleave - #for sure - a simple test would be to use open3() and just - #close the remote stderr and see what happens - a longer - #term solution would be for Object::Remote to offer a feature - #where the user of a connection species a destination for output - #either a file name or their own file handle and the node output - #is dumped to it -# my $pid = open2( -# my $foreign_stdout, -# my $foreign_stdin, -# @{$self->final_perl_command}, -# ) or die "Failed to run perl at '$_[0]': $!"; -# -# Dlog_trace { "Connection to remote side successful; remote stdin and stdout: $_" } [ $foreign_stdin, $foreign_stdout ]; - - - return ($foreign_stdin, $foreign_stdout, $pid); + return ($foreign_stdin, $foreign_stdout, $pid); } -#TODO open2() forks off a child and I have not been able to locate -#a mechanism for reaping dead children so they don't become zombies -#CONFIRMED there is no reaping of children being done, find a safe -#way to do it sub _open2_for { my $self = shift; my ($foreign_stdin, $foreign_stdout, $pid) = $self->_start_perl(@_); @@ -161,7 +133,7 @@ sub _open2_for { } # if the stdin went away, we'll never get Shere # so it's not a big deal to simply give up on !defined - if (!defined($len) or 0 == length($to_send)) { + if ((!defined($len) && $! != EAGAIN) or 0 == length($to_send)) { log_trace { "Got EOF or error when writing fatnode data to filehandle, unwatching it" }; Object::Remote->current_loop ->unwatch_io( @@ -176,10 +148,47 @@ sub _open2_for { return ($foreign_stdin, $foreign_stdout, $pid); } +sub _setup_watchdog_reset { + my ($self, $conn) = @_; + my $timer_id; + + return unless $self->watchdog_timeout; + + Dlog_trace { "Creating Watchdog management timer for connection id $_" } $conn->_id; + + weaken($conn); + + $timer_id = Object::Remote->current_loop->watch_time( + every => $self->watchdog_timeout / 3, + code => sub { + unless(defined($conn)) { + log_trace { "Weak reference to connection in Watchdog was lost, terminating update timer $timer_id" }; + Object::Remote->current_loop->unwatch_time($timer_id); + return; + } + + Dlog_trace { "Reseting Watchdog for connection id $_" } $conn->_id; + #we do not want to block in the run loop so send the + #update off and ignore any result, we don't need it + #anyway + $conn->send_class_call(0, 'Object::Remote::WatchDog', 'reset'); + } + ); +} + sub fatnode_text { my ($self) = @_; - require Object::Remote::FatNode; my $text = ''; + + require Object::Remote::FatNode; + + if (defined($self->watchdog_timeout)) { + $text = "my \$WATCHDOG_TIMEOUT = '" . $self->watchdog_timeout . "';\n"; + $text .= "alarm(\$WATCHDOG_TIMEOUT);\n"; + } else { + $text = "my \$WATCHDOG_TIMEOUT = undef;\n"; + } + $text .= 'BEGIN { $ENV{OBJECT_REMOTE_DEBUG} = 1 }'."\n" if $ENV{OBJECT_REMOTE_DEBUG}; $text .= <<'END'; @@ -192,6 +201,7 @@ END eval $Object::Remote::FatNode::DATA; die $@ if $@; END + $text .= "__END__\n"; return $text; }