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 <<AMEN ;
Usage: benq [<options>] [!]<machine-or-class> ...
Options:
+-b, --body=<file> Load job body from the given file
+-g, --go Do not run editor, go enqueue the job immediately
+-i, --id=<id> Set job ID of the new job
-q, --queue=<name> Insert new jobs to the given queue
+-r, --requeue=<id> Re-queue an existing job instead of creating a new one
+-s, --subject=<subj> Set subject of the new job
+-t, --template=<file> Load job template (headers and body) from the given file
AMEN
# Prepare machine set
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', <B>);
+ 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);
-print "New job ", $job->{'ID'}, "\n";
-my $queue = BEX::Queue->new($queue_name);
+# Put the job to the queue
+print "New job ", $job->id, "\n";
for my $m (@machines) {
if ($queue->enqueue($m, $job)) {
+ $queue->write_job_status($m, $job->id, { 'Time' => time, 'Status' => 'NEW' });
print "\t$m\n";
- BEX::log("$m " . $job->{'ID'} . " queued");
} else {
print "\t$m (already queued)\n";
}
}
-# Remove the temporary file
-unlink $fn;
+# Remove the temporary file if there's any
+unlink $tmp_fn if defined $tmp_fn;