Permalink
Browse files

Merge branch 'worker-improvements'

  • Loading branch information...
corpsee committed Oct 7, 2018
2 parents d6d11ad + f5e2c23 commit b747265b5f7bc5ea0a120e3fce59a1f34d0c2e2a
View
@@ -47,7 +47,7 @@
"require": {
"php": ">=5.6.0",
"ext-openssl": "*",
"ext-pdo": "*",
"ext-PDO": "*",
"ext-json": "*",
"ext-xml": "*",
"ext-curl": "*",
View
@@ -119,6 +119,14 @@ public function __construct(Build $build, LoggerInterface $logger = null)
$this->buildErrorWriter = new BuildErrorWriter($this->build->getProjectId(), $this->build->getId());
}
/**
* @return BuildLogger
*/
public function getBuildLogger()
{
return $this->buildLogger;
}
/**
* @return null|string
*/
@@ -180,24 +188,13 @@ public function getBuildProjectTitle()
return $this->build->getProject()->getTitle();
}
/**
* Run the active build.
*/
public function execute()
{
// check current status
if ($this->build->getStatus() != Build::STATUS_PENDING) {
throw new BuilderException('Can`t build - status is not pending', BuilderException::FAIL_START);
}
// set status only if current status pending
if (!$this->build->setStatusSync(Build::STATUS_RUNNING)) {
throw new BuilderException('Can`t build - unable change status to running', BuilderException::FAIL_START);
}
// Update the build in the database, ping any external services.
$this->build->setStatusRunning();
$this->build->setStartDate(new \DateTime());
$this->store->save($this->build);
$this->build->sendStatusPostback();
$success = true;
$previousBuild = $this->build->getProject()->getPreviousBuild($this->build->getBranch());
@@ -223,13 +220,13 @@ public function execute()
// Set the status so this can be used by complete, success and failure
// stages.
if ($success) {
$this->build->setStatus(Build::STATUS_SUCCESS);
$this->build->setStatusSuccess();
} else {
$this->build->setStatus(Build::STATUS_FAILED);
$this->build->setStatusFailed();
}
} catch (\Exception $ex) {
$success = false;
$this->build->setStatus(Build::STATUS_FAILED);
$this->build->setStatusFailed();
$this->buildLogger->logFailure('Exception: ' . $ex->getMessage(), $ex);
}
@@ -255,10 +252,11 @@ public function execute()
$this->buildLogger->logFailure('Exception: ' . $ex->getMessage(), $ex);
}
$this->buildLogger->log('');
if (Build::STATUS_FAILED === $this->build->getStatus()) {
$this->buildLogger->logFailure("\nBUILD FAILED");
$this->buildLogger->logFailure('BUILD FAILED!');
} else {
$this->buildLogger->logSuccess("\nBUILD SUCCESS");
$this->buildLogger->logSuccess('BUILD SUCCESS!');
}
try {
@@ -276,7 +274,8 @@ public function execute()
$removeBuilds = (bool)Config::getInstance()->get('php-censor.build.remove_builds', true);
if ($removeBuilds) {
// Clean up:
$this->buildLogger->log("\nRemoving Build.");
$this->buildLogger->log('');
$this->buildLogger->logSuccess('REMOVING BUILD.');
$this->build->removeBuildDirectory();
}
View

This file was deleted.

Oops, something went wrong.
@@ -0,0 +1,74 @@
<?php
namespace PHPCensor\Command;
use Monolog\Handler\AbstractHandler;
use Monolog\Logger;
use PHPCensor\Logging\OutputLogHandler;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
abstract class LoggingCommand extends Command
{
/**
* @var Logger
*/
protected $logger;
/**
* @param \Monolog\Logger $logger
* @param string $name
*/
public function __construct(Logger $logger, $name = null)
{
parent::__construct($name);
$this->logger = $logger;
}
/**
* @param OutputInterface $output
*/
protected function configureLogging(OutputInterface $output)
{
$level = Logger::ERROR;
if ($output->isDebug()) {
$level = Logger::DEBUG;
} elseif ($output->isVeryVerbose()) {
$level = Logger::INFO;
} elseif ($output->isVerbose()) {
$level = Logger::NOTICE;
}
$handlers = $this->logger->getHandlers();
foreach ($handlers as $handler) {
if ($handler instanceof AbstractHandler) {
$handler->setLevel($level);
}
}
if (!$output->isQuiet()) {
$this->logger->pushHandler(
new OutputLogHandler($output, $level)
);
}
if ($output->isDebug()) {
$this->logger->notice(
sprintf('Command "%s" started in debug mode (-vvv).', $this->getName())
);
define('DEBUG_MODE', true);
}
}
/**
* @param InputInterface $input
* @param OutputInterface $output
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->configureLogging($output);
}
}
View
@@ -4,10 +4,7 @@
use Monolog\Logger;
use PHPCensor\Logging\BuildDBLogHandler;
use PHPCensor\Logging\LoggedBuildContextTidier;
use PHPCensor\Logging\OutputLogHandler;
use PHPCensor\Store\BuildStore;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use PHPCensor\Store\Factory;
@@ -20,109 +17,65 @@
*
* @author Dan Cryer <dan@block8.co.uk>
*/
class RunCommand extends Command
class RunCommand extends LoggingCommand
{
/**
* @var OutputInterface
*/
protected $output;
/**
* @var Logger
*/
protected $logger;
/**
* @var int
*/
protected $maxBuilds = 10;
/**
* @param \Monolog\Logger $logger
* @param string $name
*/
public function __construct(Logger $logger, $name = null)
{
parent::__construct($name);
$this->logger = $logger;
}
protected function configure()
{
$this
->setName('php-censor:run-builds')
->addOption('debug', null, null, 'Run PHP Censor in debug mode')
->setDescription('Run all pending PHP Censor builds');
}
/**
* Pulls all pending builds from the database or queue and runs them.
*
* @param InputInterface $input
* @param OutputInterface $output
*
* @return int
*
* @throws \PHPCensor\Exception\HttpException
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->output = $output;
// For verbose mode we want to output all informational and above
// messages to the symphony output interface.
if ($input->hasOption('verbose') && $input->getOption('verbose')) {
$this->logger->pushHandler(
new OutputLogHandler($this->output, Logger::INFO)
);
}
// Allow PHP Censor to run in "debug mode"
if ($input->hasOption('debug') && $input->getOption('debug')) {
$output->writeln('<comment>Debug mode enabled.</comment>');
define('DEBUG_MODE', true);
}
$this->logger->pushProcessor(new LoggedBuildContextTidier());
$this->logger->addInfo('Finding builds to process');
parent::execute($input, $output);
/** @var BuildStore $buildStore */
$buildStore = Factory::getStore('Build');
$result = $buildStore->getByStatus(Build::STATUS_PENDING, $this->maxBuilds);
$this->logger->addInfo(sprintf('Found %d builds', count($result['items'])));
$this->logger->notice(
sprintf('Found %d pending builds', count($result['items']))
);
$builds = 0;
while (count($result['items'])) {
$builds++;
$build = array_shift($result['items']);
$build = BuildFactory::getBuild($build);
// Skip build (for now) if there's already a build running in that project:
if (!empty($running[$build->getProjectId()])) {
$this->logger->addInfo(sprintf('Skipping Build %d - Project build already in progress.', $build->getId()));
continue;
}
$builds++;
// 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);
try {
$builder = new Builder($build, $this->logger);
$builder->execute();
} catch (\Exception $ex) {
$this->logger->addError($ex->getMessage());
$build->setStatus(Build::STATUS_FAILED);
$builder->getBuildLogger()->log('');
$builder->getBuildLogger()->logFailure(
sprintf(
'BUILD FAILED! Exception: %s',
$build->getId(),
$ex->getMessage()
),
$ex
);
$build->setStatusFailed();
$build->setFinishDate(new \DateTime());
$build->setLog($build->getLog() . PHP_EOL . PHP_EOL . $ex->getMessage());
$buildStore->save($build);
$build->sendStatusPostback();
}
@@ -133,7 +86,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
unset($buildDbLog);
}
$this->logger->addInfo('Finished processing builds.');
$this->logger->notice('Finished processing builds.');
return $builds;
}
Oops, something went wrong.

0 comments on commit b747265

Please sign in to comment.