Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Implement daemon mode#351

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
andig wants to merge5 commits intophp-pm:master
base:master
Choose a base branch
Loading
fromandig:daemon
Open
Show file tree
Hide file tree
Changes from1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
NextNext commit
Implement daemon mode
  • Loading branch information
@andig
andig committedSep 11, 2018
commit7f345164e23b156b1c73b79733dbd836ce6fad80
26 changes: 16 additions & 10 deletions.travis.yml
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -15,6 +15,7 @@ install:
script:
## Unit test
- ./vendor/bin/phpunit

## Integration test
- mkdir web && echo "Hello" > web/index.html
- bin/ppm start --workers=1 --bridge=StaticBridge --static-directory=web --max-requests=1 -v &
Expand All@@ -25,6 +26,14 @@ script:
- curl -v -f "http://127.0.0.1:8080"
- bin/ppm status
- bin/ppm stop

## Daemon mode
- bin/ppm start --workers=1 --bridge=StaticBridge --static-directory=web --max-requests=1 -v -d
- curl -v -f "http://127.0.0.1:8080"
- bin/ppm status
- bin/ppm stop
- cat ppm.log

## Test memory limit
- bin/ppm start --workers=1 --bridge=PHPPM\\Tests\\TestBridge --static-directory=web --memory-limit=5 -v > /tmp/ppmout &
- sleep 5
Expand All@@ -40,24 +49,21 @@ script:
- bash -c 'grep -q "because it reached memory limit of 5" /tmp/ppmout && exit 0'
- bin/ppm status
- bin/ppm stop
## Trigger 502 error by triggering an exit() in the worker
- bin/ppm start --workers=1 --bridge=PHPPM\\Tests\\TestBridge --static-directory=web --max-requests=1 --max-execution-time=15 -v > /tmp/ppmout &

# Trigger 502 error by triggering an exit() in the worker
- bin/ppm start --workers=1 --bridge=PHPPM\\Tests\\TestBridge --static-directory=web --max-requests=1 --max-execution-time=15 -v &
- sleep 5
- bash -c 'if [[ $(curl --write-out %{http_code} --silent --output /dev/null "http://127.0.0.1:8080/test?exit_prematurely=1") == "502" ]]; then exit 0; else exit 1; fi'
- bin/ppm status
## Trigger 503 error by tying up the worker and making a second request

# Trigger 503 error by tying up the worker and making a second request
- curl -f --silent "http://127.0.0.1:8080/test?sleep=10000" &
- bash -c 'if [[ $(curl --write-out %{http_code} --silent --output /dev/null "http://127.0.0.1:8080") == "503" ]]; then exit 0; else exit 1; fi'
- bin/ppm status
## Trigger 504 error by making a sleep request and waiting until the timeout

# Trigger 504 error by making a sleep request and waiting until the timeout
- bash -c 'if [[ $(curl --write-out %{http_code} --silent --output /dev/null "http://127.0.0.1:8080/test?sleep=10000") == "504" ]]; then exit 0; else exit 1; fi'
- bin/ppm status
## Trigger 502 error by making the bridge throw an exception
- bash -c 'if [[ $(curl --write-out %{http_code} --silent --output /dev/null "http://127.0.0.1:8080/test?exception=1") == "502" ]]; then exit 0; else exit 1; fi'
- bash -c 'grep -q "An exception was thrown by the bridge. Forcing restart of the worker. The exception was" /tmp/ppmout && exit 0'
- bash -c 'grep -q "This is a very bad exception" /tmp/ppmout && exit 0'
- bin/ppm status
- bin/ppm stop

matrix:
fast_finish: true
Expand Down
2 changes: 2 additions & 0 deletionssrc/Commands/ConfigTrait.php
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -22,6 +22,8 @@ protected function configurePPMOptions(Command $command)
->addOption('workers', null, InputOption::VALUE_REQUIRED, 'Worker count. Default is 8. Should be minimum equal to the number of CPU cores.', 8)
->addOption('app-env', null, InputOption::VALUE_REQUIRED, 'The environment that your application will use to bootstrap (if any)', 'dev')
->addOption('debug', null, InputOption::VALUE_REQUIRED, 'Enable/Disable debugging so that your application is more verbose, enables also hot-code reloading. 1|0', 0)
->addOption('daemonize', 'd', InputOption::VALUE_NONE, 'Run in background')
->addOption('logfile', null, InputOption::VALUE_REQUIRED, 'Logfile (daemon mode only)', './ppm.log')
->addOption('logging', null, InputOption::VALUE_REQUIRED, 'Enable/Disable http logging to stdout. 1|0', 1)
->addOption('static-directory', null, InputOption::VALUE_REQUIRED, 'Static files root directory, if not provided static files will not be served', '')
->addOption('max-requests', null, InputOption::VALUE_REQUIRED, 'Max requests per worker until it will be restarted', 1000)
Expand Down
67 changes: 67 additions & 0 deletionssrc/Commands/DaemonTrait.php
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
<?php

