Permalink
Browse files

Improved worker logging.

  • Loading branch information...
corpsee committed Oct 6, 2018
1 parent 62e7f15 commit f5e2c23a7dae92f8cfb7e84ce2a36017cebc3c03
View
@@ -252,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 {
@@ -273,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();
}
@@ -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,108 +17,62 @@
*
* @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());
$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);
@@ -135,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;
}
@@ -3,10 +3,7 @@
namespace PHPCensor\Command;
use PHPCensor\Config;
use Monolog\Logger;
use PHPCensor\Logging\OutputLogHandler;
use PHPCensor\Worker\BuildWorker;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
@@ -15,35 +12,12 @@
*
* @author Dan Cryer <dan@block8.co.uk>
*/
class WorkerCommand extends Command
class WorkerCommand extends LoggingCommand
{
/**
* @var OutputInterface
*/
protected $output;
/**
* @var Logger
*/
protected $logger;
/**
* @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:worker')
->addOption('debug', null, null, 'Run PHP Censor in Debug Mode')
->setDescription('Runs the PHP Censor build worker.');
}
@@ -55,21 +29,7 @@ protected function configure()
*/
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 to run in "debug mode"
if ($input->hasOption('debug') && $input->getOption('debug')) {
$output->writeln('<comment>Debug mode enabled.</comment>');
define('DEBUG_MODE', true);
}
parent::execute($input, $output);
$config = Config::getInstance()->get('php-censor.queue', []);
if (empty($config['host']) || empty($config['name'])) {
@@ -78,9 +38,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
);
}
$worker = new BuildWorker($config['host'], $config['name']);
$worker->setLogger($this->logger);
$worker->startWorker();
(new BuildWorker($this->logger, $config['host'], $config['name']))
->startWorker();
}
}
@@ -2,6 +2,7 @@
namespace PHPCensor\Console;
use Monolog\Handler\HandlerInterface;
use Monolog\Handler\RotatingFileHandler;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
@@ -15,6 +16,7 @@
use PHPCensor\Command\ScheduleBuildCommand;
use PHPCensor\Command\WorkerCommand;
use PHPCensor\Config;
use PHPCensor\Logging\AnsiFormatter;
use PHPCensor\Logging\Handler;
use PHPCensor\Service\BuildService;
use PHPCensor\Store\BuildStore;
@@ -55,13 +57,16 @@ protected function initLogger(Config $applicationConfig)
$rotate = (bool)$applicationConfig->get('php-censor.log.rotate', false);
$maxFiles = (int)$applicationConfig->get('php-censor.log.max_files', 0);
/** @var HandlerInterface[] $loggerHandlers */
$loggerHandlers = [];
if ($rotate) {
$loggerHandlers[] = new RotatingFileHandler(RUNTIME_DIR . 'console.log', $maxFiles, Logger::DEBUG);
} else {
$loggerHandlers[] = new StreamHandler(RUNTIME_DIR . 'console.log', Logger::DEBUG);
}
$loggerHandlers[0]->setFormatter(new AnsiFormatter());
$logger = new Logger('php-censor', $loggerHandlers);
Handler::register($logger);
@@ -113,7 +113,7 @@ public function executeCommand($args = [])
}
if (!empty($this->lastError)) {
$this->logger->log("\033[0;31m" . $this->lastError . "\033[0m", LogLevel::ERROR);
$this->logger->logFailure($this->lastError);
}
$rtn = false;
View
@@ -139,6 +139,16 @@ public function send(Builder $builder = null)
$message->setCc($this->emailCc);
}
return $mailer->send($message);
ob_start();
$result = $mailer->send($message);
$rawOutput = ob_get_clean();
if ($rawOutput) {
$builder->getBuildLogger()->logWarning($rawOutput);
}
return $result;
}
}
Oops, something went wrong.

0 comments on commit f5e2c23

Please sign in to comment.