]> mj.ucw.cz Git - bex.git/commitdiff
Accept partial job IDs as long as they are unique
authorMartin Mares <mj@ucw.cz>
Sun, 29 Mar 2015 15:50:35 +0000 (17:50 +0200)
committerMartin Mares <mj@ucw.cz>
Sun, 29 Mar 2015 15:50:35 +0000 (17:50 +0200)
Also, "bex job" without arguments lists all known jobs and it can
be abbreviated to "bex j".

NOTES
bex
lib/bin/bex-add
lib/bin/bex-job
lib/bin/bex-queue
lib/bin/bex-run
lib/perl/BEX/Queue.pm

diff --git a/NOTES b/NOTES
index 499ad43bf6aabf0579be64baf59d7910ab99cb7b..008fbd38a7c7c0be27552a5d400db3cbdf61eef9 100644 (file)
--- a/NOTES
+++ b/NOTES
@@ -38,6 +38,9 @@ Prep: <command>                       Run <command> 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 06ea876a0301e8c119990195b824a085ccaf0a2f..1eccca3da4a13b3e04b6dae9a1149ef7175ce999 100755 (executable)
--- a/bex
+++ b/bex
@@ -1,6 +1,6 @@
 #!/usr/bin/perl
 # Batch EXecutor -- Master Program
-# (c) 2012 Martin Mares <mj@ucw.cz>
+# (c) 2012--2015 Martin Mares <mj@ucw.cz>
 
 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',
index 9a717bf7d1f4e9d2f3f392c8d11c49fb5dba2e59..195300eb6fc389e71d07fcaa7878b9e55ce5ac51 100755 (executable)
@@ -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
index 3f10779e2f939ac8fac5ff0329ef905b00c499df..06f0e1394021c8bf0f5962f792bb3086678354f1 100755 (executable)
@@ -1,6 +1,6 @@
 #!/usr/bin/perl
 # Batch EXecutor -- Operations on a Job
-# (c) 2011-2012 Martin Mares <mj@ucw.cz>
+# (c) 2011-2015 Martin Mares <mj@ucw.cz>
 
 use strict;
 use warnings;
@@ -12,7 +12,7 @@ my $queue_name;
 
 sub usage() {
        print <<AMEN ;
-Usage: bex job [<options>] <job-id>
+Usage: bex job [<options>] [<job-id>]
 
 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;
index d0f34f16451a9cd2a16c585f9643397ed458c866..6a39852465a62f7a2194d2f704402a2087c79881 100755 (executable)
@@ -1,6 +1,6 @@
 #!/usr/bin/perl
 # Batch EXecutor -- Show Queued Jobs
-# (c) 2011-2013 Martin Mares <mj@ucw.cz>
+# (c) 2011-2015 Martin Mares <mj@ucw.cz>
 
 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 = ();
index c29141e39cd75e20999f723c3739ce5d3b215c6b..cd133fa82c73afe57d5d6e4fb090ea09a8126659 100755 (executable)
@@ -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";
 
index 683791129b0063c508c78860da1d9242f392e141..9783fa071b288f89948f16816fda40eb71fda3e2 100644 (file)
@@ -1,5 +1,5 @@
 # Batch EXecutor -- Queues
-# (c) 2011-2013 Martin Mares <mj@ucw.cz>
+# (c) 2011-2015 Martin Mares <mj@ucw.cz>
 
 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