2 # Batch EXecutor 2.0 -- Insert to Queue
3 # (c) 2011 Martin Mares <mj@ucw.cz>
22 "b|body=s" => \$given_body,
23 "g|go!" => \$given_go,
24 "i|id=s" => \$given_id,
25 "q|queue=s" => \$queue_name,
26 "r|requeue=s" => \$requeue_id,
27 "s|subject=s" => \$given_subject,
28 "t|template=s" => \$given_template,
30 Usage: benq [<options>] [!]<machine-or-class> ...
33 -b, --body=<file> Load job body from the given file
34 -g, --go Do not run editor, go enqueue the job immediately
35 -i, --id=<id> Set job ID of the new job
36 -q, --queue=<name> Insert new jobs to the given queue
37 -r, --requeue=<id> Re-queue an existing job instead of creating a new one
38 -s, --subject=<subj> Set subject of the new job
39 -t, --template=<file> Load job template (headers and body) from the given file
43 @ARGV or die "No machines specified\n";
44 my @machines = BEX::Config::parse_machine_list(@ARGV);
45 @machines or die "No machines match\n";
47 my $queue = BEX::Queue->new($queue_name);
51 if (defined $requeue_id) {
52 # When requeueing, just fetch the existing job
53 if (defined($given_body) || defined($given_id) || defined($given_subject) || defined($given_template)) {
54 die "Parameters of a requeued job cannot be changed\n";
56 my $fn = $queue->job_file($requeue_id);
57 -f $fn or die "Job $requeue_id not known\n";
58 $job = BEX::Job->new_from_file($fn);
61 if (defined $given_template) {
62 $job = BEX::Job->new_from_file($given_template);
66 $job->attr('ID', $given_id) if defined $given_id;
67 $job->attr('Subject', $given_subject) if defined $given_subject;
68 if (defined $given_body) {
69 open B, '<', $given_body or die "Cannot open $given_body: $!\n";
71 $job->attr('body', <B>);
75 # Let the user edit the template
78 my $orig_stat = stat($tmp_fn) or die;
79 system "editor", $tmp_fn and die "Editor exited with an error, file kept as $tmp_fn\n";
80 my $new_stat = stat($tmp_fn) or die "File $tmp_fn disappeared under my hands: $!\n";
81 if ($new_stat->mtime <= $orig_stat->mtime && $new_stat->size == $orig_stat->size) {
85 $job = BEX::Job->new_from_file($tmp_fn);
89 # Put the job to the queue
90 print "New job ", $job->id, "\n";
91 for my $m (@machines) {
92 if ($queue->enqueue($m, $job)) {
93 $queue->write_job_status($m, $job->id, { 'Time' => time, 'Status' => 'NEW' });
96 print "\t$m (already queued)\n";
100 # Remove the temporary file if there's any
101 unlink $tmp_fn if defined $tmp_fn;