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

Commit

Permalink
feat(server): Set-up hooks on lifecycle events
Browse files Browse the repository at this point in the history
  • Loading branch information
k911 committed Mar 19, 2019
1 parent 35eb034 commit 271a341
Show file tree
Hide file tree
Showing 22 changed files with 443 additions and 47 deletions.
22 changes: 6 additions & 16 deletions src/Bridge/Symfony/Bundle/DependencyInjection/SwooleExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use K911\Swoole\Bridge\Symfony\HttpKernel\DebugHttpKernelRequestHandler;
use K911\Swoole\Server\Config\Socket;
use K911\Swoole\Server\Configurator\ConfiguratorInterface;
use K911\Swoole\Server\Configurator\WithWorkerStartHandler;
use K911\Swoole\Server\HttpServerConfiguration;
use K911\Swoole\Server\RequestHandler\AdvancedStaticFilesServer;
use K911\Swoole\Server\RequestHandler\RequestHandlerInterface;
Expand Down Expand Up @@ -120,13 +119,6 @@ private function registerHttpServerConfiguration(array $config, ContainerBuilder
->addArgument($settings);

$this->registerHttpServerHMR($hmr, $container);

if ($container->has(WorkerStartHandlerInterface::class)) {
$container->register(WithWorkerStartHandler::class)
->setAutowired(true)
->setAutoconfigured(true)
->setPublic(false);
}
}

private function registerHttpServerHMR(string $hmr, ContainerBuilder $container): void
Expand All @@ -136,18 +128,16 @@ private function registerHttpServerHMR(string $hmr, ContainerBuilder $container)
}

if ('inotify' === $hmr) {
$container->register(HotModuleReloaderInterface::class, InotifyHMR::class)
->setAutowired(true)
$container->autowire(HotModuleReloaderInterface::class, InotifyHMR::class)
->setAutoconfigured(true)
->setPublic(false);
}

if (!$container->has(WorkerStartHandlerInterface::class)) {
$container->register(WorkerStartHandlerInterface::class, HMRWorkerStartHandler::class)
->setAutowired(true)
->setAutoconfigured(true)
->setPublic(false);
}
$container->autowire(HMRWorkerStartHandler::class)
->setAutoconfigured(false)
->setPublic(false)
->setArgument('$decorated', new Reference(HMRWorkerStartHandler::class.'.inner'))
->setDecoratedService(WorkerStartHandlerInterface::class);
}

private function resolveAutoHMR(): string
Expand Down
26 changes: 25 additions & 1 deletion src/Bridge/Symfony/Bundle/Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,35 @@ services:

'K911\Swoole\Server\HttpServer':

'K911\Swoole\Server\WorkerHandler\WorkerStartHandlerInterface':
class: K911\Swoole\Server\WorkerHandler\NoOpWorkerStartHandler

'K911\Swoole\Server\LifecycleHandler\ServerStartHandlerInterface':
class: K911\Swoole\Server\LifecycleHandler\NoOpServerStartHandler

'K911\Swoole\Server\LifecycleHandler\ServerShutdownHandlerInterface':
class: K911\Swoole\Server\LifecycleHandler\NoOpServerShutdownHandler

'K911\Swoole\Server\LifecycleHandler\ServerManagerStartHandlerInterface':
class: K911\Swoole\Server\LifecycleHandler\NoOpServerManagerStartHandler

'K911\Swoole\Server\LifecycleHandler\ServerManagerStopHandlerInterface':
class: K911\Swoole\Server\LifecycleHandler\NoOpServerManagerStopHandler

'K911\Swoole\Server\HttpServerConfiguration':

'K911\Swoole\Server\Configurator\WithHttpServerConfiguration':

'K911\Swoole\Server\Configurator\WithServerShutdownHandler':

'K911\Swoole\Server\Configurator\WithServerStartHandler':

'K911\Swoole\Server\Configurator\WithServerManagerStartHandler':

'K911\Swoole\Server\Configurator\WithServerManagerStopHandler':

'K911\Swoole\Server\Configurator\WithWorkerStartHandler':

'K911\Swoole\Server\Configurator\CallableChainConfiguratorFactory':

'swoole_bundle.server.http_server.configurator_collection':
Expand All @@ -61,4 +86,3 @@ services:
autoconfigure: false
arguments:
$handler: '@K911\Swoole\Server\LifecycleHandler\SigIntHandler'

26 changes: 26 additions & 0 deletions src/Server/Configurator/WithServerManagerStartHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace K911\Swoole\Server\Configurator;

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

final class WithServerManagerStartHandler implements ConfiguratorInterface
{
private $handler;

public function __construct(ServerManagerStartHandlerInterface $handler)
{
$this->handler = $handler;
}

/**
* {@inheritdoc}
*/
public function configure(Server $server): void
{
$server->on('ManagerStart', [$this->handler, 'handle']);
}
}
26 changes: 26 additions & 0 deletions src/Server/Configurator/WithServerManagerStopHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace K911\Swoole\Server\Configurator;

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

final class WithServerManagerStopHandler implements ConfiguratorInterface
{
private $handler;

public function __construct(ServerManagerStopHandlerInterface $handler)
{
$this->handler = $handler;
}

/**
* {@inheritdoc}
*/
public function configure(Server $server): void
{
$server->on('ManagerStop', [$this->handler, 'handle']);
}
}
26 changes: 26 additions & 0 deletions src/Server/Configurator/WithServerShutdownHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace K911\Swoole\Server\Configurator;

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

