From 7e735bbb3b5484346fa0c6970ff9db084df13cbd Mon Sep 17 00:00:00 2001 From: Dmitry Khomutov Date: Wed, 1 Feb 2017 21:53:31 +0700 Subject: [PATCH] Queue improvements --- app/config.example.yml | 7 +-- src/PHPCensor/Builder.php | 4 +- src/PHPCensor/Command/InstallCommand.php | 46 +++++++++++-------- src/PHPCensor/Command/PollCommand.php | 2 +- src/PHPCensor/Command/RebuildCommand.php | 1 + src/PHPCensor/Command/RunCommand.php | 14 ++++-- src/PHPCensor/Controller/BuildController.php | 2 +- src/PHPCensor/Model/Build.php | 2 +- src/PHPCensor/Service/BuildStatusService.php | 2 +- src/PHPCensor/Store/Base/BuildStoreBase.php | 8 +++- src/PHPCensor/View/Build/header-row.phtml | 2 +- src/PHPCensor/View/Home/ajax-timeline.phtml | 2 +- src/PHPCensor/View/Home/index.phtml | 2 +- src/PHPCensor/Worker/BuildWorker.php | 4 +- .../PHPCensor/Command/InstallCommandTest.php | 2 +- tests/PHPCensor/Model/BuildTest.php | 2 +- tests/PHPCensor/Service/BuildServiceTest.php | 4 +- .../Service/BuiltStatusServiceTest.php | 2 +- 18 files changed, 65 insertions(+), 43 deletions(-) diff --git a/app/config.example.yml b/app/config.example.yml index 48e920eb..eeb36973 100644 --- a/app/config.example.yml +++ b/app/config.example.yml @@ -19,9 +19,10 @@ php-censor: from_address: 'no-reply@php-censor.local' smtp_address: queue: - host: localhost - name: php-censor-queue - lifetime: 600 + use_queue: true + host: localhost + name: php-censor-queue + lifetime: 600 github: token: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' comments: diff --git a/src/PHPCensor/Builder.php b/src/PHPCensor/Builder.php index 614c19fb..2eda61ac 100644 --- a/src/PHPCensor/Builder.php +++ b/src/PHPCensor/Builder.php @@ -190,7 +190,7 @@ class Builder implements LoggerAwareInterface $previous_build = $this->build->getProject()->getPreviousBuild($this->build->getBranch()); - $previous_state = Build::STATUS_NEW; + $previous_state = Build::STATUS_PENDING; if ($previous_build) { $previous_state = $previous_build->getStatus(); @@ -223,7 +223,7 @@ class Builder implements LoggerAwareInterface } else { $this->pluginExecutor->executePlugins($this->config, 'failure'); - if ($previous_state == Build::STATUS_SUCCESS || $previous_state == Build::STATUS_NEW) { + if ($previous_state == Build::STATUS_SUCCESS || $previous_state == Build::STATUS_PENDING) { $this->pluginExecutor->executePlugins($this->config, 'broken'); } } diff --git a/src/PHPCensor/Command/InstallCommand.php b/src/PHPCensor/Command/InstallCommand.php index eca0e9d9..d3a7c4bc 100644 --- a/src/PHPCensor/Command/InstallCommand.php +++ b/src/PHPCensor/Command/InstallCommand.php @@ -43,6 +43,7 @@ class InstallCommand extends Command $this ->setName('php-censor:install') + ->addOption('url', null, InputOption::VALUE_OPTIONAL, Lang::get('installation_url')) ->addOption('db-type', null, InputOption::VALUE_OPTIONAL, Lang::get('db_host')) ->addOption('db-host', null, InputOption::VALUE_OPTIONAL, Lang::get('db_host')) @@ -54,9 +55,10 @@ class InstallCommand extends Command ->addOption('admin-pass', null, InputOption::VALUE_OPTIONAL, Lang::get('admin_pass')) ->addOption('admin-mail', null, InputOption::VALUE_OPTIONAL, Lang::get('admin_email')) ->addOption('config-path', null, InputOption::VALUE_OPTIONAL, Lang::get('config_path'), $defaultPath) - ->addOption('queue-disabled', null, InputOption::VALUE_NONE, 'Don\'t ask for queue details') - ->addOption('queue-server', null, InputOption::VALUE_OPTIONAL, 'Beanstalkd queue server hostname') + ->addOption('queue-use', null, InputOption::VALUE_OPTIONAL, 'Don\'t ask for queue details') + ->addOption('queue-host', null, InputOption::VALUE_OPTIONAL, 'Beanstalkd queue server hostname') ->addOption('queue-name', null, InputOption::VALUE_OPTIONAL, 'Beanstalkd queue name') + ->setDescription(Lang::get('install_app')); } @@ -250,45 +252,53 @@ class InstallCommand extends Command $config['per_page'] = 10; $config['url'] = $url; - $config['queue'] = $this->getQueueInformation($input, $output, $helper); + $config['queue'] = $this->getQueueInformation($input, $output); return $config; } /** * If the user wants to use a queue, get the necessary details. + * * @param InputInterface $input * @param OutputInterface $output - * @param QuestionHelper $helper * @return array */ - protected function getQueueInformation(InputInterface $input, OutputInterface $output, QuestionHelper $helper) + protected function getQueueInformation(InputInterface $input, OutputInterface $output) { - if ($input->getOption('queue-disabled')) { - return null; + $skipQueueConfig = [ + 'queue-use' => false, + ]; + + if (!$input->getOption('queue-use')) { + return $skipQueueConfig; } - $rtn = []; + $queueConfig = [ + 'queue-use' => true, + ]; - $helper = $this->getHelper('question'); - $question = new ConfirmationQuestion('Use beanstalkd to manage build queue? ', true); + /** @var $helper QuestionHelper */ + $helper = $this->getHelper('question'); + $question = new ConfirmationQuestion('Use beanstalkd to manage build queue? ', false); if (!$helper->ask($input, $output, $question)) { $output->writeln('Skipping beanstalkd configuration.'); - return null; + + return $skipQueueConfig; } - if (!$rtn['host'] = $input->getOption('queue-server')) { - $questionQueue = new Question('Enter your beanstalkd hostname [localhost]: ', 'localhost'); - $rtn['host'] = $helper->ask($input, $output, $questionQueue); + if (!$queueConfig['host'] = $input->getOption('queue-host')) { + $questionQueue = new Question('Enter your beanstalkd hostname [localhost]: ', 'localhost'); + $queueConfig['host'] = $helper->ask($input, $output, $questionQueue); } - if (!$rtn['name'] = $input->getOption('queue-name')) { - $questionName = new Question('Enter the queue (tube) name to use [php-censor-queue]: ', 'php-censor-queue'); - $rtn['name'] = $helper->ask($input, $output, $questionName); + if (!$queueConfig['name'] = $input->getOption('queue-name')) { + $questionName = new Question('Enter the queue (tube) name to use [php-censor-queue]: ', 'php-censor-queue'); + $queueConfig['name'] = $helper->ask($input, $output, $questionName); } - return $rtn; + return $queueConfig; } /** diff --git a/src/PHPCensor/Command/PollCommand.php b/src/PHPCensor/Command/PollCommand.php index 32ac9a04..8c43a637 100644 --- a/src/PHPCensor/Command/PollCommand.php +++ b/src/PHPCensor/Command/PollCommand.php @@ -87,7 +87,7 @@ class PollCommand extends Command $build = new Build(); $build->setProjectId($project->getId()); $build->setCommitId($last_commit); - $build->setStatus(Build::STATUS_NEW); + $build->setStatus(Build::STATUS_PENDING); $build->setBranch($project->getBranch()); $build->setCreated(new \DateTime()); $build->setCommitMessage($message); diff --git a/src/PHPCensor/Command/RebuildCommand.php b/src/PHPCensor/Command/RebuildCommand.php index 31b10827..f3e06174 100644 --- a/src/PHPCensor/Command/RebuildCommand.php +++ b/src/PHPCensor/Command/RebuildCommand.php @@ -1,4 +1,5 @@ hasOption('debug') && $input->getOption('debug')) { $output->writeln('Debug mode enabled.'); define('DEBUG_MODE', true); @@ -90,8 +91,11 @@ class RunCommand extends Command $this->logger->pushProcessor(new LoggedBuildContextTidier()); $this->logger->addInfo(Lang::get('finding_builds')); - $store = Factory::getStore('Build'); - $result = $store->getByStatus(0, $this->maxBuilds); + + /** @var BuildStore $store */ + $store = Factory::getStore('Build'); + $result = $store->getByStatus(Build::STATUS_PENDING, $this->maxBuilds); + $this->logger->addInfo(Lang::get('found_n_builds', count($result['items']))); $builds = 0; @@ -147,7 +151,7 @@ class RunCommand extends Command { /** @var \PHPCensor\Store\BuildStore $store */ $store = Factory::getStore('Build'); - $running = $store->getByStatus(1); + $running = $store->getByStatus(Build::STATUS_RUNNING); $rtn = []; $timeout = Config::getInstance()->get('php-censor.build.failed_after', 1800); diff --git a/src/PHPCensor/Controller/BuildController.php b/src/PHPCensor/Controller/BuildController.php index 5f71f718..7a0ed2f0 100644 --- a/src/PHPCensor/Controller/BuildController.php +++ b/src/PHPCensor/Controller/BuildController.php @@ -272,7 +272,7 @@ class BuildController extends Controller public function ajaxQueue() { $rtn = [ - 'pending' => $this->formatBuilds($this->buildStore->getByStatus(Build::STATUS_NEW)), + 'pending' => $this->formatBuilds($this->buildStore->getByStatus(Build::STATUS_PENDING)), 'running' => $this->formatBuilds($this->buildStore->getByStatus(Build::STATUS_RUNNING)), ]; diff --git a/src/PHPCensor/Model/Build.php b/src/PHPCensor/Model/Build.php index fc553d59..4e7d420c 100644 --- a/src/PHPCensor/Model/Build.php +++ b/src/PHPCensor/Model/Build.php @@ -21,7 +21,7 @@ use Symfony\Component\Yaml\Parser as YamlParser; */ class Build extends BuildBase { - const STATUS_NEW = 0; + const STATUS_PENDING = 0; const STATUS_RUNNING = 1; const STATUS_SUCCESS = 2; const STATUS_FAILED = 3; diff --git a/src/PHPCensor/Service/BuildStatusService.php b/src/PHPCensor/Service/BuildStatusService.php index 0f29415d..dcde6ee6 100644 --- a/src/PHPCensor/Service/BuildStatusService.php +++ b/src/PHPCensor/Service/BuildStatusService.php @@ -105,7 +105,7 @@ class BuildStatusService { if (in_array($this->build->getStatus(), $this->finishedStatusIds)) { return 'Sleeping'; - } elseif ($this->build->getStatus() == Build::STATUS_NEW) { + } elseif ($this->build->getStatus() == Build::STATUS_PENDING) { return 'Pending'; } elseif ($this->build->getStatus() == Build::STATUS_RUNNING) { return 'Building'; diff --git a/src/PHPCensor/Store/Base/BuildStoreBase.php b/src/PHPCensor/Store/Base/BuildStoreBase.php index 442c79eb..849c1a65 100644 --- a/src/PHPCensor/Store/Base/BuildStoreBase.php +++ b/src/PHPCensor/Store/Base/BuildStoreBase.php @@ -85,7 +85,14 @@ class BuildStoreBase extends Store /** * Get multiple Build by Status. + * + * @param $value + * @param int $limit + * @param string $useConnection + * * @return array + * + * @throws HttpException */ public function getByStatus($value, $limit = 1000, $useConnection = 'read') { @@ -93,7 +100,6 @@ class BuildStoreBase extends Store throw new HttpException('Value passed to ' . __FUNCTION__ . ' cannot be null.'); } - $query = 'SELECT * FROM {{build}} WHERE {{status}} = :status LIMIT :limit'; $stmt = Database::getConnection($useConnection)->prepareCommon($query); $stmt->bindValue(':status', $value); diff --git a/src/PHPCensor/View/Build/header-row.phtml b/src/PHPCensor/View/Build/header-row.phtml index 417c1c72..7bb8f4d0 100644 --- a/src/PHPCensor/View/Build/header-row.phtml +++ b/src/PHPCensor/View/Build/header-row.phtml @@ -10,7 +10,7 @@

getProject()->getTitle(); ?> - getStatus() == \PHPCensor\Model\Build::STATUS_NEW): ?> + getStatus() == \PHPCensor\Model\Build::STATUS_PENDING): ?> getCreated()->format('H:i')); ?> getStatus() == \PHPCensor\Model\Build::STATUS_RUNNING): ?> getStarted()->format('H:i')); ?> diff --git a/src/PHPCensor/View/Home/ajax-timeline.phtml b/src/PHPCensor/View/Home/ajax-timeline.phtml index 4d6f9a03..55fa651e 100644 --- a/src/PHPCensor/View/Home/ajax-timeline.phtml +++ b/src/PHPCensor/View/Home/ajax-timeline.phtml @@ -5,7 +5,7 @@ getStatus()) { - case \PHPCensor\Model\Build::STATUS_NEW: + case \PHPCensor\Model\Build::STATUS_PENDING: $updated = $build->getCreated(); $label = Lang::get('pending'); $color = 'blue'; diff --git a/src/PHPCensor/View/Home/index.phtml b/src/PHPCensor/View/Home/index.phtml index fe5ad109..6562c463 100644 --- a/src/PHPCensor/View/Home/index.phtml +++ b/src/PHPCensor/View/Home/index.phtml @@ -31,7 +31,7 @@ getStatus()) { - case \PHPCensor\Model\Build::STATUS_NEW: + case \PHPCensor\Model\Build::STATUS_PENDING: $updated = $build->getCreated(); $label = Lang::get('pending'); $color = 'blue'; diff --git a/src/PHPCensor/Worker/BuildWorker.php b/src/PHPCensor/Worker/BuildWorker.php index 2302b3b2..10066d0e 100644 --- a/src/PHPCensor/Worker/BuildWorker.php +++ b/src/PHPCensor/Worker/BuildWorker.php @@ -59,8 +59,8 @@ class BuildWorker */ public function __construct($host, $queue) { - $this->host = $host; - $this->queue = $queue; + $this->host = $host; + $this->queue = $queue; $this->pheanstalk = new Pheanstalk($this->host); } diff --git a/tests/PHPCensor/Command/InstallCommandTest.php b/tests/PHPCensor/Command/InstallCommandTest.php index 5c8e2404..fa56408a 100644 --- a/tests/PHPCensor/Command/InstallCommandTest.php +++ b/tests/PHPCensor/Command/InstallCommandTest.php @@ -107,7 +107,7 @@ class InstallCommandTest extends \PHPUnit_Framework_TestCase '--admin-name' => 'admin', '--admin-pass' => 'admin-password', '--url' => 'http://php-censor.local', - '--queue-disabled' => null, + '--queue-use' => null, ]; if (!is_null($exclude)) { diff --git a/tests/PHPCensor/Model/BuildTest.php b/tests/PHPCensor/Model/BuildTest.php index 3fac5c90..f7858e02 100644 --- a/tests/PHPCensor/Model/BuildTest.php +++ b/tests/PHPCensor/Model/BuildTest.php @@ -43,7 +43,7 @@ class BuildTest extends \PHPUnit_Framework_TestCase public function testExecute_TestIsSuccessful() { $build = new Build(); - $build->setStatus(Build::STATUS_NEW); + $build->setStatus(Build::STATUS_PENDING); $this->assertFalse($build->isSuccessful()); $build->setStatus(Build::STATUS_RUNNING); diff --git a/tests/PHPCensor/Service/BuildServiceTest.php b/tests/PHPCensor/Service/BuildServiceTest.php index 46f721c4..65df07d7 100644 --- a/tests/PHPCensor/Service/BuildServiceTest.php +++ b/tests/PHPCensor/Service/BuildServiceTest.php @@ -50,7 +50,7 @@ class BuildServiceTest extends \PHPUnit_Framework_TestCase $returnValue = $this->testedService->createBuild($project); $this->assertEquals(101, $returnValue->getProjectId()); - $this->assertEquals(Build::STATUS_NEW, $returnValue->getStatus()); + $this->assertEquals(Build::STATUS_PENDING, $returnValue->getStatus()); $this->assertNull($returnValue->getStarted()); $this->assertNull($returnValue->getFinished()); $this->assertNull($returnValue->getLog()); @@ -108,7 +108,7 @@ class BuildServiceTest extends \PHPUnit_Framework_TestCase $this->assertEquals($build->getProjectId(), $returnValue->getProjectId()); $this->assertEquals($build->getCommitId(), $returnValue->getCommitId()); $this->assertNotEquals($build->getStatus(), $returnValue->getStatus()); - $this->assertEquals(Build::STATUS_NEW, $returnValue->getStatus()); + $this->assertEquals(Build::STATUS_PENDING, $returnValue->getStatus()); $this->assertNull($returnValue->getLog()); $this->assertEquals($build->getBranch(), $returnValue->getBranch()); $this->assertNotEquals($build->getCreated(), $returnValue->getCreated()); diff --git a/tests/PHPCensor/Service/BuiltStatusServiceTest.php b/tests/PHPCensor/Service/BuiltStatusServiceTest.php index 7a31c6f1..a50a74c6 100644 --- a/tests/PHPCensor/Service/BuiltStatusServiceTest.php +++ b/tests/PHPCensor/Service/BuiltStatusServiceTest.php @@ -80,7 +80,7 @@ class BuildStatusServiceTest extends \PHPUnit_Framework_TestCase 'previousBuild' => null, ], '5' => [ - 'status' => Build::STATUS_NEW, + 'status' => Build::STATUS_PENDING, 'id' => 1000, 'finishDateTime' => '2014-12-25 21:12:21', 'previousBuild' => 3,