2013-05-15 19:27:13 +02:00
|
|
|
<?php
|
|
|
|
|
2016-07-19 20:28:11 +02:00
|
|
|
namespace PHPCensor\Command;
|
2013-05-15 19:27:13 +02:00
|
|
|
|
2014-12-01 18:04:03 +01:00
|
|
|
use b8\Config;
|
2013-10-26 17:11:46 +02:00
|
|
|
use Monolog\Logger;
|
2016-07-19 20:28:11 +02:00
|
|
|
use PHPCensor\Logging\BuildDBLogHandler;
|
|
|
|
use PHPCensor\Logging\LoggedBuildContextTidier;
|
|
|
|
use PHPCensor\Logging\OutputLogHandler;
|
2017-02-01 15:53:31 +01:00
|
|
|
use PHPCensor\Store\BuildStore;
|
2013-05-15 19:27:13 +02:00
|
|
|
use Symfony\Component\Console\Command\Command;
|
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
use b8\Store\Factory;
|
2016-07-19 20:28:11 +02:00
|
|
|
use PHPCensor\Builder;
|
|
|
|
use PHPCensor\BuildFactory;
|
|
|
|
use PHPCensor\Model\Build;
|
2013-05-15 19:27:13 +02:00
|
|
|
|
2013-05-16 03:30:48 +02:00
|
|
|
/**
|
2017-01-20 14:05:25 +01:00
|
|
|
* Run console command - Runs any pending builds.
|
|
|
|
*
|
2017-02-05 05:18:33 +01:00
|
|
|
* @author Dan Cryer <dan@block8.co.uk>
|
2017-01-20 14:05:25 +01:00
|
|
|
*/
|
2013-05-15 19:27:13 +02:00
|
|
|
class RunCommand extends Command
|
|
|
|
{
|
2013-10-27 15:26:37 +01:00
|
|
|
/**
|
|
|
|
* @var OutputInterface
|
|
|
|
*/
|
|
|
|
protected $output;
|
2013-10-26 17:11:46 +02:00
|
|
|
|
2013-11-02 16:32:51 +01:00
|
|
|
/**
|
|
|
|
* @var Logger
|
|
|
|
*/
|
|
|
|
protected $logger;
|
|
|
|
|
2014-05-06 17:43:47 +02:00
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*/
|
2017-02-24 04:19:52 +01:00
|
|
|
protected $maxBuilds = 10;
|
2014-05-06 17:43:47 +02:00
|
|
|
|
2013-11-02 16:32:51 +01:00
|
|
|
/**
|
|
|
|
* @param \Monolog\Logger $logger
|
|
|
|
* @param string $name
|
|
|
|
*/
|
|
|
|
public function __construct(Logger $logger, $name = null)
|
|
|
|
{
|
|
|
|
parent::__construct($name);
|
|
|
|
$this->logger = $logger;
|
|
|
|
}
|
|
|
|
|
2013-05-15 19:27:13 +02:00
|
|
|
protected function configure()
|
|
|
|
{
|
|
|
|
$this
|
2016-07-21 19:02:11 +02:00
|
|
|
->setName('php-censor:run-builds')
|
2017-02-05 05:18:33 +01:00
|
|
|
->setDescription('Run all pending PHP Censor builds')
|
|
|
|
->addOption('debug', null, null, 'Run PHP Censor in debug mode');
|
2013-05-15 19:27:13 +02:00
|
|
|
}
|
|
|
|
|
2013-05-16 18:17:29 +02:00
|
|
|
/**
|
2017-02-01 15:53:31 +01:00
|
|
|
* Pulls all pending builds from the database or queue and runs them.
|
2013-10-27 15:26:37 +01:00
|
|
|
*/
|
2013-05-15 19:27:13 +02:00
|
|
|
protected function execute(InputInterface $input, OutputInterface $output)
|
|
|
|
{
|
|
|
|
$this->output = $output;
|
|
|
|
|
2013-10-27 15:26:37 +01:00
|
|
|
// For verbose mode we want to output all informational and above
|
|
|
|
// messages to the symphony output interface.
|
2014-12-02 17:26:55 +01:00
|
|
|
if ($input->hasOption('verbose') && $input->getOption('verbose')) {
|
2013-11-02 16:32:51 +01:00
|
|
|
$this->logger->pushHandler(
|
2013-10-27 15:26:37 +01:00
|
|
|
new OutputLogHandler($this->output, Logger::INFO)
|
|
|
|
);
|
|
|
|
}
|
2013-10-26 17:11:46 +02:00
|
|
|
|
2017-02-01 15:53:31 +01:00
|
|
|
// Allow PHP Censor to run in "debug mode"
|
2016-07-10 10:13:59 +02:00
|
|
|
if ($input->hasOption('debug') && $input->getOption('debug')) {
|
|
|
|
$output->writeln('<comment>Debug mode enabled.</comment>');
|
2016-07-21 17:20:34 +02:00
|
|
|
define('DEBUG_MODE', true);
|
2016-07-10 10:13:59 +02:00
|
|
|
}
|
|
|
|
|
2014-12-01 18:04:03 +01:00
|
|
|
$running = $this->validateRunningBuilds();
|
2013-11-02 16:32:51 +01:00
|
|
|
|
2014-12-01 18:04:03 +01:00
|
|
|
$this->logger->pushProcessor(new LoggedBuildContextTidier());
|
2017-02-05 05:18:33 +01:00
|
|
|
$this->logger->addInfo('Finding builds to process');
|
2017-02-01 15:53:31 +01:00
|
|
|
|
|
|
|
/** @var BuildStore $store */
|
|
|
|
$store = Factory::getStore('Build');
|
|
|
|
$result = $store->getByStatus(Build::STATUS_PENDING, $this->maxBuilds);
|
|
|
|
|
2017-02-05 05:18:33 +01:00
|
|
|
$this->logger->addInfo(sprintf('Found %d builds', count($result['items'])));
|
2013-11-02 16:32:51 +01:00
|
|
|
|
|
|
|
$builds = 0;
|
2013-10-27 15:25:43 +01:00
|
|
|
|
2014-12-02 17:26:55 +01:00
|
|
|
while (count($result['items'])) {
|
|
|
|
$build = array_shift($result['items']);
|
2013-05-16 00:47:37 +02:00
|
|
|
$build = BuildFactory::getBuild($build);
|
2013-06-09 18:42:50 +02:00
|
|
|
|
2014-12-01 18:04:03 +01:00
|
|
|
// Skip build (for now) if there's already a build running in that project:
|
2017-01-20 14:05:25 +01:00
|
|
|
if (in_array($build->getProjectId(), $running)) {
|
2017-02-05 05:18:33 +01:00
|
|
|
$this->logger->addInfo(sprintf('Skipping Build %d - Project build already in progress.', $build->getId()));
|
2014-12-02 17:26:55 +01:00
|
|
|
$result['items'][] = $build;
|
|
|
|
|
|
|
|
// Re-run build validator:
|
|
|
|
$running = $this->validateRunningBuilds();
|
2014-12-01 18:04:03 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$builds++;
|
|
|
|
|
2014-02-25 11:16:58 +01:00
|
|
|
try {
|
|
|
|
// Logging relevant to this build should be stored
|
|
|
|
// against the build itself.
|
|
|
|
$buildDbLog = new BuildDBLogHandler($build, Logger::INFO);
|
|
|
|
$this->logger->pushHandler($buildDbLog);
|
2013-06-09 18:42:50 +02:00
|
|
|
|
2014-02-25 11:16:58 +01:00
|
|
|
$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.
|
2016-05-09 08:20:26 +02:00
|
|
|
$this->logger->popHandler();
|
2017-03-30 22:26:35 +02:00
|
|
|
// destructor implicitly call flush
|
|
|
|
unset($buildDbLog);
|
2014-02-25 11:16:58 +01:00
|
|
|
} catch (\Exception $ex) {
|
|
|
|
$build->setStatus(Build::STATUS_FAILED);
|
2014-12-02 17:26:55 +01:00
|
|
|
$build->setFinished(new \DateTime());
|
2014-02-25 11:16:58 +01:00
|
|
|
$build->setLog($build->getLog() . PHP_EOL . PHP_EOL . $ex->getMessage());
|
|
|
|
$store->save($build);
|
|
|
|
}
|
2013-10-26 17:11:46 +02:00
|
|
|
|
2013-05-15 19:27:13 +02:00
|
|
|
}
|
2013-06-09 18:42:50 +02:00
|
|
|
|
2017-02-05 05:18:33 +01:00
|
|
|
$this->logger->addInfo('Finished processing builds.');
|
2013-11-02 16:32:51 +01:00
|
|
|
|
2013-06-09 18:42:50 +02:00
|
|
|
return $builds;
|
2013-05-15 19:27:13 +02:00
|
|
|
}
|
2014-05-06 17:43:47 +02:00
|
|
|
|
2014-12-01 18:04:03 +01:00
|
|
|
public function setMaxBuilds($numBuilds)
|
2014-05-06 17:43:47 +02:00
|
|
|
{
|
|
|
|
$this->maxBuilds = (int)$numBuilds;
|
|
|
|
}
|
2014-12-01 18:04:03 +01:00
|
|
|
|
|
|
|
protected function validateRunningBuilds()
|
|
|
|
{
|
2016-07-21 19:20:59 +02:00
|
|
|
/** @var \PHPCensor\Store\BuildStore $store */
|
2016-04-20 17:39:48 +02:00
|
|
|
$store = Factory::getStore('Build');
|
2017-02-01 15:53:31 +01:00
|
|
|
$running = $store->getByStatus(Build::STATUS_RUNNING);
|
2016-04-20 17:39:48 +02:00
|
|
|
$rtn = [];
|
2014-12-01 18:04:03 +01:00
|
|
|
|
2016-07-21 19:02:11 +02:00
|
|
|
$timeout = Config::getInstance()->get('php-censor.build.failed_after', 1800);
|
2014-12-01 18:04:03 +01:00
|
|
|
|
|
|
|
foreach ($running['items'] as $build) {
|
2016-07-21 19:20:59 +02:00
|
|
|
/** @var \PHPCensor\Model\Build $build */
|
2014-12-01 18:04:03 +01:00
|
|
|
$build = BuildFactory::getBuild($build);
|
|
|
|
|
|
|
|
$now = time();
|
|
|
|
$start = $build->getStarted()->getTimestamp();
|
|
|
|
|
|
|
|
if (($now - $start) > $timeout) {
|
2017-02-05 05:18:33 +01:00
|
|
|
$this->logger->addInfo(sprintf('Build %d marked as failed due to timeout.', $build->getId()));
|
2014-12-01 18:04:03 +01:00
|
|
|
$build->setStatus(Build::STATUS_FAILED);
|
2014-12-02 17:26:55 +01:00
|
|
|
$build->setFinished(new \DateTime());
|
2014-12-01 18:04:03 +01:00
|
|
|
$store->save($build);
|
2015-02-27 10:51:18 +01:00
|
|
|
$build->removeBuildDirectory();
|
2014-12-01 18:04:03 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$rtn[$build->getProjectId()] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $rtn;
|
|
|
|
}
|
2013-05-16 16:14:10 +02:00
|
|
|
}
|