2015-10-05 13:13:22 +02:00
|
|
|
<?php
|
|
|
|
|
2016-07-19 20:28:11 +02:00
|
|
|
namespace PHPCensor\Worker;
|
2015-10-05 13:13:22 +02:00
|
|
|
|
|
|
|
use b8\Store\Factory;
|
|
|
|
use Monolog\Logger;
|
2015-10-09 10:27:45 +02:00
|
|
|
use Pheanstalk\Job;
|
2015-10-05 13:13:22 +02:00
|
|
|
use Pheanstalk\Pheanstalk;
|
2016-07-19 20:28:11 +02:00
|
|
|
use PHPCensor\Builder;
|
|
|
|
use PHPCensor\BuildFactory;
|
|
|
|
use PHPCensor\Logging\BuildDBLogHandler;
|
|
|
|
use PHPCensor\Model\Build;
|
2015-10-05 13:13:22 +02:00
|
|
|
|
2015-10-05 15:11:43 +02:00
|
|
|
/**
|
|
|
|
* Class BuildWorker
|
2016-07-21 19:20:59 +02:00
|
|
|
* @package PHPCensor\Worker
|
2015-10-05 15:11:43 +02:00
|
|
|
*/
|
2015-10-05 13:13:22 +02:00
|
|
|
class BuildWorker
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* If this variable changes to false, the worker will stop after the current build.
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
protected $run = true;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The logger for builds to use.
|
|
|
|
* @var \Monolog\Logger
|
|
|
|
*/
|
|
|
|
protected $logger;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* beanstalkd host
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $host;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* beanstalkd queue to watch
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $queue;
|
|
|
|
|
2015-10-09 10:27:45 +02:00
|
|
|
/**
|
|
|
|
* @var \Pheanstalk\Pheanstalk
|
|
|
|
*/
|
|
|
|
protected $pheanstalk;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
protected $totalJobs = 0;
|
|
|
|
|
2015-10-05 13:13:22 +02:00
|
|
|
/**
|
|
|
|
* @param $host
|
|
|
|
* @param $queue
|
|
|
|
*/
|
|
|
|
public function __construct($host, $queue)
|
|
|
|
{
|
2017-02-01 15:53:31 +01:00
|
|
|
$this->host = $host;
|
|
|
|
$this->queue = $queue;
|
2015-10-09 10:27:45 +02:00
|
|
|
$this->pheanstalk = new Pheanstalk($this->host);
|
2015-10-05 13:13:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Logger $logger
|
|
|
|
*/
|
|
|
|
public function setLogger(Logger $logger)
|
|
|
|
{
|
|
|
|
$this->logger = $logger;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Start the worker.
|
|
|
|
*/
|
|
|
|
public function startWorker()
|
|
|
|
{
|
2015-10-09 10:27:45 +02:00
|
|
|
$this->pheanstalk->watch($this->queue);
|
|
|
|
$this->pheanstalk->ignore('default');
|
2015-10-05 13:13:22 +02:00
|
|
|
$buildStore = Factory::getStore('Build');
|
|
|
|
|
|
|
|
while ($this->run) {
|
|
|
|
// Get a job from the queue:
|
2015-10-09 10:27:45 +02:00
|
|
|
$job = $this->pheanstalk->reserve();
|
2015-10-05 13:13:22 +02:00
|
|
|
|
|
|
|
// Get the job data and run the job:
|
|
|
|
$jobData = json_decode($job->getData(), true);
|
2015-10-06 12:38:09 +02:00
|
|
|
|
2015-10-09 10:27:45 +02:00
|
|
|
if (!$this->verifyJob($job, $jobData)) {
|
2015-10-06 12:38:09 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-10-05 13:13:22 +02:00
|
|
|
$this->logger->addInfo('Received build #'.$jobData['build_id'].' from Beanstalkd');
|
|
|
|
|
2015-10-08 18:30:34 +02:00
|
|
|
try {
|
|
|
|
$build = BuildFactory::getBuildById($jobData['build_id']);
|
|
|
|
} catch (\Exception $ex) {
|
2015-10-05 13:13:22 +02:00
|
|
|
$this->logger->addWarning('Build #' . $jobData['build_id'] . ' does not exist in the database.');
|
2015-10-09 10:27:45 +02:00
|
|
|
$this->pheanstalk->delete($job);
|
2017-04-05 15:21:57 +02:00
|
|
|
continue;
|
2015-10-05 13:13:22 +02:00
|
|
|
}
|
|
|
|
|
2017-04-04 22:12:20 +02:00
|
|
|
// Logging relevant to this build should be stored
|
|
|
|
// against the build itself.
|
|
|
|
$buildDbLog = new BuildDBLogHandler($build, Logger::INFO);
|
|
|
|
$this->logger->pushHandler($buildDbLog);
|
2015-10-05 13:13:22 +02:00
|
|
|
|
2017-04-04 22:12:20 +02:00
|
|
|
try {
|
2015-10-05 13:13:22 +02:00
|
|
|
$builder = new Builder($build, $this->logger);
|
|
|
|
$builder->execute();
|
|
|
|
} 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;
|
2015-10-09 10:27:45 +02:00
|
|
|
$this->pheanstalk->release($job);
|
2017-04-05 16:09:55 +02:00
|
|
|
unset($job);
|
2015-10-05 13:13:22 +02:00
|
|
|
} catch (\Exception $ex) {
|
2017-05-23 17:48:03 +02:00
|
|
|
$this->logger->addError($ex->getMessage());
|
|
|
|
|
2015-10-05 13:13:22 +02:00
|
|
|
$build->setStatus(Build::STATUS_FAILED);
|
2017-10-15 16:58:36 +02:00
|
|
|
$build->setFinishDate(new \DateTime());
|
2015-10-05 13:13:22 +02:00
|
|
|
$build->setLog($build->getLog() . PHP_EOL . PHP_EOL . $ex->getMessage());
|
|
|
|
$buildStore->save($build);
|
|
|
|
$build->sendStatusPostback();
|
|
|
|
}
|
|
|
|
|
2017-04-04 22:12:20 +02:00
|
|
|
// After execution we no longer want to record the information
|
|
|
|
// back to this specific build so the handler should be removed.
|
|
|
|
$this->logger->popHandler();
|
|
|
|
// destructor implicitly call flush
|
|
|
|
unset($buildDbLog);
|
|
|
|
|
2015-10-05 13:13:22 +02:00
|
|
|
// Delete the job when we're done:
|
2017-04-05 16:09:55 +02:00
|
|
|
if (!empty($job)) {
|
|
|
|
$this->pheanstalk->delete($job);
|
|
|
|
}
|
2015-10-05 13:13:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stops the worker after the current build.
|
|
|
|
*/
|
|
|
|
public function stopWorker()
|
|
|
|
{
|
|
|
|
$this->run = false;
|
|
|
|
}
|
2015-10-09 10:27:45 +02:00
|
|
|
|
2015-10-09 10:38:55 +02:00
|
|
|
/**
|
|
|
|
* Checks that the job received is actually from PHPCI, and has a valid type.
|
|
|
|
* @param Job $job
|
|
|
|
* @param $jobData
|
|
|
|
* @return bool
|
|
|
|
*/
|
2015-10-09 10:27:45 +02:00
|
|
|
protected function verifyJob(Job $job, $jobData)
|
|
|
|
{
|
|
|
|
if (empty($jobData) || !is_array($jobData)) {
|
|
|
|
// Probably not from PHPCI.
|
2015-10-09 10:38:55 +02:00
|
|
|
$this->pheanstalk->delete($job);
|
2015-10-09 10:27:45 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-07-21 19:02:11 +02:00
|
|
|
if (!array_key_exists('type', $jobData) || $jobData['type'] !== 'php-censor.build') {
|
2015-10-09 10:27:45 +02:00
|
|
|
// Probably not from PHPCI.
|
|
|
|
$this->pheanstalk->delete($job);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2015-10-05 13:13:22 +02:00
|
|
|
}
|