2 # Batch EXecutor 3.0 -- Insert to Queue
3 # (c) 2011-2012 Martin Mares <mj@ucw.cz>
20 "b|body=s" => \$given_body,
21 "g|go!" => \$given_go,
22 "i|id=s" => \$given_id,
23 "q|queue=s" => \$queue_name,
24 "r|requeue=s" => \$requeue_id,
25 "s|subject=s" => \$given_subject,
26 "t|template=s" => \$given_template,
28 Usage: bex add [<options>] [!]<machine-or-class> ...
31 -b, --body=<file> Load job body from the given file
32 -g, --go Do not run editor, go enqueue the job immediately
33 -i, --id=<id> Set job ID of the new job
34 -q, --queue=<name> Insert new jobs to the given queue
35 -r, --requeue=<id> Re-queue an existing job instead of creating a new one
36 -s, --subject=<subj> Set subject of the new job
37 -t, --template=<file> Load job template (headers and body) from the given file
41 @ARGV or die "No machines specified\n";
42 my @machines = BEX::Config::parse_machine_list(@ARGV);
43 @machines or die "No machines match\n";
45 my $queue = BEX::Queue->new($queue_name);
49 if (defined $requeue_id) {
50 # When requeueing, just fetch the existing job
51 if (defined($given_body) || defined($given_id) || defined($given_subject) || defined($given_template)) {
52 die "Parameters of a requeued job cannot be changed\n";
54 my $fn = $queue->job_file($requeue_id);
55 -f $fn or die "Job $requeue_id not known\n";
56 $job = BEX::Job->new_from_file($fn);
59 if (defined $given_template) {
60 $job = BEX::Job->new_from_file($given_template);
64 $job->attr('ID', $given_id) if defined $given_id;
65 $job->attr('Subject', $given_subject) if defined $given_subject;
66 if (defined $given_body) {
67 open B, '<', $given_body or die "Cannot open $given_body: $!\n";
69 $job->attr('body', <B>);
73 # Let the user edit the template
76 my $orig_stat = stat($tmp_fn) or die;
77 system "editor", $tmp_fn and die "Editor exited with an error, file kept as $tmp_fn\n";
78 my $new_stat = stat($tmp_fn) or die "File $tmp_fn disappeared under my hands: $!\n";
79 if ($new_stat->mtime <= $orig_stat->mtime && $new_stat->size == $orig_stat->size) {
83 $job = BEX::Job->new_from_file($tmp_fn);
87 # Put the job to the queue
88 print "New job ", $job->id, "\n";
89 for my $m (@machines) {
90 if ($queue->enqueue($m, $job)) {
91 $queue->update_job_status($m, $job->id, 'NEW');
94 $queue->log($m, $job->id, 'REQUEUE');
95 print "\t$m (already queued)\n";
99 # Remove the temporary file if there's any
100 unlink $tmp_fn if defined $tmp_fn;