2 # Batch EXecutor 3.0 -- Show Queued Jobs
3 # (c) 2011-2012 Martin Mares <mj@ucw.cz>
26 Usage: bex queue [<options and actions>] [[!]<machine-or-class> ...]
29 --by-job Show jobs sorted by job ID (default)
30 -h, --by-host Show jobs sorted by host
31 --rm Remove jobs from the queue
32 --move-to=<queue> Move jobs to a different queue
35 -a, --attn Show only jobs needing attention (e.g., failures)
36 -f, --filenames Show filenames of jobs and log files
37 -j, --job=<id> Act on the specified job (default: on all)
38 -q, --queue=<name> Act on the given queue
39 -s, --summary Show only a summary
40 -S, --state=<regex> Act only on jobs whose state matches the given regex
41 -w, --why[=<lines>] In case of failed jobs, display last few lines of output
46 Getopt::Long::Configure("bundling");
48 "by-job!" => \$op_by_job,
49 "h|by-host!" => \$op_by_host,
51 "move-to=s" => \$op_move_to,
52 "a|attn!" => \$attention,
53 "f|filenames!" => \$filenames,
54 "j|job=s" => \$given_job,
55 "q|queue=s" => \$queue_name,
56 "s|summary!" => \$summary,
57 "S|state=s" => \$state_regex,
60 ) or die "Try `bex queue --help' for more information.\n";
62 my @machines = BEX::Config::parse_machine_list(@ARGV ? @ARGV : '*');
63 my $queue = BEX::Queue->new($queue_name);
66 my %status_cache = ();
69 $status_cache{$m}{$j} or $status_cache{$m}{$j} = $queue->read_job_status($m, $j);
70 return $status_cache{$m}{$j};
74 return get_status($m, $j)->{'Status'} // 'UNKNOWN';
80 for my $m (@machines) {
81 for my $j ($queue->scan($m)) {
82 if (defined $given_job) {
83 next if $j ne $given_job;
85 if (defined $attention) {
86 next if get_stat($m, $j) =~ m{^(NEW|NOPING)$};
88 if (defined $state_regex) {
89 next unless get_stat($m, $j) =~ m{^($state_regex)$};
91 push @{$jobs{$j}}, $m;
92 push @{$machs{$m}}, $j;
100 my $ops = 0 + defined($op_by_host) + defined($op_by_job) + defined($op_rm) + defined($op_move_to);
101 if ($ops > 1) { die "Multiple actions are not allowed\n"; }
103 if ($op_rm) { do_rm(); }
104 elsif (defined $op_move_to) { do_move_to(); }
111 my %mach_locked = ();
113 my %cnt_by_mach = ();
115 for my $m (keys %machs) {
116 $mach_locked{$m} = $queue->is_locked($m, undef);
117 for my $j (@{$machs{$m}}) {
118 my $st = get_status($m, $j);
119 my $s = get_stat($m, $j);
120 $cnt_by_job{$j}{$s}++;
121 $cnt_by_mach{$m}{$s}++;
124 $why{$m}{$j} .= "\t\t== Job file: " . $queue->queue_file($m, $j) . "\n";
126 if (defined($st->{'Time'}) && defined($st->{'Status'})) {
127 $stat{$m}{$j} = ' [' . $st->{'Status'} . ' on ' .
128 POSIX::strftime('%Y-%m-%d', localtime $st->{'Time'}) . ']';
129 if (defined($why) && $st->{'Status'} eq 'FAILED') {
130 my $lines = $why ? $why : 3;
131 my $log = $queue->log_file($m, $j);
133 $why{$m}{$j} .= join("", map { "\t\t>> $_" } `tail -n$lines $log`);
139 if ($mach_locked{$m} || $queue->is_locked($m, $j)) {
140 $stat{$m}{$j} .= ' [LOCKED]';
145 if ($queue->is_locked(undef, undef)) {
146 print "### Queue lock present\n\n";
151 for my $m (sort keys %cnt_by_mach) {
152 print "$m: ", join(" ", map { "$_:" . $cnt_by_mach{$m}{$_} } sort keys %{$cnt_by_mach{$m}}), "\n";
155 for my $j (sort keys %cnt_by_job) {
156 print $queue->job_name($j), ": ", join(" ", map { "$_:" . $cnt_by_job{$j}{$_} } sort keys %{$cnt_by_job{$j}}), "\n";
161 for my $m (sort keys %machs) {
162 print "$m", ($mach_locked{$m} ? ' [LOCKED]' : ''), "\n";
163 for my $j (@{$machs{$m}}) {
164 print "\t" . $queue->job_name($j) . $stat{$m}{$j}, "\n";
169 for my $j (sort keys %jobs) {
170 print $queue->job_name($j), "\n";
171 for my $m (sort @{$jobs{$j}}) {
172 print "\t$m", $stat{$m}{$j}, "\n";
183 for my $m (sort keys %machs) {
184 for my $j (sort @{$machs{$m}}) {
185 if (!$queue->lock($m, $j)) {
186 print STDERR "Cannot remove $m:", $queue->job_name($j), ", it is locked\n";
189 $queue->update_job_status($m, $j, 'REMOVED');
190 $queue->remove($m, $j);
191 print "Removed $m:", $queue->job_name($j), "\n";
202 my $dest = BEX::Queue->new($op_move_to);
203 $dest->{'Name'} ne $queue->{'Name'} or die "Moving to the same queue is not permitted\n";
204 for my $j (sort keys %jobs) {
205 my $job = BEX::Job->new_from_file($queue->job_file($j));
206 for my $m (sort @{$jobs{$j}}) {
207 if (!$queue->lock($m, $j)) {
208 print STDERR "Cannot move $m:", $queue->job_name($j), ", it is locked\n";
211 my $enq = $dest->enqueue($m, $job);
213 $dest->update_job_status($m, $job->id, 'NEW', 'Moved to this queue');
215 $dest->log($m, $job->id, 'REQUEUE', 'Moved to this queue');
217 $queue->update_job_status($m, $job->id, 'REMOVED', 'Moved from this queue');
218 $queue->remove($m, $j);
219 print "Moved $m:", $dest->job_name($j);
220 print " (already queued)" if !$enq;