2 # Batch EXecutor 3.0 -- Insert to Queue
3 # (c) 2011-2012 Martin Mares <mj@ucw.cz>
22 Usage: bex add [<options>] [!]<machine-or-class> ...
25 -b, --body=<file> Load job body from the given file
26 -e, --execute=<command> Set job body to the given command
27 -g, --go Do not run editor, go enqueue the job immediately
28 -i, --id=<id> Set job ID of the new job
29 -q, --queue=<name> Insert new jobs to the given queue
30 -r, --requeue=<id> Re-queue an existing job instead of creating a new one
31 -s, --subject=<subj> Set subject of the new job
32 -t, --template=<file> Load job template (headers and body) from the given file
38 "b|body=s" => \$given_body,
39 "e|execute=s" => \$given_execute,
40 "g|go!" => \$given_go,
41 "i|id=s" => \$given_id,
42 "q|queue=s" => \$queue_name,
43 "r|requeue=s" => \$requeue_id,
44 "s|subject=s" => \$given_subject,
45 "t|template=s" => \$given_template,
47 ) or die "Try `bex add --help' for more information.\n";
50 @ARGV or die "No machines specified\n";
51 my @machines = BEX::Config::parse_machine_list(@ARGV);
52 @machines or die "No machines match\n";
54 my $queue = BEX::Queue->new($queue_name);
58 if (defined $requeue_id) {
59 # When requeueing, just fetch the existing job
60 if (defined($given_body) || defined($given_execute) || defined($given_id) || defined($given_subject) || defined($given_template)) {
61 die "Parameters of a requeued job cannot be changed\n";
63 my $fn = $queue->job_file($requeue_id);
64 -f $fn or die "Job $requeue_id not known\n";
65 $job = BEX::Job->new_from_file($fn);
68 if (defined $given_template) {
69 $job = BEX::Job->new_from_file($given_template);
73 $job->attr('ID', $given_id) if defined $given_id;
74 $job->attr('Subject', $given_subject) if defined $given_subject;
75 if (defined $given_execute) {
76 $job->attr('body', $given_execute. "\n");
77 } elsif (defined $given_body) {
78 open B, '<', $given_body or die "Cannot open $given_body: $!\n";
80 $job->attr('body', <B>);
84 # Let the user edit the template
87 my $orig_stat = stat($tmp_fn) or die;
88 system "editor", $tmp_fn and die "Editor exited with an error, file kept as $tmp_fn\n";
89 my $new_stat = stat($tmp_fn) or die "File $tmp_fn disappeared under my hands: $!\n";
90 if ($new_stat->mtime <= $orig_stat->mtime && $new_stat->size == $orig_stat->size) {
94 $job = BEX::Job->new_from_file($tmp_fn);
98 # Put the job to the queue
99 print "New job ", $job->id, "\n";
100 $queue->save_job($job);
101 for my $m (@machines) {
102 if ($queue->enqueue($m, $job)) {
103 $queue->update_job_status($m, $job->id, 'NEW');
106 $queue->log($m, $job->id, 'REQUEUE');
107 print "\t$m (already queued)\n";
111 # Remove the temporary file if there's any
112 unlink $tmp_fn if defined $tmp_fn;