host = $host; $this->queue = $queue; } /** * @param int $maxJobs */ public function setMaxJobs($maxJobs = -1) { $this->maxJobs = $maxJobs; } /** * @param Logger $logger */ public function setLogger(Logger $logger) { $this->logger = $logger; } /** * Start the worker. */ public function startWorker() { $pheanstalk = new Pheanstalk($this->host); $pheanstalk->watch($this->queue); $buildStore = Factory::getStore('Build'); $jobs = 0; while ($this->run) { // Get a job from the queue: $job = $pheanstalk->reserve(); // Make sure we don't run more than maxJobs jobs on this worker: $jobs++; if ($this->maxJobs != -1 && $this->maxJobs <= $jobs) { $this->run = false; } // Get the job data and run the job: $jobData = json_decode($job->getData(), true); $this->logger->addInfo('Received build #'.$jobData['build_id'].' from Beanstalkd'); // If the job comes with config data, reset our config and database connections // and then make sure we kill the worker afterwards: if (!empty($jobData['config'])) { $this->logger->addDebug('Using job-specific config.'); $currentConfig = Config::getInstance()->getArray(); $config = new Config($jobData['config']); Database::reset($config); } $build = BuildFactory::getBuildById($jobData['build_id']); if (empty($build)) { $this->logger->addWarning('Build #' . $jobData['build_id'] . ' does not exist in the database.'); $pheanstalk->delete($job); } try { // Logging relevant to this build should be stored // against the build itself. $buildDbLog = new BuildDBLogHandler($build, Logger::INFO); $this->logger->pushHandler($buildDbLog); $builder = new Builder($build, $this->logger); $builder->execute(); // After execution we no longer want to record the information // back to this specific build so the handler should be removed. $this->logger->popHandler($buildDbLog); } catch (\PDOException $ex) { // If we've caught a PDO Exception, it is probably not the fault of the build, but of a failed // connection or similar. Release the job and kill the worker. $this->run = false; $pheanstalk->release($job); } catch (\Exception $ex) { $build->setStatus(Build::STATUS_FAILED); $build->setFinished(new \DateTime()); $build->setLog($build->getLog() . PHP_EOL . PHP_EOL . $ex->getMessage()); $buildStore->save($build); $build->sendStatusPostback(); } // Reset the config back to how it was prior to running this job: if (!empty($currentConfig)) { $config = new Config($currentConfig); Database::reset($config); } // Delete the job when we're done: $pheanstalk->delete($job); } } /** * Stops the worker after the current build. */ public function stopWorker() { $this->run = false; } }