add progress indications
[spider.git] / perl / DXCommandmode.pm
index 4212d968b236aaf408288bd09a55930d0a102b8b..36b3ea3e27220825440d796d40343d8b0692edb5 100644 (file)
@@ -43,7 +43,7 @@ use JSON;
 use Time::HiRes qw(gettimeofday tv_interval);
 
 use Mojo::IOLoop;
-use Mojo::IOLoop::ForkCall;
+use Mojo::IOLoop::Subprocess;
 use Mojo::UserAgent;
 
 use strict;
@@ -490,7 +490,7 @@ sub send_ans
 }
 
 # 
-# this is the thing that runs the command, it is done like this for the 
+# this is the thing that preps for running the command, it is done like this for the 
 # benefit of remote command execution
 #
 
@@ -544,8 +544,17 @@ sub run_cmd
                        if ($package && $self->can("${package}::handle")) {
                                no strict 'refs';
                                dbg("cmd: package $package") if isdbg('command');
+                               if (isdbg('progress')) {
+                                       my $s = "CMD: '$cmd' by $call ip $self->{hostname}";
+                               }
+                               my $t0 = [gettimeofday];
                                eval { @ans = &{"${package}::handle"}($self, $args) };
                                return (DXDebug::shortmess($@)) if $@;
+                               if (isdbg('progress')) {
+                                       my $msecs = _diffms($t0);
+                                       my $s = "CMD: '$cmd' by $call ip: $self->{hostname} ${msecs}mS";
+                                       dbg($s);
+                               }
                        } else {
                                dbg("cmd: $package not present") if isdbg('command');
                                return $self->_error_out('e1');
@@ -1265,22 +1274,6 @@ sub send_motd
        $self->send_file($motd) if -e $motd;
 }
 
-sub _diffms
-{
-       return unless isdbg('chan');
-       my $call = shift;
-       my $line = shift;
-       my $ta = shift;
-       my $tb = shift || [gettimeofday];
-
-       my $a = int($ta->[0] * 1000) + int($ta->[1] / 1000); 
-       my $b = int($tb->[0] * 1000) + int($tb->[1] / 1000);
-       my $msecs = $b - $a;
-
-       my $s = "forkcall stats: $call '$line' ";
-       $s .= "${msecs}mS";
-       dbg($s);
-}
 
 # Punt off a long running command into a separate process
 #
@@ -1309,20 +1302,32 @@ sub spawn_cmd
        my $t0 = [gettimeofday];
 
        no strict 'refs';
-               
-       my $fc = Mojo::IOLoop::ForkCall->new;
-       $fc->serializer(\&encode_json);
-       $fc->deserializer(\&decode_json);
+
+       # just behave normally if something has set the "one-shot" _nospawn in the channel
+       return ($cmdref->(@$args)) if $self->{_nospawn};
+       
+       my $fc = Mojo::IOLoop::Subprocess->new;
+#      $fc->serializer(\&encode_json);
+#      $fc->deserializer(\&decode_json);
        $fc->run(
-                        sub {my @args = @_; my @res = $cmdref->(@args); return @res},
-                        $args,
+                        sub {
+                                my $subpro = shift;
+                                if (isdbg('spawn_cmd')) {
+                                        my $s = "line: $line";
+                                        $s .= ", args: " . join(', ', @$args) if $args && @$args;
+                                }
+                                my @res = $cmdref->(@$args);
+#                               diffms("rcmd from $call 1", $line, $t0, scalar @res) if isdbg('chan');
+                                return @res;
+                        },
+#                       $args,
                         sub {
                                 my ($fc, $err, @res) = @_; 
                                 my $dxchan = DXChannel::get($call);
                                 return unless $dxchan;
 
-                                if (defined $err) {
-                                        my $s = "DXCommand::spawn_cmd: call $call error $err";
+                                if ($err) {
+                                        my $s = "DXProt::spawn_cmd: call $call error $err";
                                         dbg($s) if isdbg('chan');
                                         $dxchan->send($s);
                                         return;
@@ -1338,7 +1343,7 @@ sub spawn_cmd
                                                 $dxchan->send(@res);
                                         }
                                 }
-                                _diffms($call, $line, $t0);
+                                diffms("by $call", $line, $t0, scalar @res) if isdbg('progress');
                         });
        
        return @out;