Skip to content
This repository has been archived by the owner on Jan 17, 2022. It is now read-only.

Commit

Permalink
fix(command): Graceful shutdown
Browse files Browse the repository at this point in the history
Command processes `swoole:server:run` and `swoole:server:profile` killed with SIGINT (CONTROL-C),
previously were killed not gracefully (event console.terminate) was not emitted.
  • Loading branch information
k911 committed Oct 14, 2018
1 parent 75b30e0 commit 7e6c9a4
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,10 @@ protected function prepareConfigurationRowsToPrint(HttpServerConfiguration $serv
*/
protected function startServer(HttpServerConfiguration $serverConfiguration, HttpServer $server, SymfonyStyle $io): void
{
$io->comment('Quit the server with CONTROL-C.');

if ($server->start()) {
$io->newLine();
$io->success('Swoole HTTP Server has been successfully shutdown.');
} else {
$io->error('Failure during starting Swoole HTTP Server.');
Expand Down
36 changes: 29 additions & 7 deletions src/Bridge/Symfony/Bundle/Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ services:

'K911\Swoole\Server\RequestHandler\LimitedRequestHandler':

'K911\Swoole\Server\LifecycleHandler\SigIntHandler':

'K911\Swoole\Server\Runtime\BootableInterface':
class: 'K911\Swoole\Server\Runtime\BootManager'
arguments: [!tagged 'swoole_bundle.bootable_service']
Expand All @@ -46,11 +48,36 @@ services:
class: K911\Swoole\Server\Configurator\WithRequestHandler
autoconfigure: false

'swoole_bundle.server.http_server.configurator.with_sigint_and_request_handler':
class: K911\Swoole\Server\Configurator\WithServerStartHandler
autoconfigure: false
arguments:
$decorated: '@swoole_bundle.server.http_server.configurator.with_request_handler'
$handler: '@K911\Swoole\Server\LifecycleHandler\SigIntHandler'

'swoole_bundle.server.http_server.configurator.with_sigint_and_limited_request_handler':
class: K911\Swoole\Server\Configurator\WithServerStartHandler
autoconfigure: false
arguments:
$decorated: '@swoole_bundle.server.http_server.configurator.with_limited_request_handler'
$handler: '@K911\Swoole\Server\LifecycleHandler\SigIntHandler'

'swoole_bundle.server.http_server.factory':
class: K911\Swoole\Server\HttpServerFactory
arguments:
$configurator: '@swoole_bundle.server.http_server.configurator.with_request_handler'

'swoole_bundle.server.http_server.sigint_factory':
class: K911\Swoole\Server\HttpServerFactory
arguments:
$configurator: '@swoole_bundle.server.http_server.configurator.with_sigint_and_request_handler'

'swoole_bundle.server.http_server.sigint_and_limited_factory':
class: K911\Swoole\Server\HttpServerFactory
arguments:
$configurator: '@swoole_bundle.server.http_server.configurator.with_sigint_and_limited_request_handler'


'K911\Swoole\Bridge\Symfony\Bundle\Command\ServerStartCommand':
tags: ['console.command']
arguments:
Expand All @@ -65,14 +92,9 @@ services:
'K911\Swoole\Bridge\Symfony\Bundle\Command\ServerRunCommand':
tags: ['console.command']
arguments:
$serverFactory: '@swoole_bundle.server.http_server.factory'

'swoole_bundle.server.http_server.limited_factory':
class: K911\Swoole\Server\HttpServerFactory
arguments:
$configurator: '@swoole_bundle.server.http_server.configurator.with_limited_request_handler'
$serverFactory: '@swoole_bundle.server.http_server.sigint_factory'

'K911\Swoole\Bridge\Symfony\Bundle\Command\ServerProfileCommand':
tags: ['console.command']
arguments:
$serverFactory: '@swoole_bundle.server.http_server.limited_factory'
$serverFactory: '@swoole_bundle.server.http_server.sigint_and_limited_factory'
30 changes: 30 additions & 0 deletions src/Server/Configurator/WithServerStartHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace K911\Swoole\Server\Configurator;

use K911\Swoole\Server\LifecycleHandler\ServerStartHandlerInterface;
use Swoole\Http\Server;

final class WithServerStartHandler implements ConfiguratorInterface
{
private $handler;
private $decorated;

public function __construct(ConfiguratorInterface $decorated, ServerStartHandlerInterface $handler)
{
$this->handler = $handler;
$this->decorated = $decorated;
}

/**
* {@inheritdoc}
*/
public function configure(Server $server): void
{
$this->decorated->configure($server);

$server->on('start', [$this->handler, 'handle']);
}
}
17 changes: 17 additions & 0 deletions src/Server/LifecycleHandler/ServerStartHandlerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace K911\Swoole\Server\LifecycleHandler;

use Swoole\Server;

interface ServerStartHandlerInterface
{
/**
* Handle "OnStart" event.
*
* @param Server $server
*/
public function handle(Server $server): void;
}
20 changes: 20 additions & 0 deletions src/Server/LifecycleHandler/SigIntHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace K911\Swoole\Server\LifecycleHandler;

use Swoole\Process;
use Swoole\Server;

final class SigIntHandler implements ServerStartHandlerInterface
{
/**
* {@inheritdoc}
*/
public function handle(Server $server): void
{
// 2 => SIGINT
Process::signal(2, [$server, 'shutdown']);
}
}

0 comments on commit 7e6c9a4

Please sign in to comment.