From 7e5a7889dfaf6159d2b55aeacb997d589ea9e7b2 Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Wed, 15 Feb 2012 23:31:06 +0100 Subject: [PATCH] Rename subcommands --- bex | 6 +- lib/bin/ls | 160 ++++++++++++++++++++++++++++++++++++++++++-------- lib/bin/mach | 42 +++++++++++++ lib/bin/queue | 156 ------------------------------------------------ 4 files changed, 182 insertions(+), 182 deletions(-) create mode 100755 lib/bin/mach delete mode 100755 lib/bin/queue diff --git a/bex b/bex index 02084e0..237db3f 100755 --- a/bex +++ b/bex @@ -24,9 +24,9 @@ General options: Commands (and aliases): add (a) Add new jobs to a queue job Operations on queued jobs -ls List known machines and groups +ls (l) Show queues and jobs on them +mach List known machines and groups prun (pr) Parallel version of `run' -queue (q) Show queues and jobs run (r) Run queued jobs AMEN exit 0; @@ -53,8 +53,8 @@ $sub =~ /^[0-9a-zA-Z]+$/ or die "Invalid subcommand $sub\n"; my %aliases = ( 'a' => 'add', + 'l' => 'ls', 'p' => 'prun', - 'q' => 'queue', 'r' => 'run', ); if (defined $aliases{$sub}) { $sub = $aliases{$sub}; } diff --git a/lib/bin/ls b/lib/bin/ls index 542835c..79db513 100755 --- a/lib/bin/ls +++ b/lib/bin/ls @@ -1,42 +1,156 @@ #!/usr/bin/perl -# Batch EXecutor 3.0 -- List Machines and Groups +# Batch EXecutor 3.0 -- Show Queued Jobs # (c) 2011-2012 Martin Mares use strict; use warnings; use Getopt::Long; +use POSIX; use BEX; -my $edit; +my $op_by_job; +my $op_by_host; +my $op_rm; +my $op_move_to; + my $queue_name; +my $given_job; GetOptions( -) && @ARGV == 0 or die <] + "by-job!" => \$op_by_job, + "h|by-host!" => \$op_by_host, + "rm!" => \$op_rm, + "move-to=s" => \$op_move_to, + "j|job=s" => \$given_job, + "q|queue=s" => \$queue_name, +) or die <] [[!] ...] + +Actions: + --by-job Show jobs sorted by job ID (default) +-h, --by-host Show jobs sorted by host + --rm Remove jobs from the queue + --move-to= Move jobs to a different queue Options: -None defined so far. +-j, --job= Act on the specified job (default: on all) +-q, --queue= Act on the given queue AMEN -my $machines = \%BEX::Config::machines; +my @machines = BEX::Config::parse_machine_list(@ARGV ? @ARGV : '*'); +my $queue = BEX::Queue->new($queue_name); + +# Select jobs +my %jobs = (); +my %machs = (); +for my $m (@machines) { + for my $j ($queue->scan($m)) { + if (defined $given_job) { + next if $j ne $given_job; + } + push @{$jobs{$j}}, $m; + push @{$machs{$m}}, $j; + } +} + +sub do_ls(); +sub do_rm(); +sub do_move_to(); + +my $ops = 0 + defined($op_by_host) + defined($op_by_job) + defined($op_rm) + defined($op_move_to); +if ($ops > 1) { die "Multiple actions are not allowed\n"; } + +if ($op_rm) { do_rm(); } +elsif (defined $op_move_to) { do_move_to(); } +else { do_ls(); } +exit 0; + +sub do_ls() +{ + my %stat = (); + my %mach_locked = (); + for my $m (keys %machs) { + $mach_locked{$m} = $queue->is_locked($m, undef); + for my $j (@{$machs{$m}}) { + my $st = $queue->read_job_status($m, $j); + if (defined($st->{'Time'}) && defined($st->{'Status'})) { + $stat{$m}{$j} = ' [' . $st->{'Status'} . ' on ' . + POSIX::strftime('%Y-%m-%d', localtime $st->{'Time'}) . ']'; + } else { + $stat{$m}{$j} = ''; + } + if ($mach_locked{$m} || $queue->is_locked($m, $j)) { + $stat{$m}{$j} .= ' [LOCKED]'; + } + } + } + + if ($queue->is_locked(undef, undef)) { + print "### Queue lock present\n\n"; + } + + if ($op_by_host) { + for my $m (sort keys %machs) { + print "$m", ($mach_locked{$m} ? ' [LOCKED]' : ''), "\n"; + for my $j (@{$machs{$m}}) { + print "\t" . $queue->job_name($j) . $stat{$m}{$j}, "\n"; + } + } + } else { + for my $j (sort keys %jobs) { + print $queue->job_name($j), "\n"; + for my $m (sort @{$jobs{$j}}) { + print "\t$m", $stat{$m}{$j}, "\n"; + } + } + } +} -print "# Hosts:\n"; -for my $h (sort keys %$machines) { - my $m = $machines->{$h}; - ref $m eq 'HASH' or next; - print "$h\n"; +sub do_rm() +{ + my $err = 0; + for my $m (sort keys %machs) { + for my $j (sort @{$machs{$m}}) { + if (!$queue->lock($m, $j)) { + print STDERR "Cannot remove $m:", $queue->job_name($j), ", it is locked\n"; + $err = 1; + } else { + $queue->update_job_status($m, $j, 'REMOVED'); + $queue->remove($m, $j); + print "Removed $m:", $queue->job_name($j), "\n"; + } + } + } + $queue->unlock; + exit $err; } -print "\n# Groups:\n"; -for my $h (sort keys %$machines) { - my $m = $machines->{$h}; - ref $m eq 'ARRAY' or next; - print "$h = ", join(" ", - map { - my $x = $machines->{$_}; - !defined($x) ? "$_?" : - ref $x eq 'HASH' ? $_ : - ref $x eq 'ARRAY' ? "\@$_" : - "$_???" - } @$m), "\n"; +sub do_move_to() +{ + my $err = 0; + my $dest = BEX::Queue->new($op_move_to); + $dest->{'Name'} ne $queue->{'Name'} or die "Moving to the same queue is not permitted\n"; + for my $j (sort keys %jobs) { + my $job = BEX::Job->new_from_file($queue->job_file($j)); + for my $m (sort @{$jobs{$j}}) { + if (!$queue->lock($m, $j)) { + print STDERR "Cannot move $m:", $queue->job_name($j), ", it is locked\n"; + $err = 1; + } else { + my $enq = $dest->enqueue($m, $job); + if ($enq) { + $dest->update_job_status($m, $job->id, 'NEW', 'Moved to this queue'); + } else { + $dest->log($m, $job->id, 'REQUEUE', 'Moved to this queue'); + } + $queue->update_job_status($m, $job->id, 'REMOVED', 'Moved from this queue'); + $queue->remove($m, $j); + print "Moved $m:", $dest->job_name($j); + print " (already queued)" if !$enq; + print "\n"; + } + } + } + $queue->unlock; + exit $err; } diff --git a/lib/bin/mach b/lib/bin/mach new file mode 100755 index 0000000..69c26c6 --- /dev/null +++ b/lib/bin/mach @@ -0,0 +1,42 @@ +#!/usr/bin/perl +# Batch EXecutor 3.0 -- List Machines and Groups +# (c) 2011-2012 Martin Mares + +use strict; +use warnings; +use Getopt::Long; +use BEX; + +my $edit; +my $queue_name; + +GetOptions( +) && @ARGV == 0 or die <] + +Options: +None defined so far. +AMEN + +my $machines = \%BEX::Config::machines; + +print "# Hosts:\n"; +for my $h (sort keys %$machines) { + my $m = $machines->{$h}; + ref $m eq 'HASH' or next; + print "$h\n"; +} + +print "\n# Groups:\n"; +for my $h (sort keys %$machines) { + my $m = $machines->{$h}; + ref $m eq 'ARRAY' or next; + print "$h = ", join(" ", + map { + my $x = $machines->{$_}; + !defined($x) ? "$_?" : + ref $x eq 'HASH' ? $_ : + ref $x eq 'ARRAY' ? "\@$_" : + "$_???" + } @$m), "\n"; +} diff --git a/lib/bin/queue b/lib/bin/queue deleted file mode 100755 index 2399519..0000000 --- a/lib/bin/queue +++ /dev/null @@ -1,156 +0,0 @@ -#!/usr/bin/perl -# Batch EXecutor 3.0 -- Show Queued Jobs -# (c) 2011-2012 Martin Mares - -use strict; -use warnings; -use Getopt::Long; -use POSIX; -use BEX; - -my $op_by_job; -my $op_by_host; -my $op_rm; -my $op_move_to; - -my $queue_name; -my $given_job; - -GetOptions( - "by-job!" => \$op_by_job, - "h|by-host!" => \$op_by_host, - "rm!" => \$op_rm, - "move-to=s" => \$op_move_to, - "j|job=s" => \$given_job, - "q|queue=s" => \$queue_name, -) or die <] [[!] ...] - -Actions: - --by-job Show jobs sorted by job ID (default) --h, --by-host Show jobs sorted by host - --rm Remove jobs from the queue - --move-to= Move jobs to a different queue - -Options: --j, --job= Act on the specified job (default: on all) --q, --queue= Act on the given queue -AMEN - -my @machines = BEX::Config::parse_machine_list(@ARGV ? @ARGV : '*'); -my $queue = BEX::Queue->new($queue_name); - -# Select jobs -my %jobs = (); -my %machs = (); -for my $m (@machines) { - for my $j ($queue->scan($m)) { - if (defined $given_job) { - next if $j ne $given_job; - } - push @{$jobs{$j}}, $m; - push @{$machs{$m}}, $j; - } -} - -sub do_ls(); -sub do_rm(); -sub do_move_to(); - -my $ops = 0 + defined($op_by_host) + defined($op_by_job) + defined($op_rm) + defined($op_move_to); -if ($ops > 1) { die "Multiple actions are not allowed\n"; } - -if ($op_rm) { do_rm(); } -elsif (defined $op_move_to) { do_move_to(); } -else { do_ls(); } -exit 0; - -sub do_ls() -{ - my %stat = (); - my %mach_locked = (); - for my $m (keys %machs) { - $mach_locked{$m} = $queue->is_locked($m, undef); - for my $j (@{$machs{$m}}) { - my $st = $queue->read_job_status($m, $j); - if (defined($st->{'Time'}) && defined($st->{'Status'})) { - $stat{$m}{$j} = ' [' . $st->{'Status'} . ' on ' . - POSIX::strftime('%Y-%m-%d', localtime $st->{'Time'}) . ']'; - } else { - $stat{$m}{$j} = ''; - } - if ($mach_locked{$m} || $queue->is_locked($m, $j)) { - $stat{$m}{$j} .= ' [LOCKED]'; - } - } - } - - if ($queue->is_locked(undef, undef)) { - print "### Queue lock present\n\n"; - } - - if ($op_by_host) { - for my $m (sort keys %machs) { - print "$m", ($mach_locked{$m} ? ' [LOCKED]' : ''), "\n"; - for my $j (@{$machs{$m}}) { - print "\t" . $queue->job_name($j) . $stat{$m}{$j}, "\n"; - } - } - } else { - for my $j (sort keys %jobs) { - print $queue->job_name($j), "\n"; - for my $m (sort @{$jobs{$j}}) { - print "\t$m", $stat{$m}{$j}, "\n"; - } - } - } -} - -sub do_rm() -{ - my $err = 0; - for my $m (sort keys %machs) { - for my $j (sort @{$machs{$m}}) { - if (!$queue->lock($m, $j)) { - print STDERR "Cannot remove $m:", $queue->job_name($j), ", it is locked\n"; - $err = 1; - } else { - $queue->update_job_status($m, $j, 'REMOVED'); - $queue->remove($m, $j); - print "Removed $m:", $queue->job_name($j), "\n"; - } - } - } - $queue->unlock; - exit $err; -} - -sub do_move_to() -{ - my $err = 0; - my $dest = BEX::Queue->new($op_move_to); - $dest->{'Name'} ne $queue->{'Name'} or die "Moving to the same queue is not permitted\n"; - for my $j (sort keys %jobs) { - my $job = BEX::Job->new_from_file($queue->job_file($j)); - for my $m (sort @{$jobs{$j}}) { - if (!$queue->lock($m, $j)) { - print STDERR "Cannot move $m:", $queue->job_name($j), ", it is locked\n"; - $err = 1; - } else { - my $enq = $dest->enqueue($m, $job); - if ($enq) { - $dest->update_job_status($m, $job->id, 'NEW', 'Moved to this queue'); - } else { - $dest->log($m, $job->id, 'REQUEUE', 'Moved to this queue'); - } - $queue->update_job_status($m, $job->id, 'REMOVED', 'Moved from this queue'); - $queue->remove($m, $j); - print "Moved $m:", $dest->job_name($j); - print " (already queued)" if !$enq; - print "\n"; - } - } - } - $queue->unlock; - exit $err; -} -- 2.39.2