#!/usr/bin/perl
-# Batch EXecutor 2.0
+# Batch EXecutor 2.0 -- Insert to Queue
# (c) 2011 Martin Mares <mj@ucw.cz>
use strict;
use warnings;
+use Getopt::Long;
+use File::stat;
use lib 'lib';
use BEX;
+my $queue_name;
+
+GetOptions(
+ "q|queue=s" => \$queue_name,
+) or die <<AMEN ;
+Usage: benq [<options>] [!]<machine-or-class> ...
+
+Options:
+-q, --queue=<name> Insert new jobs to the given queue
+AMEN
+
+# Prepare machine set
+@ARGV or die "No machines specified\n";
+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;
-system "editor", $fn;
-## FIXME: Check exit code of editor
-$job = BEX::Job->new_from_file($fn);
-## FIXME: Compare and exit if no changes
+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;
-for my $m (keys %BEX::Config::machines) {
- $queue->enqueue($m, $job);
+# 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);
+for my $m (@machines) {
+ if ($queue->enqueue($m, $job)) {
+ print "\t$m\n";
+ } else {
+ print "\t$m (already queued)\n";
+ }
}
+# Remove the temporary file
unlink $fn;