From: Martin Mares Date: Mon, 31 Oct 2011 13:05:34 +0000 (+0100) Subject: benq has a lot of switches for creating the job X-Git-Tag: v3.0~42 X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=4bbe4aaeacc775eea28e6e4664a445dd098cd4b1;p=bex.git benq has a lot of switches for creating the job --- diff --git a/TODO b/TODO index 8b56683..7d243ba 100644 --- a/TODO +++ b/TODO @@ -1,6 +1,3 @@ -- benq: options for specifying subject and other params -- benq: take job from file -- benq: requeue job - bprun: option for setting max # of running jobs - bprun --job - bprun --curses @@ -10,3 +7,4 @@ - Detector of orphans (unused queue dirs, jobs on non-existent machines, non-queued jobs) - job failed => give a more explanatory message - write_job_status should be atomic +- bq --show diff --git a/benq b/benq index b4b6735..76d7948 100755 --- a/benq +++ b/benq @@ -10,15 +10,33 @@ use File::stat; use lib 'lib'; use BEX; +my $given_body; +my $given_go; +my $given_id; my $queue_name; +my $requeue_id; +my $given_subject; +my $given_template; GetOptions( + "b|body=s" => \$given_body, + "g|go!" => \$given_go, + "i|id=s" => \$given_id, "q|queue=s" => \$queue_name, + "r|requeue=s" => \$requeue_id, + "s|subject=s" => \$given_subject, + "t|template=s" => \$given_template, ) or die <] [!] ... Options: +-b, --body= Load job body from the given file +-g, --go Do not run editor, go enqueue the job immediately +-i, --id= Set job ID of the new job -q, --queue= Insert new jobs to the given queue +-r, --requeue= Re-queue an existing job instead of creating a new one +-s, --subject= Set subject of the new job +-t, --template= Load job template (headers and body) from the given file AMEN # Prepare machine set @@ -26,21 +44,50 @@ AMEN my @machines = BEX::Config::parse_machine_list(@ARGV); @machines or die "No machines match\n"; -# Create job template and let the user edit it -my $job = BEX::Job->new; -my $fn = $job->save; -my $orig_stat = stat($fn) or die; -system "editor", $fn and die "Editor exited with an error, file kept as $fn\n"; -my $new_stat = stat($fn) or die "File $fn disappeared under my hands: $!\n"; -if ($new_stat->mtime <= $orig_stat->mtime && $new_stat->size == $orig_stat->size) { - unlink $fn; - die "Cancelled\n"; +my $queue = BEX::Queue->new($queue_name); +my $job; +my $tmp_fn; + +if (defined $requeue_id) { + # When requeueing, just fetch the existing job + if (defined($given_body) || 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"; + $job = BEX::Job->new_from_file($fn); +} else { + # Create job template + if (defined $given_template) { + $job = BEX::Job->new_from_file($given_template); + } else { + $job = BEX::Job->new; + } + $job->attr('ID', $given_id) if defined $given_id; + $job->attr('Subject', $given_subject) if defined $given_subject; + if (defined $given_body) { + open B, '<', $given_body or die "Cannot open $given_body: $!\n"; + local $/; + $job->attr('body', ); + close B; + } + + # Let the user edit the template + if (!$given_go) { + $tmp_fn = $job->save; + my $orig_stat = stat($tmp_fn) or die; + system "editor", $tmp_fn and die "Editor exited with an error, file kept as $tmp_fn\n"; + my $new_stat = stat($tmp_fn) or die "File $tmp_fn disappeared under my hands: $!\n"; + if ($new_stat->mtime <= $orig_stat->mtime && $new_stat->size == $orig_stat->size) { + unlink $tmp_fn; + die "Cancelled\n"; + } + $job = BEX::Job->new_from_file($tmp_fn); + } } -# Re-load the job and put it to the queue -$job = BEX::Job->new_from_file($fn); +# Put the job to the queue print "New job ", $job->id, "\n"; -my $queue = BEX::Queue->new($queue_name); for my $m (@machines) { if ($queue->enqueue($m, $job)) { $queue->write_job_status($m, $job->id, { 'Time' => time, 'Status' => 'NEW' }); @@ -50,5 +97,5 @@ for my $m (@machines) { } } -# Remove the temporary file -unlink $fn; +# Remove the temporary file if there's any +unlink $tmp_fn if defined $tmp_fn;