MAjority of daemon work done, can start stop and status the daemon

This commit is contained in:
Gabriel Baker 2013-06-09 20:11:22 +01:00
parent 910e09eb52
commit a963a3f284
5 changed files with 170 additions and 21 deletions

View file

@ -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()

View file

@ -0,0 +1,76 @@
<?php
/**
* PHPCI - Continuous Integration for PHP
* nohup PHPCI_DIR/console phpci:start-daemon > /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 <gabriel.baker@autonomicpilot.co.uk>
* @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);
}
}

View file

@ -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.

2
daemon/.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
*
!.gitignore

22
daemonise Executable file
View file

@ -0,0 +1,22 @@
#!/usr/bin/env php
<?php
/**
* PHPCI - Continuous Integration for PHP
*
* @copyright Copyright 2013, Block 8 Limited.
* @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md
* @link http://www.phptesting.org/
*/
define('PHPCI_BIN_DIR', dirname(__FILE__) . '/vendor/bin/');
define('PHPCI_DIR', dirname(__FILE__) . '/');
define('ENABLE_SHELL_PLUGIN', false);
require('bootstrap.php');
use PHPCI\Command\DaemoniseCommand;
use Symfony\Component\Console\Application;
$application = new Application();
$application->add(new DaemoniseCommand);
$application->run();