1 # Batch EXecutor -- Queues
2 # (c) 2011-2015 Martin Mares <mj@ucw.cz>
16 my ($class, $name) = @_;
18 my $path = $BEX::Config::home . '/' . $name;
19 -d $path or die "Queue directory $path does not exist (use 'bex qman init' to create it)\n";
20 -d "$path/hosts" && -d "$path/jobs" or die "Queue directory $path is misconfigured\n";
30 my ($queue, $machine, $jid) = @_;
31 return $queue->host_dir($machine) . '/' . $jid . '.log';
34 # Most actions have to be logged by the caller
36 my ($queue, $mach, $jid, $stat, $msg) = @_;
37 my $t = POSIX::strftime("%Y-%m-%d %H:%M:%S", localtime);
38 my $m = join(" ", $t, $mach, $jid, $stat);
39 $m .= " $msg" if defined $msg;
41 my $fh = $queue->{'LogFH'} //= new IO::File $queue->{'Path'} . '/log', '>>' or die "Cannot open log: $!";
44 # Append to the per-job log file
45 if (open L, '>>', $queue->log_file($mach, $jid)) {
52 my ($queue, $machine) = @_;
53 return $queue->{'Path'} . '/hosts/' . $machine;
57 my ($queue, $machine, $jid) = @_;
58 return $queue->host_dir($machine) . '/' . $jid . '.job';
62 my ($queue, $machine, $jid) = @_;
63 return $queue->host_dir($machine) . '/' . $jid . '.stat';
67 my ($queue, $machine, $jid) = @_;
68 return $queue->host_dir($machine) . '/' . $jid . '.tmp';
72 my ($queue, $jid) = @_;
73 return $queue->{'Path'} . '/jobs/' . $jid. '.job';
76 sub attachment_dir($$) {
77 my ($queue, $jid) = @_;
78 return $queue->{'Path'} . '/jobs/' . $jid. '.attach';
82 my ($queue, $job) = @_;
83 # If the job already exists, it is shamelessly rewritten by new contents
84 $job->save($queue->job_file($job->id));
89 opendir my $dir, $queue->{'Path'} . '/jobs' or die "Cannot open job directory: $!\n";
91 while (readdir $dir) {
99 sub match_job_id($$) {
100 my ($id, $pattern) = @_;
101 return $id =~ m{\b$pattern};
104 # Resolve a (possibly partial) job ID given by the user
105 sub resolve_job_id($$) {
106 my ($queue, $name) = @_;
107 BEX::Job::check_id($name) or die "Invalid job ID $name\n";
108 if (-f $queue->job_file($name)) {
112 my @candidates = grep { match_job_id($_, $name) } $queue->all_job_ids();
113 @candidates or die "No job called $name exists\n";
114 @candidates == 1 or die "Partial job ID $name is not unique\n";
115 return $candidates[0];
119 my ($queue, $machine, $job) = @_;
120 # The job must be already saved to the current queue
121 my $qf = $queue->queue_file($machine, $job->id);
125 my $dir = $queue->host_dir($machine);
126 -d $dir or mkdir $dir or die "Cannot create directory $dir: $!";
127 symlink '../../jobs/' . $job->id . '.job', $qf or die "Cannot create $qf: $!";
132 my ($queue, $machine) = @_;
134 if (opendir D, $queue->host_dir($machine)) {
135 while ($_ = readdir D) {
146 my ($queue, $machine, $jid, $force_remove) = @_;
147 if ($BEX::Config::keep_history && !$force_remove) {
148 my $s = $queue->{'Path'} . '/hosts/' . $machine;
149 my $d = $queue->{'Path'} . '/history/' . $machine;
150 File::Path::mkpath($d);
151 for my $suff ('job', 'stat', 'log') {
152 my $src = "$s/$jid.$suff";
153 my $dst = "$d/$jid.$suff";
155 rename $src, $dst or die "Cannot rename $src to $dst: $!";
157 # Might be present from the previous incarnation of the same job
162 unlink $queue->queue_file($machine, $jid);
163 unlink $queue->status_file($machine, $jid);
164 unlink $queue->log_file($machine, $jid);
166 unlink $queue->temp_file($machine, $jid);
169 sub job_metadata($$) {
170 my ($queue, $jid) = @_;
171 my $cache = $queue->{'MetaCache'};
172 if (!defined $cache->{$jid}) {
173 $cache->{$jid} = BEX::Job->new_from_file($queue->job_file($jid), 1);
175 return $cache->{$jid};
179 my ($queue, $jid) = @_;
180 return $queue->job_metadata($jid)->name;
183 sub read_job_status($$$) {
184 my ($queue, $machine, $jid) = @_;
186 my $sf = $queue->status_file($machine, $jid);
187 if (open S, '<', $sf) {
190 /^(\w+):\s*(.*)/ or die "Parse error in $sf";
198 sub write_job_status($$$$) {
199 my ($queue, $machine, $jid, $stat) = @_;
200 my $sf = $queue->status_file($machine, $jid);
201 open S, '>', "$sf.$$" or die "Cannot create $sf.$$: $!";
202 for my $k (sort keys %$stat) {
203 print S "$k: ", $stat->{$k}, "\n" if defined $stat->{$k};
206 rename "$sf.$$", $sf or die "Cannot rename $sf.$$ to $sf: $!";
209 sub update_job_status($$$$;$) {
210 my ($queue, $machine, $jid, $stat, $msg) = @_;
216 $queue->write_job_status($machine, $jid, $s);
217 $queue->log($machine, $jid, $stat, $msg);
221 my ($queue, $machine, $jid) = @_;
222 my $lock = $queue->{'Path'};
224 $lock .= "/hosts/$machine/$jid.lock";
225 } elsif (defined $machine) {
226 $lock .= "/hosts/$machine/lock";
232 # Whenever we want to run a job on a machine, we must obtain a lock;
233 # at most one lock can be held at a time by a single BEX::Queue object.
234 # See the description of locking schemes in BEX::Config.
236 my ($queue, $machine, $jid) = @_;
238 given ($BEX::Config::locking_scheme) {
240 $lock = lock_name($queue, undef, undef);
243 defined($machine) or return 1;
244 $lock = lock_name($queue, $machine, undef);
247 defined($machine) && defined($jid) or return 1;
248 $lock = lock_name($queue, $machine, $jid);
250 when ('none') { return 1; }
251 default { die "Invalid BEX::Config::locking_scheme"; }
253 if (defined($queue->{'LockName'})) {
254 return 1 if ($queue->{'LockName'} eq $lock);
257 open $queue->{'LockHandle'}, '>>', $lock or die "Cannot create $lock: $!";
258 if (!flock($queue->{'LockHandle'}, LOCK_EX | LOCK_NB)) {
259 close $queue->{'LockHandle'};
260 delete $queue->{'LockHandle'};
263 $queue->{'LockName'} = $lock;
269 defined $queue->{'LockName'} or return;
270 unlink $queue->{'LockName'};
271 flock $queue->{'LockHandle'}, LOCK_UN;
272 close $queue->{'LockHandle'};
273 delete $queue->{'LockHandle'};
274 delete $queue->{'LockName'};
277 # Unsafe (does not check fcntl, only existence of a lock file), but should be enough for `bex ls'
279 my ($queue, $machine, $jid) = @_;
280 given ($BEX::Config::locking_scheme) {
282 when ('host') { return unless defined $machine; }
283 when ('jid') { return unless defined $jid; }
284 when ('none') { return; }
286 my $lock = lock_name($queue, $machine, $jid);