2 # Batch EXecutor 3.0 -- Insert to Queue
3 # (c) 2011-2012 Martin Mares <mj@ucw.cz>
21 Usage: bex add [<options>] [!]<machine-or-class> ...
24 -b, --body=<file> Load job body from the given file
25 -g, --go Do not run editor, go enqueue the job immediately
26 -i, --id=<id> Set job ID of the new job
27 -q, --queue=<name> Insert new jobs to the given queue
28 -r, --requeue=<id> Re-queue an existing job instead of creating a new one
29 -s, --subject=<subj> Set subject of the new job
30 -t, --template=<file> Load job template (headers and body) from the given file
36 "b|body=s" => \$given_body,
37 "g|go!" => \$given_go,
38 "i|id=s" => \$given_id,
39 "q|queue=s" => \$queue_name,
40 "r|requeue=s" => \$requeue_id,
41 "s|subject=s" => \$given_subject,
42 "t|template=s" => \$given_template,
44 ) or die "Try `bex add --help' for more information.\n";
47 @ARGV or die "No machines specified\n";
48 my @machines = BEX::Config::parse_machine_list(@ARGV);
49 @machines or die "No machines match\n";
51 my $queue = BEX::Queue->new($queue_name);
55 if (defined $requeue_id) {
56 # When requeueing, just fetch the existing job
57 if (defined($given_body) || defined($given_id) || defined($given_subject) || defined($given_template)) {
58 die "Parameters of a requeued job cannot be changed\n";
60 my $fn = $queue->job_file($requeue_id);
61 -f $fn or die "Job $requeue_id not known\n";
62 $job = BEX::Job->new_from_file($fn);
65 if (defined $given_template) {
66 $job = BEX::Job->new_from_file($given_template);
70 $job->attr('ID', $given_id) if defined $given_id;
71 $job->attr('Subject', $given_subject) if defined $given_subject;
72 if (defined $given_body) {
73 open B, '<', $given_body or die "Cannot open $given_body: $!\n";
75 $job->attr('body', <B>);
79 # Let the user edit the template
82 my $orig_stat = stat($tmp_fn) or die;
83 system "editor", $tmp_fn and die "Editor exited with an error, file kept as $tmp_fn\n";
84 my $new_stat = stat($tmp_fn) or die "File $tmp_fn disappeared under my hands: $!\n";
85 if ($new_stat->mtime <= $orig_stat->mtime && $new_stat->size == $orig_stat->size) {
89 $job = BEX::Job->new_from_file($tmp_fn);
93 # Put the job to the queue
94 print "New job ", $job->id, "\n";
95 $queue->save_job($job);
96 for my $m (@machines) {
97 if ($queue->enqueue($m, $job)) {
98 $queue->update_job_status($m, $job->id, 'NEW');
101 $queue->log($m, $job->id, 'REQUEUE');
102 print "\t$m (already queued)\n";
106 # Remove the temporary file if there's any
107 unlink $tmp_fn if defined $tmp_fn;