namespace PHPPM\Commands;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Output\StreamOutput;

trait DaemonTrait
{
/**
* Split into a new process. Returns true when it's the child process and false
* for the parent process.
*
* Borrowed from https://github.com/phlib/console-process/blob/master/src/Command/DaemonCommand.php
*
* @return bool
*/
private function daemonize()
{
// prevent permission issues
umask(0);
$pid = pcntl_fork();
if ($pid == -1) {
/* fork failed */
throw new \RuntimeException('Failed to fork the daemon.');
} elseif ($pid) {
/* close the parent */
return false;
}
// make ourselves the session leader
if (posix_setsid() == -1) {
throw new \RuntimeException('Failed to become a daemon.');
}
return true;
}

/**
* Borrowed from https://github.com/phlib/console-process/blob/master/src/Command/DaemonCommand.php
* @param InputInterface $input
* @return InputInterface
*/
private function recreateInput(InputInterface $input)
{
return clone $input;
}

/**
* Borrowed from https://github.com/phlib/console-process/blob/master/src/Command/DaemonCommand.php
* @param OutputInterface $output
* @return OutputInterface
*/
private function recreateOutput(OutputInterface $output, $logfile)
{
$verbosityLevel = $output->getVerbosity();

if ($logfile) {
$newInstance = new StreamOutput(fopen($logfile, 'a'));
} else {
$newInstance = new NullOutput();
}

$newInstance->setVerbosity($verbosityLevel);
return $newInstance;
}
}
16 changes: 16 additions & 0 deletionssrc/Commands/StartCommand.php
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -6,11 +6,13 @@
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class StartCommand extends Command
{
use ConfigTrait;
use DaemonTrait;

/**
* {@inheritdoc}
Expand All@@ -30,6 +32,20 @@ protected function configure()

protected function execute(InputInterface $input, OutputInterface $output)
{
if ($input->getOption('daemonize')) {
$isChild = $this->daemonize();
if (!$isChild) {
if ($output->isVerbose()) {
$output->writeln('Daemonized');
}
return null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Asthis project supports Symfony 5, the return value of a commandsexecute method must be an integer. While it isnot part of the methods signature in the abstractCommand class, it is stillvalidated and an exception will be thrown for any non-integer return value. So I suggest, you change this toreturn 0; along with the other return statement below.

}

// children shouldn't hold onto parents input/output
$input = $this->recreateInput($input);
$output = $this->recreateOutput($output, $input->getOption('logfile'));
}

$config = $this->initializeConfig($input, $output);

$handler = new ProcessManager($output, $config['port'], $config['host'], $config['workers']);
Expand Down
16 changes: 14 additions & 2 deletionssrc/ProcessManager.php
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -529,10 +529,22 @@ public function handleSigchld()
$pid = pcntl_waitpid(-1, $status, WNOHANG);
}

/**
* Write pid file
*/
public function writePid()
{
$pid = getmypid();
file_put_contents($this->pidfile, $pid);

if (file_exists($this->pidfile)) {
throw new \InvalidArgumentException(sprintf("PID file '%s' already exists", $this->pidfile));
}
if (!is_writable(dirname($this->pidfile))) {
throw new \InvalidArgumentException(sprintf("PID file '%s' not writable", $this->pidfile));
}
if (false === file_put_contents($this->pidfile, $pid)) {
throw new \Exception(sprintf("Failed writing PID file '%s'", $this->pidfile));
}
}

/**
Expand DownExpand Up@@ -1191,7 +1203,7 @@ protected function newSlaveInstance($port)
require_once file_exists($dir . '/vendor/autoload.php')
? $dir . '/vendor/autoload.php'
: $dir . '/../../autoload.php';

if (!pcntl_installed()) {
error_log(
sprintf(
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp