2 # Batch EXecutor 3.0 -- Show Queued Jobs
3 # (c) 2011-2012 Martin Mares <mj@ucw.cz>
20 "by-job!" => \$op_by_job,
21 "h|by-host!" => \$op_by_host,
23 "move-to=s" => \$op_move_to,
24 "j|job=s" => \$given_job,
25 "q|queue=s" => \$queue_name,
27 Usage: bq [<options and actions>] [[!]<machine-or-class> ...]
30 --by-job Show jobs sorted by job ID (default)
31 -h, --by-host Show jobs sorted by host
32 --rm Remove jobs from the queue
33 --move-to=<queue> Move jobs to a different queue
36 -j, --job=<id> Act on the specified job (default: on all)
37 -q, --queue=<name> Act on the given queue
40 my @machines = BEX::Config::parse_machine_list(@ARGV ? @ARGV : '*');
41 my $queue = BEX::Queue->new($queue_name);
46 for my $m (@machines) {
47 for my $j ($queue->scan($m)) {
48 if (defined $given_job) {
49 next if $j ne $given_job;
51 push @{$jobs{$j}}, $m;
52 push @{$machs{$m}}, $j;
60 my $ops = 0 + defined($op_by_host) + defined($op_by_job) + defined($op_rm) + defined($op_move_to);
61 if ($ops > 1) { die "Multiple actions are not allowed\n"; }
63 if ($op_rm) { do_rm(); }
64 elsif (defined $op_move_to) { do_move_to(); }
72 for my $m (keys %machs) {
73 $mach_locked{$m} = $queue->is_locked($m, undef);
74 for my $j (@{$machs{$m}}) {
75 my $st = $queue->read_job_status($m, $j);
76 if (defined($st->{'Time'}) && defined($st->{'Status'})) {
77 $stat{$m}{$j} = ' [' . $st->{'Status'} . ' on ' .
78 POSIX::strftime('%Y-%m-%d', localtime $st->{'Time'}) . ']';
82 if ($mach_locked{$m} || $queue->is_locked($m, $j)) {
83 $stat{$m}{$j} .= ' [LOCKED]';
88 if ($queue->is_locked(undef, undef)) {
89 print "### Queue lock present\n\n";
93 for my $m (sort keys %machs) {
94 print "$m", ($mach_locked{$m} ? ' [LOCKED]' : ''), "\n";
95 for my $j (@{$machs{$m}}) {
96 print "\t" . $queue->job_name($j) . $stat{$m}{$j}, "\n";
100 for my $j (sort keys %jobs) {
101 print $queue->job_name($j), "\n";
102 for my $m (sort @{$jobs{$j}}) {
103 print "\t$m", $stat{$m}{$j}, "\n";
112 for my $m (sort keys %machs) {
113 for my $j (sort @{$machs{$m}}) {
114 if (!$queue->lock($m, $j)) {
115 print STDERR "Cannot remove $m:", $queue->job_name($j), ", it is locked\n";
118 $queue->update_job_status($m, $j, 'REMOVED');
119 $queue->remove($m, $j);
120 print "Removed $m:", $queue->job_name($j), "\n";
131 my $dest = BEX::Queue->new($op_move_to);
132 $dest->{'Name'} ne $queue->{'Name'} or die "Moving to the same queue is not permitted\n";
133 for my $j (sort keys %jobs) {
134 my $job = BEX::Job->new_from_file($queue->job_file($j));
135 for my $m (sort @{$jobs{$j}}) {
136 if (!$queue->lock($m, $j)) {
137 print STDERR "Cannot move $m:", $queue->job_name($j), ", it is locked\n";
140 my $enq = $dest->enqueue($m, $job);
142 $dest->update_job_status($m, $job->id, 'NEW', 'Moved to this queue');
144 $dest->log($m, $job->id, 'REQUEUE', 'Moved to this queue');
146 $queue->update_job_status($m, $job->id, 'REMOVED', 'Moved from this queue');
147 $queue->remove($m, $j);
148 print "Moved $m:", $dest->job_name($j);
149 print " (already queued)" if !$enq;