final class WithServerShutdownHandler implements ConfiguratorInterface
{
private $handler;

public function __construct(ServerShutdownHandlerInterface $handler)
{
$this->handler = $handler;
}

/**
* {@inheritdoc}
*/
public function configure(Server $server): void
{
$server->on('shutdown', [$this->handler, 'handle']);
}
}
18 changes: 18 additions & 0 deletions src/Server/LifecycleHandler/NoOpServerManagerStartHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace K911\Swoole\Server\LifecycleHandler;

use Swoole\Server;

final class NoOpServerManagerStartHandler implements ServerManagerStartHandlerInterface
{
/**
* {@inheritdoc}
*/
public function handle(Server $server): void
{
// noop
}
}
18 changes: 18 additions & 0 deletions src/Server/LifecycleHandler/NoOpServerManagerStopHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace K911\Swoole\Server\LifecycleHandler;

use Swoole\Server;

final class NoOpServerManagerStopHandler implements ServerManagerStopHandlerInterface
{
/**
* {@inheritdoc}
*/
public function handle(Server $server): void
{
// noop
}
}
18 changes: 18 additions & 0 deletions src/Server/LifecycleHandler/NoOpServerShutdownHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace K911\Swoole\Server\LifecycleHandler;

use Swoole\Server;

final class NoOpServerShutdownHandler implements ServerShutdownHandlerInterface
{
/**
* {@inheritdoc}
*/
public function handle(Server $server): void
{
// noop
}
}
18 changes: 18 additions & 0 deletions src/Server/LifecycleHandler/NoOpServerStartHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace K911\Swoole\Server\LifecycleHandler;

use Swoole\Server;

final class NoOpServerStartHandler implements ServerStartHandlerInterface
{
/**
* {@inheritdoc}
*/
public function handle(Server $server): void
{
// noop
}
}
19 changes: 19 additions & 0 deletions src/Server/LifecycleHandler/ServerManagerStartHandlerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace K911\Swoole\Server\LifecycleHandler;

use Swoole\Server;

interface ServerManagerStartHandlerInterface
{
/**
* Handle "OnManagerStart" event.
*
* Info: Handler is executed in manager process
*
* @param Server $server
*/
public function handle(Server $server): void;
}
19 changes: 19 additions & 0 deletions src/Server/LifecycleHandler/ServerManagerStopHandlerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace K911\Swoole\Server\LifecycleHandler;

use Swoole\Server;

interface ServerManagerStopHandlerInterface
{
/**
* Handle "OnManagerStop" event.
*
* Info: Handler is executed in manager process
*
* @param Server $server
*/
public function handle(Server $server): void;
}
17 changes: 17 additions & 0 deletions src/Server/LifecycleHandler/ServerShutdownHandlerInterface.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 ServerShutdownHandlerInterface
{
/**
* Handle "OnShutdown" event.
*
* @param Server $server
*/
public function handle(Server $server): void;
}
11 changes: 11 additions & 0 deletions src/Server/LifecycleHandler/SigIntHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,23 @@

final class SigIntHandler implements ServerStartHandlerInterface
{
private $decorated;

public function __construct(?ServerStartHandlerInterface $decorated = null)
{
$this->decorated = $decorated;
}

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

if ($this->decorated instanceof ServerStartHandlerInterface) {
$this->decorated->handle($server);
}
}
}
8 changes: 7 additions & 1 deletion src/Server/WorkerHandler/HMRWorkerStartHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,24 @@ final class HMRWorkerStartHandler implements WorkerStartHandlerInterface
{
private $hmr;
private $interval;
private $decorated;

public function __construct(HotModuleReloaderInterface $hmr, int $interval = 2000)
public function __construct(HotModuleReloaderInterface $hmr, int $interval = 2000, ?WorkerStartHandlerInterface $decorated = null)
{
$this->hmr = $hmr;
$this->interval = $interval;
$this->decorated = $decorated;
}

/**
* {@inheritdoc}
*/
public function handle(Server $worker, int $workerId): void
{
if ($this->decorated instanceof WorkerStartHandlerInterface) {
$this->decorated->handle($worker, $workerId);
}

if ($worker->taskworker) {
return;
}
Expand Down
18 changes: 18 additions & 0 deletions src/Server/WorkerHandler/NoOpWorkerStartHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace K911\Swoole\Server\WorkerHandler;

use Swoole\Server;

final class NoOpWorkerStartHandler implements WorkerStartHandlerInterface
{
/**
* {@inheritdoc}
*/
public function handle(Server $worker, int $workerId): void
{
// noop
}
}
10 changes: 5 additions & 5 deletions src/Server/WorkerHandler/WorkerStartHandlerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ interface WorkerStartHandlerInterface
*
* To differentiate between server worker and task worker use snippet:
*
* ```php
* if($server->taskworker) {
* echo "Hello from task worker process";
* }
* ```
* ```php
* if($server->taskworker) {
* echo "Hello from task worker process";
* }
* ```
*
* @param Server $worker
* @param int $workerId
Expand Down

0 comments on commit 271a341

Please sign in to comment.