2 # Batch EXecutor 2.0 -- Run Queued Jobs
3 # (c) 2011 Martin Mares <mj@ucw.cz>
15 "q|queue=s" => \$queue_name,
17 Usage: brun [<options>] [[!]<machine-or-class> ...]
20 -q, --queue=<name> Run jobs in the given queue
25 `ping -c1 -n $mach >/dev/null 2>/dev/null`;
30 my ($job, $queue, $mach) = @_;
31 # FIXME: rsyncing, rsync-only jobs
34 my $tmp = $queue->temp_file($mach, $job->{'ID'});
35 open T, '>', $tmp or die;
36 if (defined $BEX::Config::job_prolog) {
37 open P, $BEX::Config::job_prolog or return "Cannot open prolog: $!";
38 while (<P>) { print T; }
41 print T "#!/bin/sh\n";
43 print T "# BEX job ", $job->{'ID'}, "\n";
44 print T $job->{'body'};
45 if (defined $BEX::Config::job_epilog) {
46 open E, $BEX::Config::job_epilog or return "Cannot open epilog: $!";
47 while (<E>) { print T; }
52 my $cmd = 't=$(mktemp -t bex-XXXXXXXX) && cat >$t && chmod u+x $t && echo $t';
53 my $rtmp = `ssh <$tmp $mach '$cmd'`;
54 !$? && defined($rtmp) && $rtmp ne '' or return "Transfer failed";
57 system 'ssh', '-t', $mach, "$rtmp ; e=\$? ; rm -f $rtmp ; exit \$e";
65 my @machines = BEX::Config::parse_machine_list(@ARGV ? @ARGV : '*');
66 my $queue = BEX::Queue->new($queue_name);
67 for my $mach (@machines) {
68 my @q = $queue->scan($mach) or next;
71 my $job = BEX::Job->new_from_file($queue->job_file($jid));
75 print "### Running $jid (", $job->attr('Subject'), ") on $mach ###\n";
76 $ping //= ping_machine($mach);
81 $s = run_job($job, $queue, $mach);
84 BEX::log("$mach $jid $s");
87 $queue->remove($mach, $jid);
90 $stat->{'Status'} = $s;
91 $queue->write_job_status($mach, $jid, $stat);