/** * Return an array of scheduled jobs * * @param string $start timestamp * @param string $end timestamp * * @return array array of jobs */ public function getJobs($start = 0, $end = null) { if ($end === null) { $end = (int) Service::Redis()->zcard(\ResqueScheduler\ResqueScheduler::QUEUE_NAME); } $timestamps = Service::Redis()->zrangebyscore(\ResqueScheduler\ResqueScheduler::QUEUE_NAME, $start, $end); if (empty($timestamps)) { return array(); } $pipelineCommands = array(); foreach ($timestamps as $key) { $pipelineCommands[] = array('llen', \ResqueScheduler\ResqueScheduler::QUEUE_NAME . ':' . $key); } $timestampLength = Service::Redis()->pipeline($pipelineCommands, \Redis::PIPELINE); $i = 0; $pipelineCommands = array(); foreach ($timestamps as $key) { $pipelineCommands[] = array('lrange', array(\ResqueScheduler\ResqueScheduler::QUEUE_NAME . ':' . $key, 0, (int) $timestampLength[$i++])); } $jobs = Service::Redis()->pipeline($pipelineCommands, \Redis::PIPELINE); $results = array(); $i = 0; foreach ($jobs as &$job) { foreach ($job as &$j) { $j = json_decode($j, true); $j['job_id'] = $j['args'][0]['id']; unset($j['args'][0]['id']); $j['args'] = var_export($j['args'][0], true); $j['time'] = date('c', $timestamps[$i]); $j['status'] = self::JOB_STATUS_SCHEDULED; $results[] = $j; } $i++; } return $results; }
/** * Construct the API class */ public function __construct() { $this->ResqueStatus = new \ResqueStatus\ResqueStatus(\ResqueBoard\Lib\Service\Service::Redis()); }
/** * Assign a status to each jobs * * @param array $jobs An array of jobs * * @since 1.0.0 * @return An array of jobs */ private function setJobStatus($jobs) { $jobIds = array_keys($jobs); $jobsCursor = Service::Mongo()->selectCollection(Service::$settings['Mongo']['database'], 'done_events')->find(array('d.job_id' => array('$in' => array_values($jobIds)))); foreach ($jobsCursor as $successJob) { $jobs[$successJob['d']['job_id']]['status'] = self::JOB_STATUS_COMPLETE; $jobs[$successJob['d']['job_id']]['took'] = $successJob['d']['time']; unset($jobIds[array_search($successJob['d']['job_id'], $jobIds)]); } if (!empty($jobIds)) { $jobsCursor = Service::Mongo()->selectCollection(Service::$settings['Mongo']['database'], 'fail_events')->find(array('d.job_id' => array('$in' => array_values($jobIds)))); $pipelineCommands = array(); foreach ($jobsCursor as $failedJob) { $jobs[$failedJob['d']['job_id']]['status'] = self::JOB_STATUS_FAILED; $jobs[$failedJob['d']['job_id']]['log'] = $failedJob['d']['log']; $jobs[$failedJob['d']['job_id']]['took'] = $failedJob['d']['time']; $pipelineCommands[] = array('get', 'failed:' . $failedJob['d']['job_id']); unset($jobIds[array_search($failedJob['d']['job_id'], $jobIds)]); } $failedTrace = array_filter(Service::Redis()->pipeline($pipelineCommands)); foreach ($failedTrace as $trace) { $trace = unserialize($trace); $jobs[$trace['payload']['id']]['trace'] = var_export($trace, true); } } if (!empty($jobIds)) { $jobsCursor = Service::Mongo()->selectCollection(Service::$settings['Mongo']['database'], 'process_events')->find(array('d.job_id' => array('$in' => array_values($jobIds)))); foreach ($jobsCursor as $processJob) { $jobs[$processJob['d']['job_id']]['status'] = self::JOB_STATUS_RUNNING; unset($jobIds[array_search($processJob['d']['job_id'], $jobIds)]); } } if (!empty($jobIds)) { foreach ($jobIds as $id) { $jobs[$id]['status'] = self::JOB_STATUS_WAITING; } } return array_values($jobs); }