tidy debuging in DXCron
[spider.git] / perl / DXCron.pm
index f9a4f07ea12585ab62b30389b1c4734d341fa2eb..c011d7b3d3e3df0740bfbdc3662edd6646b95d30 100644 (file)
@@ -15,6 +15,8 @@ use DXDebug;
 use IO::File;
 use DXLog;
 
+use Mojo::IOLoop::Subprocess;
+
 use strict;
 
 use vars qw{@crontab @lcrontab @scrontab $mtime $lasttime $lastmin};
@@ -60,7 +62,7 @@ sub cread
        my $line = 0;
        my @out;
 
-       dbg("cron: reading $fn\n") if isdbg('cron');
+       dbg("DXCron::cread reading $fn\n") if isdbg('cron');
        open($fh, $fn) or confess("cron: can't open $fn $!");
        while (<$fh>) {
                $line++;
@@ -79,9 +81,9 @@ sub cread
                if (!$err) {
                        $ref->{cmd} = $cmd;
                        push @out, $ref;
-                       dbg("cron: adding $_\n") if isdbg('cron');
+                       dbg("DXCron::cread: adding $_\n") if isdbg('cron');
                } else {
-                       dbg("cron: error on line $line '$_'\n") if isdbg('cron');
+                       dbg("DXCron::cread: error on line $line '$_'\n") if isdbg('cron');
                }
        }
        close($fh);
@@ -244,13 +246,13 @@ sub spawn
 {
        my $line = shift;
 
-       my $fc = Mojo::IOLoop::ForkCall->new;
+       dbg("DXCron::spawn: $line") if isdbg("cron");
+       my $fc = Mojo::IOLoop::Subprocess->new();
        $fc->run(
                         sub {my @res = `$line`; return @res},
-                        [],
                         sub {
                                 my ($fc, $err, @res) = @_; 
-                                if (defined $err) {
+                                if ($err) {
                                         my $s = "DXCron::spawn: error $err";
                                         dbg($s);
                                         return;
@@ -263,6 +265,33 @@ sub spawn
                        );
 }
 
+sub spawn_cmd
+{
+       my $line = shift;
+
+       dbg("DXCron::spawn_cmd run: $line") if isdbg('cron');
+       my $fc = Mojo::IOLoop::Subprocess->new();
+       $fc->run(
+                        sub {
+                                $main::me->{_nospawn} = 1;
+                                my @res = $main::me->run_cmd($line);
+                                delete $main::me->{_nospawn};
+                                return @res;
+                        },
+                        sub {
+                                my ($fc, $err, @res) = @_; 
+                                if ($err) {
+                                        my $s = "DXCron::spawn_cmd: error $err";
+                                        dbg($s);
+                                }
+                                for (@res) {
+                                        chomp;
+                                        dbg("DXCron::spawn_cmd: $_") if isdbg("cron");
+                                }
+                        }
+                       );
+}
+
 # do an rcmd to another cluster from the crontab
 sub rcmd
 {
@@ -280,12 +309,13 @@ sub rcmd
 sub run_cmd
 {
        my $line = shift;
-       my @in = DXCommandmode::run_cmd($main::me, $line);
-       dbg("cmd run: $line") if isdbg('cron');
+       my @in = $main::me->run_cmd($line);
+       dbg("DXCron::run_cmd: $line") if isdbg('cron');
        for (@in) {
                s/\s*$//og;
-               dbg("cmd out: $_") if isdbg('cron');
+               dbg("DXCron::cmd out: $_") if isdbg('cron');
        }
 }
+
 1;
 __END__