2 # Batch EXecutor 2.0 -- Show Queued Jobs
3 # (c) 2011 Martin Mares <mj@ucw.cz>
22 "by-job!" => \$op_by_job,
23 "h|by-host!" => \$op_by_host,
25 "move-to=s" => \$op_move_to,
26 "j|job=s" => \$given_job,
27 "q|queue=s" => \$queue_name,
29 Usage: bq [<options and actions>] [[!]<machine-or-class> ...]
32 --by-job Show jobs sorted by job ID (default)
33 -h, --by-host Show jobs sorted by host
34 --rm Remove jobs from the queue
35 --move-to=<queue> Move jobs to a different queue
38 -j, --job=<id> Act on the specified job (default: on all)
39 -q, --queue=<name> Act on the given queue
42 my @machines = BEX::Config::parse_machine_list(@ARGV ? @ARGV : '*');
43 my $queue = BEX::Queue->new($queue_name);
48 for my $m (@machines) {
49 for my $j ($queue->scan($m)) {
50 if (defined $given_job) {
51 next if $j ne $given_job;
53 push @{$jobs{$j}}, $m;
54 push @{$machs{$m}}, $j;
62 my $ops = 0 + defined($op_by_host) + defined($op_by_job) + defined($op_rm) + defined($op_move_to);
63 if ($ops > 1) { die "Multiple actions are not allowed\n"; }
65 if ($op_rm) { do_rm(); }
66 elsif (defined $op_move_to) { do_move_to(); }
74 for my $m (keys %machs) {
75 $mach_locked{$m} = $queue->is_locked($m, undef);
76 for my $j (@{$machs{$m}}) {
77 my $st = $queue->read_job_status($m, $j);
78 if (defined($st->{'Time'}) && defined($st->{'Status'})) {
79 $stat{$m}{$j} = ' [' . $st->{'Status'} . ' on ' .
80 POSIX::strftime('%Y-%m-%d', localtime $st->{'Time'}) . ']';
84 if ($mach_locked{$m} || $queue->is_locked($m, $j)) {
85 $stat{$m}{$j} .= ' [LOCKED]';
90 if ($queue->is_locked(undef, undef)) {
91 print "### Queue lock present\n\n";
95 for my $m (sort keys %machs) {
96 print "$m", ($mach_locked{$m} ? ' [LOCKED]' : ''), "\n";
97 for my $j (@{$machs{$m}}) {
98 print "\t" . $queue->job_name($j) . $stat{$m}{$j}, "\n";
102 for my $j (sort keys %jobs) {
103 print $queue->job_name($j), "\n";
104 for my $m (sort @{$jobs{$j}}) {
105 print "\t$m", $stat{$m}{$j}, "\n";
114 for my $m (sort keys %machs) {
115 for my $j (sort @{$machs{$m}}) {
116 if (!$queue->lock($m, $j)) {
117 print STDERR "Cannot remove $m:", $queue->job_name($j), ", it is locked\n";
120 $queue->update_job_status($m, $j, 'REMOVED');
121 $queue->remove($m, $j);
122 print "Removed $m:", $queue->job_name($j), "\n";
133 my $dest = BEX::Queue->new($op_move_to);
134 $dest->{'Name'} ne $queue->{'Name'} or die "Moving to the same queue is not permitted\n";
135 for my $j (sort keys %jobs) {
136 my $job = BEX::Job->new_from_file($queue->job_file($j));
137 for my $m (sort @{$jobs{$j}}) {
138 if (!$queue->lock($m, $j)) {
139 print STDERR "Cannot move $m:", $queue->job_name($j), ", it is locked\n";
142 my $enq = $dest->enqueue($m, $job);
144 $dest->update_job_status($m, $job->id, 'NEW', 'Moved to this queue');
146 $dest->log($m, $job->id, 'REQUEUE', 'Moved to this queue');
148 $queue->update_job_status($m, $job->id, 'REMOVED', 'Moved from this queue');
149 $queue->remove($m, $j);
150 print "Moved $m:", $dest->job_name($j);
151 print " (already queued)" if !$enq;