From: Martin Mares Date: Sun, 29 Mar 2015 15:50:35 +0000 (+0200) Subject: Accept partial job IDs as long as they are unique X-Git-Tag: v3.3~4 X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=2fa98b2d0ea8a0180f494993e1a3e8c3d20d9248;p=bex.git Accept partial job IDs as long as they are unique Also, "bex job" without arguments lists all known jobs and it can be abbreviated to "bex j". --- diff --git a/NOTES b/NOTES index 499ad43..008fbd3 100644 --- a/NOTES +++ b/NOTES @@ -38,6 +38,9 @@ Prep: Run in a shell before the job body is executed; $HOST contains the name of the host. This is useful for example if you want to transfer data to the host by rsync. +Whenever a user command wants a job ID, it accepts any substring, as long as +it is unique. + ### Status files ### Structure identical to job headers, but they do not contain a body. diff --git a/bex b/bex index 06ea876..1eccca3 100755 --- a/bex +++ b/bex @@ -1,6 +1,6 @@ #!/usr/bin/perl # Batch EXecutor -- Master Program -# (c) 2012 Martin Mares +# (c) 2012--2015 Martin Mares use strict; use warnings; @@ -20,7 +20,7 @@ General options: Commands (and aliases): add (a) Add new jobs to a queue help Show this help -job Operations on queued jobs +job (j) Operations on jobs mach List known machines and groups prun (pr) Parallel version of `run' qman Management of queues @@ -59,6 +59,7 @@ if ($sub eq 'help') { show_help(); } my %aliases = ( 'a' => 'add', + 'j' => 'job', 'p' => 'prun', 'q' => 'queue', 'r' => 'run', diff --git a/lib/bin/bex-add b/lib/bin/bex-add index 9a717bf..195300e 100755 --- a/lib/bin/bex-add +++ b/lib/bin/bex-add @@ -70,8 +70,8 @@ if (defined $requeue_id) { if (defined($given_body) || defined($given_execute) || defined($given_id) || defined($given_subject) || defined($given_template)) { die "Parameters of a requeued job cannot be changed\n"; } - my $fn = $queue->job_file($requeue_id); - -f $fn or die "Job $requeue_id not known\n"; + my $id = $queue->resolve_job_id($requeue_id); + my $fn = $queue->job_file($id); $job = BEX::Job->new_from_file($fn); } else { # Create job template diff --git a/lib/bin/bex-job b/lib/bin/bex-job index 3f10779..06f0e13 100755 --- a/lib/bin/bex-job +++ b/lib/bin/bex-job @@ -1,6 +1,6 @@ #!/usr/bin/perl # Batch EXecutor -- Operations on a Job -# (c) 2011-2012 Martin Mares +# (c) 2011-2015 Martin Mares use strict; use warnings; @@ -12,7 +12,7 @@ my $queue_name; sub usage() { print <] +Usage: bex job [] [] Options: -e, --edit Run editor on the given job (no locking) @@ -25,11 +25,20 @@ GetOptions( "e|edit!" => \$edit, "q|queue=s" => \$queue_name, "help" => \&usage, -) && @ARGV == 1 or die "Try `bex job --help' for more information.\n"; +) && @ARGV <= 1 or die "Try `bex job --help' for more information.\n"; my $queue = BEX::Queue->new($queue_name); -my $fn = $queue->job_file($ARGV[0]); --f $fn or die "No such job " . $ARGV[0] . "\n"; + +if (!@ARGV) { + my @jobs = $queue->all_job_ids; + for (sort @jobs) { + print $_, "\n"; + } + exit 0; +} + +my $jid = $queue->resolve_job_id($ARGV[0]); +my $fn = $queue->job_file($jid); if ($edit) { system "editor", $fn; diff --git a/lib/bin/bex-queue b/lib/bin/bex-queue index d0f34f1..6a39852 100755 --- a/lib/bin/bex-queue +++ b/lib/bin/bex-queue @@ -1,6 +1,6 @@ #!/usr/bin/perl # Batch EXecutor -- Show Queued Jobs -# (c) 2011-2013 Martin Mares +# (c) 2011-2015 Martin Mares use strict; use warnings; @@ -61,6 +61,7 @@ GetOptions( my @machines = BEX::Config::parse_machine_list(@ARGV ? @ARGV : '*'); my $queue = BEX::Queue->new($queue_name); +$given_job = $queue->resolve_job_id($given_job) if defined $given_job; # Status cache my %status_cache = (); diff --git a/lib/bin/bex-run b/lib/bin/bex-run index c29141e..cd133fa 100755 --- a/lib/bin/bex-run +++ b/lib/bin/bex-run @@ -200,6 +200,7 @@ sub run_job($$$) { my @machines = BEX::Config::parse_machine_list(@ARGV ? @ARGV : '*'); my $queue = BEX::Queue->new($queue_name); +$given_job = $queue->resolve_job_id($given_job) if defined $given_job; $queue->lock(undef, undef) or die "The queue is locked by another bex run, cannot continue.\n"; diff --git a/lib/perl/BEX/Queue.pm b/lib/perl/BEX/Queue.pm index 6837911..9783fa0 100644 --- a/lib/perl/BEX/Queue.pm +++ b/lib/perl/BEX/Queue.pm @@ -1,5 +1,5 @@ # Batch EXecutor -- Queues -# (c) 2011-2013 Martin Mares +# (c) 2011-2015 Martin Mares use strict; use warnings; @@ -84,6 +84,32 @@ sub save_job($$) { $job->save($queue->job_file($job->id)); } +sub all_job_ids($) { + my ($queue) = @_; + opendir my $dir, $queue->{'Path'} . '/jobs' or die "Cannot open job directory: $!\n"; + my @jobs = (); + while (readdir $dir) { + s{\.job$}{} or next; + push @jobs, $_; + } + closedir $dir; + return @jobs; +} + +# Resolve a (possibly partial) job ID given by the user +sub resolve_job_id($$) { + my ($queue, $name) = @_; + BEX::Job::check_id($name) or die "Invalid job ID $name\n"; + if (-f $queue->job_file($name)) { + return $name; + } + + my @candidates = map { index($_, $name) >= 0 ? $_ : () } $queue->all_job_ids(); + @candidates or die "No job called $name exists\n"; + @candidates == 1 or die "Partial job ID $name is not unique\n"; + return $candidates[0]; +} + sub enqueue($$$) { my ($queue, $machine, $job) = @_; # The job must be already saved to the current queue