#!/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");

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