From 910e09eb52e777d0bb8d08cdad2ac7bb67817aca Mon Sep 17 00:00:00 2001 From: Gabriel Baker Date: Sun, 9 Jun 2013 17:42:50 +0100 Subject: [PATCH] daemon work --- PHPCI/Command/DaemonCommand.php | 74 +++++++++++++++++++++++++++++++++ PHPCI/Command/RunCommand.php | 9 +++- console | 2 + 3 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 PHPCI/Command/DaemonCommand.php diff --git a/PHPCI/Command/DaemonCommand.php b/PHPCI/Command/DaemonCommand.php new file mode 100644 index 00000000..78513ced --- /dev/null +++ b/PHPCI/Command/DaemonCommand.php @@ -0,0 +1,74 @@ + /dev/null 2>&1 & +* +* @copyright Copyright 2013, Block 8 Limited. +* @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md +* @link http://www.phptesting.org/ +*/ + +namespace PHPCI\Command; + +use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; +use b8\Store\Factory; +use PHPCI\Builder; +use PHPCI\BuildFactory; + +/** +* Daemon that loops and call the run-command. +* @author Gabriel Baker +* @package PHPCI +* @subpackage Console +*/ +class DaemonCommand extends Command +{ + protected function configure() + { + $this + ->setName('phpci:start-daemon') + ->setDescription('Starts the daemon to run commands.'); + } + + /** + * Loops through running. + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + + $this->run = true; + $this->sleep = 0; + $runner = new RunCommand; + + while ($this->run) { + + try { + $buildCount = $runner->execute($input, $output); + } catch (\Exception $e) { + var_dump($e); + } + + if (0 == $buildCount && $this->sleep < 15) { + $this->sleep++; + } else if (1 < $this->sleep) { + $this->sleep--; + } + echo $buildCount . ' ' . $this->sleep . ','; + sleep($this->sleep); + } + + } + + /** + * Called when log entries are made in Builder / the plugins. + * @see \PHPCI\Builder::log() + */ + public function logCallback($log) + { + $this->output->writeln($log); + } +} diff --git a/PHPCI/Command/RunCommand.php b/PHPCI/Command/RunCommand.php index b28d9832..abd30c6c 100644 --- a/PHPCI/Command/RunCommand.php +++ b/PHPCI/Command/RunCommand.php @@ -42,18 +42,23 @@ class RunCommand extends Command $store = Factory::getStore('Build'); $result = $store->getByStatus(0); + $builds = 0; foreach ($result['items'] as $build) { + $builds++; + $build = BuildFactory::getBuild($build); - + if ($input->getOption('verbose')) { $builder = new Builder($build, array($this, 'logCallback')); } else { $builder = new Builder($build); } - + $builder->execute(); } + + return $builds; } /** diff --git a/console b/console index 4a0c8120..62d7258d 100755 --- a/console +++ b/console @@ -28,10 +28,12 @@ require('bootstrap.php'); use PHPCI\Command\RunCommand; use PHPCI\Command\GenerateCommand; use PHPCI\Command\InstallCommand; +use PHPCI\Command\DaemonCommand; use Symfony\Component\Console\Application; $application = new Application(); $application->add(new RunCommand); $application->add(new InstallCommand); $application->add(new GenerateCommand); +$application->add(new DaemonCommand); $application->run();