diff --git a/PHPCI/Command/DaemonCommand.php b/PHPCI/Command/DaemonCommand.php index 78513ced..94ecb464 100644 --- a/PHPCI/Command/DaemonCommand.php +++ b/PHPCI/Command/DaemonCommand.php @@ -30,8 +30,13 @@ class DaemonCommand extends Command protected function configure() { $this - ->setName('phpci:start-daemon') - ->setDescription('Starts the daemon to run commands.'); + ->setName('phpci:daemon') + ->setDescription('Initiates the daemon to run commands.') + ->addArgument( + 'state', + InputArgument::REQUIRED, + 'start|stop|status' + ); } /** @@ -39,30 +44,73 @@ class DaemonCommand extends Command */ protected function execute(InputInterface $input, OutputInterface $output) { + $state = $input->getArgument('state'); - $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); + switch ($state) { + case 'start': + $this->startDaemon(); + break; + case 'stop': + $this->stopDaemon(); + break; + case 'status': + $this->statusDaemon(); + break; + default: + echo "Not a valid choice, please use start stop or status"; + break; } } + protected function startDaemon() + { + + if ( file_exists(PHPCI_DIR.'/daemon/daemon.pid') ) { + echo "Already started\n"; + return "alreadystarted"; + } + + $logfile = PHPCI_DIR."/daemon/daemon.log"; + $cmd = "nohup %s/daemonise phpci:daemonise > %s 2>&1 &"; + $command = sprintf($cmd, PHPCI_DIR, $logfile); + exec($command); + } + + protected function stopDaemon() + { + + if ( !file_exists(PHPCI_DIR.'/daemon/daemon.pid') ) { + echo "Not started\n"; + return "notstarted"; + } + + $cmd = "kill $(cat %s/daemon/daemon.pid)"; + $command = sprintf($cmd, PHPCI_DIR); + exec($command); + unlink(PHPCI_DIR.'/daemon/daemon.pid'); + } + + protected function statusDaemon() + { + + if ( !file_exists(PHPCI_DIR.'/daemon/daemon.pid') ) { + echo "Not running\n"; + return "notrunning"; + } + + $pid = trim(file_get_contents(PHPCI_DIR.'/daemon/daemon.pid')); + $pidcheck = sprintf("/proc/%s", $pid); + if ( is_dir($pidcheck) ) { + echo "Running\n"; + return "running"; + } + + unlink(PHPCI_DIR.'/daemon/daemon.pid'); + echo "Not running\n"; + return "notrunning"; + } + /** * Called when log entries are made in Builder / the plugins. * @see \PHPCI\Builder::log() diff --git a/PHPCI/Command/DaemoniseCommand.php b/PHPCI/Command/DaemoniseCommand.php new file mode 100644 index 00000000..f46e6948 --- /dev/null +++ b/PHPCI/Command/DaemoniseCommand.php @@ -0,0 +1,76 @@ + /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 DaemoniseCommand extends Command +{ + protected function configure() + { + $this + ->setName('phpci:daemonise') + ->setDescription('Starts the daemon to run commands.'); + } + + /** + * Loops through running. + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + $cmd = "echo %s > '%s/daemon/daemon.pid'"; + $command = sprintf($cmd, getmypid(), PHPCI_DIR); + exec($command); + + $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 '.'.(0 === $buildCount?'':'build'); + 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/console b/console index 62d7258d..f571c454 100755 --- a/console +++ b/console @@ -10,6 +10,7 @@ define('PHPCI_BIN_DIR', dirname(__FILE__) . '/vendor/bin/'); define('PHPCI_DIR', dirname(__FILE__) . '/'); +define('ENABLE_SHELL_PLUGIN', false); // If this is the first time ./console has been run, we probably don't have Composer or any of our dependencies yet. // So we need to install and run Composer. diff --git a/daemon/.gitignore b/daemon/.gitignore new file mode 100644 index 00000000..c96a04f0 --- /dev/null +++ b/daemon/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore \ No newline at end of file diff --git a/daemonise b/daemonise new file mode 100755 index 00000000..63ac409a --- /dev/null +++ b/daemonise @@ -0,0 +1,22 @@ +#!/usr/bin/env php +add(new DaemoniseCommand); +$application->run();