Deleted daemon mode (You should use worker mode instead)
This commit is contained in:
parent
5e0dadf5e1
commit
d77f0e8474
12 changed files with 23 additions and 392 deletions
|
|
@ -1,202 +0,0 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* PHPCI - Continuous Integration for PHP
|
||||
*
|
||||
* @copyright Copyright 2014, Block 8 Limited.
|
||||
* @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md
|
||||
* @link https://www.phptesting.org/
|
||||
*/
|
||||
|
||||
namespace PHPCensor\Command;
|
||||
|
||||
use Monolog\Logger;
|
||||
use PHPCensor\ProcessControl\Factory;
|
||||
use PHPCensor\ProcessControl\ProcessControlInterface;
|
||||
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;
|
||||
|
||||
/**
|
||||
* Daemon that loops and call the run-command.
|
||||
* @author Gabriel Baker <gabriel.baker@autonomicpilot.co.uk>
|
||||
* @package PHPCI
|
||||
* @subpackage Console
|
||||
*/
|
||||
class DaemonCommand extends Command
|
||||
{
|
||||
|
||||
/**
|
||||
* @var Logger
|
||||
*/
|
||||
protected $logger;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $pidFilePath;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $logFilePath;
|
||||
|
||||
/**
|
||||
* @var ProcessControlInterface
|
||||
*/
|
||||
protected $processControl;
|
||||
|
||||
public function __construct(Logger $logger, ProcessControlInterface $processControl = null, $name = null)
|
||||
{
|
||||
parent::__construct($name);
|
||||
$this->logger = $logger;
|
||||
$this->processControl = $processControl ?: Factory::getInstance();
|
||||
}
|
||||
|
||||
protected function configure()
|
||||
{
|
||||
$this
|
||||
->setName('php-censor:daemon')
|
||||
->setDescription('Initiates the daemon to run commands.')
|
||||
->addArgument(
|
||||
'state', InputArgument::REQUIRED, 'start|stop|status'
|
||||
)->addOption(
|
||||
'pid-file',
|
||||
'p',
|
||||
InputOption::VALUE_REQUIRED,
|
||||
'Path of the PID file',
|
||||
(ROOT_DIR . 'runtime' . DIRECTORY_SEPARATOR . 'daemon' . DIRECTORY_SEPARATOR . 'daemon.pid')
|
||||
)->addOption(
|
||||
'log-file',
|
||||
'l',
|
||||
InputOption::VALUE_REQUIRED,
|
||||
'Path of the log file',
|
||||
(ROOT_DIR . 'runtime' . DIRECTORY_SEPARATOR . 'daemon' . DIRECTORY_SEPARATOR . 'daemon.log')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Loops through running.
|
||||
*/
|
||||
protected function execute(InputInterface $input, OutputInterface $output)
|
||||
{
|
||||
$this->pidFilePath = $input->getOption('pid-file');
|
||||
$this->logFilePath = $input->getOption('log-file');
|
||||
|
||||
$state = $input->getArgument('state');
|
||||
|
||||
switch ($state) {
|
||||
case 'start':
|
||||
$this->startDaemon();
|
||||
break;
|
||||
case 'stop':
|
||||
$this->stopDaemon();
|
||||
break;
|
||||
case 'status':
|
||||
$this->statusDaemon($output);
|
||||
break;
|
||||
default:
|
||||
$this->output->writeln("<error>Not a valid choice, please use start, stop or status</error>");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
protected function startDaemon()
|
||||
{
|
||||
$pid = $this->getRunningPid();
|
||||
if ($pid) {
|
||||
$this->logger->notice("Daemon already started", ['pid' => $pid]);
|
||||
return "alreadystarted";
|
||||
}
|
||||
|
||||
$this->logger->info("Trying to start the daemon");
|
||||
|
||||
$cmd = "nohup %sdaemonise php-censor:daemonise > %s 2>&1 &";
|
||||
$command = sprintf($cmd, BIN_DIR, $this->logFilePath);
|
||||
$output = $exitCode = null;
|
||||
exec($command, $output, $exitCode);
|
||||
|
||||
if ($exitCode !== 0) {
|
||||
$this->logger->error(sprintf("daemonise exited with status %d", $exitCode));
|
||||
return "notstarted";
|
||||
}
|
||||
|
||||
for ($i = 0; !($pid = $this->getRunningPid()) && $i < 5; $i++) {
|
||||
sleep(1);
|
||||
}
|
||||
|
||||
if (!$pid) {
|
||||
$this->logger->error("Could not start the daemon");
|
||||
return "notstarted";
|
||||
}
|
||||
|
||||
$this->logger->notice("Daemon started", ['pid' => $pid]);
|
||||
return "started";
|
||||
}
|
||||
|
||||
protected function stopDaemon()
|
||||
{
|
||||
$pid = $this->getRunningPid();
|
||||
if (!$pid) {
|
||||
$this->logger->notice("Cannot stop the daemon as it is not started");
|
||||
return "notstarted";
|
||||
}
|
||||
|
||||
$this->logger->info("Trying to terminate the daemon", ['pid' => $pid]);
|
||||
$this->processControl->kill($pid);
|
||||
|
||||
for ($i = 0; ($pid = $this->getRunningPid()) && $i < 5; $i++) {
|
||||
sleep(1);
|
||||
}
|
||||
|
||||
if ($pid) {
|
||||
$this->logger->warning("The daemon is resiting, trying to kill it", ['pid' => $pid]);
|
||||
$this->processControl->kill($pid, true);
|
||||
|
||||
for ($i = 0; ($pid = $this->getRunningPid()) && $i < 5; $i++) {
|
||||
sleep(1);
|
||||
}
|
||||
}
|
||||
|
||||
if (!$pid) {
|
||||
$this->logger->notice("Daemon stopped");
|
||||
return "stopped";
|
||||
}
|
||||
|
||||
$this->logger->error("Could not stop the daemon");
|
||||
}
|
||||
|
||||
protected function statusDaemon(OutputInterface $output)
|
||||
{
|
||||
$pid = $this->getRunningPid();
|
||||
if ($pid) {
|
||||
$output->writeln(sprintf('The daemon is running, PID: %d', $pid));
|
||||
return "running";
|
||||
}
|
||||
|
||||
$output->writeln('The daemon is not running');
|
||||
return "notrunning";
|
||||
}
|
||||
|
||||
/** Check if there is a running daemon
|
||||
*
|
||||
* @return int|null
|
||||
*/
|
||||
protected function getRunningPid()
|
||||
{
|
||||
if (!file_exists($this->pidFilePath)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$pid = intval(trim(file_get_contents($this->pidFilePath)));
|
||||
|
||||
if($this->processControl->isRunning($pid)) {
|
||||
return $pid;
|
||||
}
|
||||
|
||||
// Not found, remove the stale PID file
|
||||
unlink($this->pidFilePath);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,111 +0,0 @@
|
|||
<?php
|
||||
/**
|
||||
* PHPCI - Continuous Integration for PHP
|
||||
*
|
||||
* @copyright Copyright 2014, Block 8 Limited.
|
||||
* @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md
|
||||
* @link https://www.phptesting.org/
|
||||
*/
|
||||
|
||||
namespace PHPCensor\Command;
|
||||
|
||||
use Monolog\Logger;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\ArgvInput;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
/**
|
||||
* Daemon that loops and call the run-command.
|
||||
* @author Gabriel Baker <gabriel.baker@autonomicpilot.co.uk>
|
||||
* @package PHPCI
|
||||
* @subpackage Console
|
||||
*/
|
||||
class DaemoniseCommand extends Command
|
||||
{
|
||||
/**
|
||||
* @var Logger
|
||||
*/
|
||||
protected $logger;
|
||||
|
||||
/**
|
||||
* @var OutputInterface
|
||||
*/
|
||||
protected $output;
|
||||
|
||||
/**
|
||||
* @var boolean
|
||||
*/
|
||||
protected $run;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected $sleep;
|
||||
|
||||
/**
|
||||
* @param \Monolog\Logger $logger
|
||||
* @param string $name
|
||||
*/
|
||||
public function __construct(Logger $logger, $name = null)
|
||||
{
|
||||
parent::__construct($name);
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
protected function configure()
|
||||
{
|
||||
$this
|
||||
->setName('php-censor:daemonise')
|
||||
->setDescription('Starts the daemon to run commands.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Loops through running.
|
||||
*/
|
||||
protected function execute(InputInterface $input, OutputInterface $output)
|
||||
{
|
||||
$cmd = "echo %s > '%sdaemon/daemon.pid'";
|
||||
$command = sprintf($cmd, getmypid(), RUNTIME_DIR);
|
||||
exec($command);
|
||||
|
||||
$this->output = $output;
|
||||
$this->run = true;
|
||||
$this->sleep = 0;
|
||||
$runner = new RunCommand($this->logger);
|
||||
$runner->setMaxBuilds(1);
|
||||
$runner->setDaemon(true);
|
||||
|
||||
$emptyInput = new ArgvInput([]);
|
||||
|
||||
while ($this->run) {
|
||||
|
||||
$buildCount = 0;
|
||||
|
||||
try {
|
||||
$buildCount = $runner->run($emptyInput, $output);
|
||||
} catch (\Exception $e) {
|
||||
$output->writeln('<error>Exception: ' . $e->getMessage() . '</error>');
|
||||
$output->writeln('<error>Line: ' . $e->getLine() . ' - File: ' . $e->getFile() . '</error>');
|
||||
}
|
||||
|
||||
if (0 == $buildCount && $this->sleep < 15) {
|
||||
$this->sleep++;
|
||||
} elseif (1 < $this->sleep) {
|
||||
$this->sleep--;
|
||||
}
|
||||
echo '.'.(0 === $buildCount?'':'build');
|
||||
sleep($this->sleep);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when log entries are made in Builder / the plugins.
|
||||
*
|
||||
* @see \PHPCensor\Builder::log()
|
||||
*/
|
||||
public function logCallback($log)
|
||||
{
|
||||
$this->output->writeln($log);
|
||||
}
|
||||
}
|
||||
|
|
@ -69,7 +69,6 @@ class RebuildCommand extends Command
|
|||
{
|
||||
$runner = new RunCommand($this->logger);
|
||||
$runner->setMaxBuilds(1);
|
||||
$runner->setDaemon(false);
|
||||
|
||||
/** @var \PHPCensor\Store\BuildStore $store */
|
||||
$store = Factory::getStore('Build');
|
||||
|
|
|
|||
|
|
@ -24,11 +24,12 @@ use PHPCensor\BuildFactory;
|
|||
use PHPCensor\Model\Build;
|
||||
|
||||
/**
|
||||
* Run console command - Runs any pending builds.
|
||||
* @author Dan Cryer <dan@block8.co.uk>
|
||||
* @package PHPCI
|
||||
* @subpackage Console
|
||||
*/
|
||||
* Run console command - Runs any pending builds.
|
||||
*
|
||||
* @author Dan Cryer <dan@block8.co.uk>
|
||||
* @package PHPCI
|
||||
* @subpackage Console
|
||||
*/
|
||||
class RunCommand extends Command
|
||||
{
|
||||
/**
|
||||
|
|
@ -46,11 +47,6 @@ class RunCommand extends Command
|
|||
*/
|
||||
protected $maxBuilds = 100;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected $isFromDaemon = false;
|
||||
|
||||
/**
|
||||
* @param \Monolog\Logger $logger
|
||||
* @param string $name
|
||||
|
|
@ -105,7 +101,7 @@ class RunCommand extends Command
|
|||
$build = BuildFactory::getBuild($build);
|
||||
|
||||
// Skip build (for now) if there's already a build running in that project:
|
||||
if (!$this->isFromDaemon && in_array($build->getProjectId(), $running)) {
|
||||
if (in_array($build->getProjectId(), $running)) {
|
||||
$this->logger->addInfo(Lang::get('skipping_build', $build->getId()));
|
||||
$result['items'][] = $build;
|
||||
|
||||
|
|
@ -147,11 +143,6 @@ class RunCommand extends Command
|
|||
$this->maxBuilds = (int)$numBuilds;
|
||||
}
|
||||
|
||||
public function setDaemon($fromDaemon)
|
||||
{
|
||||
$this->isFromDaemon = (bool)$fromDaemon;
|
||||
}
|
||||
|
||||
protected function validateRunningBuilds()
|
||||
{
|
||||
/** @var \PHPCensor\Store\BuildStore $store */
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue