#!/usr/bin/env php
<?php

$opts = getopt("c:j:");

$class = null;
$jobid = null;

if (array_key_exists("c", $opts)) {
	$class = $opts['c'];
}

if (array_key_exists("j", $opts)) {
	$jobid = $opts['j'];
}

require_once(__DIR__ . "/../app.php");

print("Job Runner Starting\n");

$running = true;

while ($running) {
	$job = Job::consumeNextJob($class);
	if ($job) {
		print("Executing " . $job->getJobClass() . " job " . $job->jobID() . "\n");
		try {
			$job->run();
		} catch (PDOException $e) {
			print_r($e);
		} catch (Exception $e) {
			print_r($e);
		}
		print("Job finished\n");
	} else {
		sleep(1);
	}

	if (file_exists("/tmp/dpr_jobrunner_quit")) {
		unlink("/tmp/dpr_jobrunner_quit");
		$running = false;
	}
}

print("Job Runner Exited Cleanly\n");
