2 # Batch EXecutor 3.0 -- Show Queued Jobs
3 # (c) 2011-2012 Martin Mares <mj@ucw.cz>
24 Usage: bex queue [<options and actions>] [[!]<machine-or-class> ...]
27 --by-job Show jobs sorted by job ID (default)
28 -h, --by-host Show jobs sorted by host
29 --rm Remove jobs from the queue
30 --move-to=<queue> Move jobs to a different queue
33 -f, --filenames Show filenames of jobs and log files
34 -j, --job=<id> Act on the specified job (default: on all)
35 -q, --queue=<name> Act on the given queue
36 -s, --summary Show only a summary
37 -w, --why[=<lines>] In case of failed jobs, display last few lines of output
42 Getopt::Long::Configure("bundling");
44 "by-job!" => \$op_by_job,
45 "h|by-host!" => \$op_by_host,
47 "move-to=s" => \$op_move_to,
48 "f|filenames!" => \$filenames,
49 "j|job=s" => \$given_job,
50 "q|queue=s" => \$queue_name,
51 "s|summary!" => \$summary,
54 ) or die "Try `bex queue --help' for more information.\n";
56 my @machines = BEX::Config::parse_machine_list(@ARGV ? @ARGV : '*');
57 my $queue = BEX::Queue->new($queue_name);
62 for my $m (@machines) {
63 for my $j ($queue->scan($m)) {
64 if (defined $given_job) {
65 next if $j ne $given_job;
67 push @{$jobs{$j}}, $m;
68 push @{$machs{$m}}, $j;
76 my $ops = 0 + defined($op_by_host) + defined($op_by_job) + defined($op_rm) + defined($op_move_to);
77 if ($ops > 1) { die "Multiple actions are not allowed\n"; }
79 if ($op_rm) { do_rm(); }
80 elsif (defined $op_move_to) { do_move_to(); }
91 for my $m (keys %machs) {
92 $mach_locked{$m} = $queue->is_locked($m, undef);
93 for my $j (@{$machs{$m}}) {
94 my $st = $queue->read_job_status($m, $j);
95 my $s = $st->{"Status"} // "UNKNOWN";
96 $cnt_by_job{$j}{$s}++;
97 $cnt_by_mach{$m}{$s}++;
100 $why{$m}{$j} .= "\t\t== Job file: " . $queue->queue_file($m, $j) . "\n";
102 if (defined($st->{'Time'}) && defined($st->{'Status'})) {
103 $stat{$m}{$j} = ' [' . $st->{'Status'} . ' on ' .
104 POSIX::strftime('%Y-%m-%d', localtime $st->{'Time'}) . ']';
105 if (defined($why) && $st->{'Status'} eq 'FAILED') {
106 my $lines = $why ? $why : 3;
107 my $log = $queue->log_file($m, $j);
109 $why{$m}{$j} .= join("", map { "\t\t>> $_" } `tail -n$lines $log`);
115 if ($mach_locked{$m} || $queue->is_locked($m, $j)) {
116 $stat{$m}{$j} .= ' [LOCKED]';
121 if ($queue->is_locked(undef, undef)) {
122 print "### Queue lock present\n\n";
127 for my $m (sort keys %cnt_by_mach) {
128 print "$m: ", join(" ", map { "$_:" . $cnt_by_mach{$m}{$_} } sort keys %{$cnt_by_mach{$m}}), "\n";
131 for my $j (sort keys %cnt_by_job) {
132 print $queue->job_name($j), ": ", join(" ", map { "$_:" . $cnt_by_job{$j}{$_} } sort keys %{$cnt_by_job{$j}}), "\n";
137 for my $m (sort keys %machs) {
138 print "$m", ($mach_locked{$m} ? ' [LOCKED]' : ''), "\n";
139 for my $j (@{$machs{$m}}) {
140 print "\t" . $queue->job_name($j) . $stat{$m}{$j}, "\n";
145 for my $j (sort keys %jobs) {
146 print $queue->job_name($j), "\n";
147 for my $m (sort @{$jobs{$j}}) {
148 print "\t$m", $stat{$m}{$j}, "\n";
159 for my $m (sort keys %machs) {
160 for my $j (sort @{$machs{$m}}) {
161 if (!$queue->lock($m, $j)) {
162 print STDERR "Cannot remove $m:", $queue->job_name($j), ", it is locked\n";
165 $queue->update_job_status($m, $j, 'REMOVED');
166 $queue->remove($m, $j);
167 print "Removed $m:", $queue->job_name($j), "\n";
178 my $dest = BEX::Queue->new($op_move_to);
179 $dest->{'Name'} ne $queue->{'Name'} or die "Moving to the same queue is not permitted\n";
180 for my $j (sort keys %jobs) {
181 my $job = BEX::Job->new_from_file($queue->job_file($j));
182 for my $m (sort @{$jobs{$j}}) {
183 if (!$queue->lock($m, $j)) {
184 print STDERR "Cannot move $m:", $queue->job_name($j), ", it is locked\n";
187 my $enq = $dest->enqueue($m, $job);
189 $dest->update_job_status($m, $job->id, 'NEW', 'Moved to this queue');
191 $dest->log($m, $job->id, 'REQUEUE', 'Moved to this queue');
193 $queue->update_job_status($m, $job->id, 'REMOVED', 'Moved from this queue');
194 $queue->remove($m, $j);
195 print "Moved $m:", $dest->job_name($j);
196 print " (already queued)" if !$enq;