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 # Resolve a (possibly partial) job ID given by the user
100 sub resolve_job_id($$) {
101 my ($queue, $name) = @_;
102 BEX::Job::check_id($name) or die "Invalid job ID $name\n";
103 if (-f $queue->job_file($name)) {
107 my @candidates = map { index($_, $name) >= 0 ? $_ : () } $queue->all_job_ids();
108 @candidates or die "No job called $name exists\n";
109 @candidates == 1 or die "Partial job ID $name is not unique\n";
110 return $candidates[0];
114 my ($queue, $machine, $job) = @_;
115 # The job must be already saved to the current queue
116 my $qf = $queue->queue_file($machine, $job->id);
120 my $dir = $queue->host_dir($machine);
121 -d $dir or mkdir $dir or die "Cannot create directory $dir: $!";
122 symlink '../../jobs/' . $job->id . '.job', $qf or die "Cannot create $qf: $!";
127 my ($queue, $machine) = @_;
129 if (opendir D, $queue->host_dir($machine)) {
130 while ($_ = readdir D) {
141 my ($queue, $machine, $jid, $force_remove) = @_;
142 if ($BEX::Config::keep_history && !$force_remove) {
143 my $s = $queue->{'Path'} . '/hosts/' . $machine;
144 my $d = $queue->{'Path'} . '/history/' . $machine;
145 File::Path::mkpath($d);
146 for my $suff ('job', 'stat', 'log') {
147 my $src = "$s/$jid.$suff";
148 my $dst = "$d/$jid.$suff";
150 rename $src, $dst or die "Cannot rename $src to $dst: $!";
152 # Might be present from the previous incarnation of the same job
157 unlink $queue->queue_file($machine, $jid);
158 unlink $queue->status_file($machine, $jid);
159 unlink $queue->log_file($machine, $jid);
161 unlink $queue->temp_file($machine, $jid);
164 sub job_metadata($$) {
165 my ($queue, $jid) = @_;
166 my $cache = $queue->{'MetaCache'};
167 if (!defined $cache->{$jid}) {
168 $cache->{$jid} = BEX::Job->new_from_file($queue->job_file($jid), 1);
170 return $cache->{$jid};
174 my ($queue, $jid) = @_;
175 return $queue->job_metadata($jid)->name;
178 sub read_job_status($$$) {
179 my ($queue, $machine, $jid) = @_;
181 my $sf = $queue->status_file($machine, $jid);
182 if (open S, '<', $sf) {
185 /^(\w+):\s*(.*)/ or die "Parse error in $sf";
193 sub write_job_status($$$$) {
194 my ($queue, $machine, $jid, $stat) = @_;
195 my $sf = $queue->status_file($machine, $jid);
196 open S, '>', "$sf.$$" or die "Cannot create $sf.$$: $!";
197 for my $k (sort keys %$stat) {
198 print S "$k: ", $stat->{$k}, "\n" if defined $stat->{$k};
201 rename "$sf.$$", $sf or die "Cannot rename $sf.$$ to $sf: $!";
204 sub update_job_status($$$$;$) {
205 my ($queue, $machine, $jid, $stat, $msg) = @_;
211 $queue->write_job_status($machine, $jid, $s);
212 $queue->log($machine, $jid, $stat, $msg);
216 my ($queue, $machine, $jid) = @_;
217 my $lock = $queue->{'Path'};
219 $lock .= "/hosts/$machine/$jid.lock";
220 } elsif (defined $machine) {
221 $lock .= "/hosts/$machine/lock";
227 # Whenever we want to run a job on a machine, we must obtain a lock;
228 # at most one lock can be held at a time by a single BEX::Queue object.
229 # See the description of locking schemes in BEX::Config.
231 my ($queue, $machine, $jid) = @_;
233 given ($BEX::Config::locking_scheme) {
235 $lock = lock_name($queue, undef, undef);
238 defined($machine) or return 1;
239 $lock = lock_name($queue, $machine, undef);
242 defined($machine) && defined($jid) or return 1;
243 $lock = lock_name($queue, $machine, $jid);
245 when ('none') { return 1; }
246 default { die "Invalid BEX::Config::locking_scheme"; }
248 if (defined($queue->{'LockName'})) {
249 return 1 if ($queue->{'LockName'} eq $lock);
252 open $queue->{'LockHandle'}, '>>', $lock or die "Cannot create $lock: $!";
253 if (!flock($queue->{'LockHandle'}, LOCK_EX | LOCK_NB)) {
254 close $queue->{'LockHandle'};
255 delete $queue->{'LockHandle'};
258 $queue->{'LockName'} = $lock;
264 defined $queue->{'LockName'} or return;
265 unlink $queue->{'LockName'};
266 flock $queue->{'LockHandle'}, LOCK_UN;
267 close $queue->{'LockHandle'};
268 delete $queue->{'LockHandle'};
269 delete $queue->{'LockName'};
272 # Unsafe (does not check fcntl, only existence of a lock file), but should be enough for `bex ls'
274 my ($queue, $machine, $jid) = @_;
275 given ($BEX::Config::locking_scheme) {
277 when ('host') { return unless defined $machine; }
278 when ('jid') { return unless defined $jid; }
279 when ('none') { return; }
281 my $lock = lock_name($queue, $machine, $jid);