This repository has been archived by the owner on Jan 17, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(daemon-mode): Daemonize Swoole HTTP server (#8)
See commands: ```bash $ bin/console swoole:server:start # starts swoole http server in background $ bin/console swoole:server:stop # stops swoole http server running in background $ bin/console swoole:server:reload # reloads swoole http server's workers and project classes (not infrastructure/vendor ones) ```
- Loading branch information
Showing
15 changed files
with
380 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
/vendor | ||
swoole.pid | ||
.php_cs.cache | ||
clover.xml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace K911\Swoole\Bridge\Symfony\Bundle\Command; | ||
|
||
use K911\Swoole\Server\HttpServer; | ||
use K911\Swoole\Server\HttpServerConfiguration; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Style\SymfonyStyle; | ||
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; | ||
use Throwable; | ||
|
||
final class ServerReloadCommand extends Command | ||
{ | ||
private $server; | ||
private $serverConfiguration; | ||
private $parameterBag; | ||
|
||
public function __construct( | ||
HttpServer $server, | ||
HttpServerConfiguration $serverConfiguration, | ||
ParameterBagInterface $parameterBag) | ||
{ | ||
$this->server = $server; | ||
$this->serverConfiguration = $serverConfiguration; | ||
$this->parameterBag = $parameterBag; | ||
|
||
parent::__construct(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function configure(): void | ||
{ | ||
$this->setName('swoole:server:reload') | ||
->setDescription("Reloads a local Swoole HTTP server's workers running in background. It will reload only classes not loaded before server initialization.") | ||
->addOption('pid_file', null, InputOption::VALUE_REQUIRED, 'Pid file', $this->parameterBag->get('kernel.project_dir').'/var/swoole.pid'); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* @throws \Assert\AssertionFailedException | ||
*/ | ||
protected function execute(InputInterface $input, OutputInterface $output): void | ||
{ | ||
$io = new SymfonyStyle($input, $output); | ||
|
||
$this->serverConfiguration->daemonize($input->getOption('pid_file')); | ||
|
||
try { | ||
$this->server->reload(); | ||
} catch (Throwable $ex) { | ||
$io->error($ex->getMessage()); | ||
exit(1); | ||
} | ||
|
||
$io->success('Swoole HTTP Server\'s workers reloaded successfully'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace K911\Swoole\Bridge\Symfony\Bundle\Command; | ||
|
||
use K911\Swoole\Server\HttpServer; | ||
use K911\Swoole\Server\HttpServerConfiguration; | ||
use RuntimeException; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Output\ConsoleOutput; | ||
use Symfony\Component\Console\Output\StreamOutput; | ||
use Symfony\Component\Console\Style\OutputStyle; | ||
use Symfony\Component\Console\Style\SymfonyStyle; | ||
use function K911\Swoole\get_object_property; | ||
|
||
final class ServerStartCommand extends AbstractServerStartCommand | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function configure(): void | ||
{ | ||
$this->setName('swoole:server:start') | ||
->setDescription('Runs a local Swoole HTTP server in background.') | ||
->addOption('pid_file', null, InputOption::VALUE_REQUIRED, 'Pid file', $this->parameterBag->get('kernel.project_dir').'/var/swoole.pid'); | ||
|
||
parent::configure(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function prepareServerConfiguration(HttpServerConfiguration $serverConfiguration, InputInterface $input): void | ||
{ | ||
/** @var string|null $pidFile */ | ||
$pidFile = $input->getOption('pid_file'); | ||
$serverConfiguration->daemonize($pidFile); | ||
|
||
parent::prepareServerConfiguration($serverConfiguration, $input); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function startServer(HttpServerConfiguration $serverConfiguration, HttpServer $server, SymfonyStyle $io): void | ||
{ | ||
if (!$serverConfiguration->existsPidFile() && !\touch($serverConfiguration->getPidFile())) { | ||
throw new RuntimeException(\sprintf('Could not create pid file "%s".', $serverConfiguration->getPid())); | ||
} | ||
|
||
// Output stream `php://stdout` must be closed | ||
$this->forceCloseOutputStream($io); | ||
|
||
$server->start(); | ||
} | ||
|
||
private function forceCloseOutputStream(SymfonyStyle $io): void | ||
{ | ||
/** @var ConsoleOutput $consoleOutput */ | ||
$consoleOutput = &get_object_property($io, 'output', OutputStyle::class); | ||
|
||
/** @var resource $stream */ | ||
$stream = &get_object_property($consoleOutput, 'stream', StreamOutput::class); | ||
|
||
\fclose($stream); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace K911\Swoole\Bridge\Symfony\Bundle\Command; | ||
|
||
use K911\Swoole\Server\HttpServer; | ||
use K911\Swoole\Server\HttpServerConfiguration; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Style\SymfonyStyle; | ||
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; | ||
use Throwable; | ||
|
||
final class ServerStopCommand extends Command | ||
{ | ||
private $server; | ||
private $serverConfiguration; | ||
private $parameterBag; | ||
|
||
public function __construct( | ||
HttpServer $server, | ||
HttpServerConfiguration $serverConfiguration, | ||
ParameterBagInterface $parameterBag) | ||
{ | ||
$this->server = $server; | ||
$this->serverConfiguration = $serverConfiguration; | ||
$this->parameterBag = $parameterBag; | ||
|
||
parent::__construct(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function configure(): void | ||
{ | ||
$this->setName('swoole:server:stop') | ||
->setDescription('Stops a local Swoole HTTP server running in background') | ||
->addOption('pid_file', null, InputOption::VALUE_REQUIRED, 'Pid file', $this->parameterBag->get('kernel.project_dir').'/var/swoole.pid'); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* @throws \Assert\AssertionFailedException | ||
*/ | ||
protected function execute(InputInterface $input, OutputInterface $output): void | ||
{ | ||
$io = new SymfonyStyle($input, $output); | ||
|
||
$this->serverConfiguration->daemonize($input->getOption('pid_file')); | ||
|
||
try { | ||
$this->server->shutdown(); | ||
} catch (Throwable $ex) { | ||
$io->error($ex->getMessage()); | ||
exit(1); | ||
} | ||
|
||
$io->success('Swoole server shutdown successfully'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.