include 'alert_functions.inc';
include 'wpt_functions.inc';
include_once 'utils.inc';
require_once 'bootstrap.php';
$key = $_REQUEST['key'];
$configKey = getWptConfigFor('jobProcessorKey');
if ($configKey != $key) {
    print "Invalid Key";
    exit;
}
checkQueueGrowthCountAndEmailAlert();
try {
    $users = Doctrine_Core::getTable('User')->findAll();
    foreach ($users as $user) {
        if ($user->IsActive) {
            processJobsForUser($user['Id'], null, null, "Scheduled");
        }
    }
    // Process results for all
    processResultsForAll();
    $jobs = Doctrine_Core::getTable('WPTJob')->findAll();
    foreach ($jobs as $job) {
        processAlertsForJob($job['Id']);
    }
} catch (Exception $e) {
    error_log("[WPTMonitor] Failed while Listing Users: " . $wptResultId . " message: " . $e->getMessage());
    logOutput('[ERROR] [jobProcessor] Exception : ' . $e->getMessage());
}
updateQueueProcessRate();
checkTesterRatioAndEmailAlert();
?>
if (isset($_REQUEST['force'])) {
    $force = $_REQUEST['force'];
}
if (isset($_REQUEST['priority'])) {
    $priority = $_REQUEST['priority'];
}
if (!isset($_REQUEST['numberofruns'])) {
    $numberOfRuns = 1;
} else {
    $numberOfRuns = $_REQUEST['numberofruns'];
}
$userId = getCurrentUserId();
if (isset($_REQUEST['runLabel'])) {
    $runLabel = $_REQUEST['runLabel'];
}
if ($jobId && $userId) {
    foreach ($jobId as $jid) {
        $runs = $numberOfRuns;
        while ($runs > 0) {
            processJobsForUser($userId, $jid, $force, $runLabel, $priority);
            $runs--;
        }
    }
} else {
    if ($resultId && $userId) {
        processResultsForAll($resultId);
    }
}
header("Location: " . $forwardTo);
/* Make sure that code below does not get executed when we redirect. */
exit;