public function __construct($queue, $jail) { $this->_db = Config::getDatabaseHandle(); if (in_array($queue, $this->_queues)) { $this->_queue = $queue; } else { $this->_queue = $this->_queues[0]; } $jails = new Jails(); if ($jails->exists($jail)) { $this->_jail = $jail; } else { trigger_error(E_USER_ERROR, 'Jail is unknown'); } }
public function push($payload) { $config = $this->_getUserConfig($payload['repository']['full_name'] . '/' . $payload['commits'][0]['id']); $ports = array(); foreach ($payload['commits'] as $commit) { foreach ($commit['added'] as $file) { $port = substr($file, 0, strpos($file, '/', strpos($file, '/') + 1)); if (preg_match('/^([a-zA-Z0-9_+.-]+)\\/([a-zA-Z0-9_+.-]+)$/', $port) == 1 && strlen($port) < 100) { $ports[] = $port; } } foreach ($commit['modified'] as $file) { $port = substr($file, 0, strpos($file, '/', strpos($file, '/') + 1)); if (preg_match('/^([a-zA-Z0-9_+.-]+)\\/([a-zA-Z0-9_+.-]+)$/', $port) == 1 && strlen($port) < 100) { $ports[] = $port; } } } $data = array('commit' => array('id' => $payload['head_commit']['id'], 'url' => $payload['head_commit']['url'], 'message' => $payload['head_commit']['message'], 'time' => $payload['head_commit']['timestamp']), 'committer' => array('name' => $payload['head_commit']['committer']['name'], 'email' => $payload['head_commit']['committer']['email']), 'repository' => array('url' => $payload['repository']['url'])); $ports = array_unique($ports); $jobgroupname = sprintf('github:%s:%s', $payload['repository']['owner']['name'], $payload['repository']['name']); $jobgroup = new Jobgroup($jobgroupname); $countjobs = $jobgroup->countJobs(); $jails = new Jails(); foreach ($config['jails'] as $jail) { if (!$jails->exists($jail)) { continue; } $queue = new Queue('preparequeue', $jail); foreach ($ports as $port) { $data['port'] = $port; if ($queue->createJob($data, $jobgroup) !== true) { return false; } } } if ($jobgroup->countJobs() > $countjobs) { $args = array('jobgroup' => $jobgroup->getJobgroupId(), 'repository' => $data['repository']['url'], 'commit' => $data['commit']['id'], 'created' => time()); \Resque::enqueue('default', '\\Redports\\Master\\Task\\TaskPreparePortstree', $args); } return true; }
$app->get('/queues/waitqueue/:jailname/take', 'isAllowed', function ($jailname) use($app) { $queue = new Queue('waitqueue', $jailname); $job = $queue->getNextJob(); if ($job === false) { textResponse(204); } $machine = new Machine(Session::getMachineId()); $machine->addJob($job->getJobId()); $job->set('machine', $machine->getName()); $job->moveToQueue('runqueue'); jsonResponse(200, $job->getJobData()); }); /* Jobs - Create new job */ $app->post('/jobs/create', 'isAllowed', function () use($app) { $queue = new Queue(); $jail = new Jails(); if (!$jail->exists($_POST['jail'])) { textResponse(404, 'Jail unknown'); } $repos = new Repositories(); if (!$repos->exists($_POST['repository'])) { textResponse(404, 'Repository unknown'); } $jobgroup = new Jobgroup($_POST['jobgroup']); if ($jobgroup->exists()) { textResponse(403, 'Jobgroup already exists'); } $data = array('port' => $_POST['port'], 'jail' => $_POST['jail'], 'repository' => $_POST['repository'], 'jobgroup' => $_POST['jobgroup']); $job = $queue->createJob($data); $jobgroup->addJob($job->getJobId()); jsonResponse(200, $job->getJobData());