MAjority of daemon work done, can start stop and status the daemon
This commit is contained in:
parent
910e09eb52
commit
a963a3f284
|
@ -30,8 +30,13 @@ class DaemonCommand extends Command
|
||||||
protected function configure()
|
protected function configure()
|
||||||
{
|
{
|
||||||
$this
|
$this
|
||||||
->setName('phpci:start-daemon')
|
->setName('phpci:daemon')
|
||||||
->setDescription('Starts the daemon to run commands.');
|
->setDescription('Initiates the daemon to run commands.')
|
||||||
|
->addArgument(
|
||||||
|
'state',
|
||||||
|
InputArgument::REQUIRED,
|
||||||
|
'start|stop|status'
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -39,28 +44,71 @@ class DaemonCommand extends Command
|
||||||
*/
|
*/
|
||||||
protected function execute(InputInterface $input, OutputInterface $output)
|
protected function execute(InputInterface $input, OutputInterface $output)
|
||||||
{
|
{
|
||||||
|
$state = $input->getArgument('state');
|
||||||
|
|
||||||
$this->run = true;
|
switch ($state) {
|
||||||
$this->sleep = 0;
|
case 'start':
|
||||||
$runner = new RunCommand;
|
$this->startDaemon();
|
||||||
|
break;
|
||||||
while ($this->run) {
|
case 'stop':
|
||||||
|
$this->stopDaemon();
|
||||||
try {
|
break;
|
||||||
$buildCount = $runner->execute($input, $output);
|
case 'status':
|
||||||
} catch (\Exception $e) {
|
$this->statusDaemon();
|
||||||
var_dump($e);
|
break;
|
||||||
|
default:
|
||||||
|
echo "Not a valid choice, please use start stop or status";
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (0 == $buildCount && $this->sleep < 15) {
|
|
||||||
$this->sleep++;
|
|
||||||
} else if (1 < $this->sleep) {
|
|
||||||
$this->sleep--;
|
|
||||||
}
|
|
||||||
echo $buildCount . ' ' . $this->sleep . ',';
|
|
||||||
sleep($this->sleep);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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";
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
76
PHPCI/Command/DaemoniseCommand.php
Normal file
76
PHPCI/Command/DaemoniseCommand.php
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
1
console
1
console
|
@ -10,6 +10,7 @@
|
||||||
|
|
||||||
define('PHPCI_BIN_DIR', dirname(__FILE__) . '/vendor/bin/');
|
define('PHPCI_BIN_DIR', dirname(__FILE__) . '/vendor/bin/');
|
||||||
define('PHPCI_DIR', dirname(__FILE__) . '/');
|
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.
|
// 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.
|
// So we need to install and run Composer.
|
||||||
|
|
2
daemon/.gitignore
vendored
Normal file
2
daemon/.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
*
|
||||||
|
!.gitignore
|
22
daemonise
Executable file
22
daemonise
Executable 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();
|
Loading…
Reference in a new issue