# Batch EXecutor 2.0 -- Queues
# (c) 2011 Martin Mares <mj@ucw.cz>

use strict;
use warnings;

package BEX::Queue;

use IO::File;
use POSIX ();

sub new($;$) {
	my ($class, $name) = @_;
	$name //= 'queue';
	-d $name or die "Queue directory $name does not exist\n";
	for my $d ("hosts", "jobs") {
		-d "$name/$d" or mkdir "$name/$d" or die "Cannot create directory $name/$d: $!";
	}
	my $queue = {
		'Name' => $name,
		'MetaCache' => {},
	};
	return bless $queue;
}

sub log_file($$) {
	my ($queue, $machine, $jid) = @_;
	return $queue->host_dir($machine) . '/' . $jid . '.log';
}

# Most actions have to be logged by the caller
sub log($$$$;$) {
	my ($queue, $mach, $jid, $stat, $msg) = @_;
	my $t = POSIX::strftime("%Y-%m-%d %H:%M:%S", localtime);
	my $m = join(" ", $t, $mach, $jid, $stat);
	$m .= " $msg" if defined $msg;

	my $fh = $queue->{'LogFH'} //= new IO::File $queue->{'Name'} . '/log', '>>' or die "Cannot open log: $!";
	print $fh "$m\n";

	# Append to the per-job log file
	if (open L, '>>', $queue->log_file($mach, $jid)) {
		print L "### $m\n";
		close L;
	}
}

sub host_dir($$) {
	my ($queue, $machine) = @_;
	return $queue->{'Name'} . '/hosts/' . $machine;
}

sub queue_file($$) {
	my ($queue, $machine, $jid) = @_;
	return $queue->host_dir($machine) . '/' . $jid . '.job';
}

sub status_file($$) {
	my ($queue, $machine, $jid) = @_;
	return $queue->host_dir($machine) . '/' . $jid . '.stat';
}

sub temp_file($$) {
	my ($queue, $machine, $jid) = @_;
	return $queue->host_dir($machine) . '/' . $jid . '.tmp';
}

sub job_file($$) {
	my ($queue, $jid) = @_;
	return $queue->{'Name'} . '/jobs/' . $jid. '.job';
}

sub enqueue($$$) {
	my ($queue, $machine, $job) = @_;
	my $qf = $queue->queue_file($machine, $job->id);
	if (-f $qf) {
		$queue->log($machine, $job->id, 'REQUEUE');
		return 0;
	}
	my $fn = $queue->job_file($job->id);
	-f $fn or $job->save($fn);
	my $dir = $queue->host_dir($machine);
	-d $dir or mkdir $dir or die "Cannot create directory $dir: $!";
	symlink '../../jobs/' . $job->id . '.job', $qf or die "Cannot create $qf: $!";
	$queue->log($machine, $job->id, 'QUEUE');
	return 1;
}

sub scan($$) {
	my ($queue, $machine) = @_;
	my @list = ();
	if (opendir D, $queue->host_dir($machine)) {
		while ($_ = readdir D) {
			/^\./ and next;
			s{\.job}{} or next;
			push @list, $_;
		}
		closedir D;
	}
	return @list;
}

sub remove($$) {
	my ($queue, $machine, $jid) = @_;
	unlink $queue->queue_file($machine, $jid);
	unlink $queue->status_file($machine, $jid);
	unlink $queue->temp_file($machine, $jid);
	unlink $queue->log_file($machine, $jid);
}

sub job_metadata($$) {
	my ($queue, $jid) = @_;
	my $cache = $queue->{'MetaCache'};
	if (!defined $cache->{$jid}) {
		$cache->{$jid} = BEX::Job->new_from_file($queue->job_file($jid), 1);
	}
	return $cache->{$jid};
}

sub read_job_status($$$) {
	my ($queue, $machine, $jid) = @_;
	my %s = ();
	my $sf = $queue->status_file($machine, $jid);
	if (open S, '<', $sf) {
		while (<S>) {
			chomp;
			/^(\w+):\s*(.*)/ or die "Parse error in $sf";
			$s{$1} = $2;
		}
		close S;
	}
	return \%s;
}

sub write_job_status($$$$) {
	my ($queue, $machine, $jid, $stat) = @_;
	my $sf = $queue->status_file($machine, $jid);
	open S, '>', $sf or die "Cannot create $sf: $!";
	for my $k (sort keys %$stat) {
		print S "$k: ", $stat->{$k}, "\n";
	}
	close S;
}

42;
