Skip to content

Commit

Permalink
Removes shutdown handler (#211)
Browse files Browse the repository at this point in the history
  • Loading branch information
nunomaduro authored Apr 19, 2021
1 parent e51bafc commit 3b5d6a6
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 33 deletions.
5 changes: 0 additions & 5 deletions bin/swoole-server
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

use Laravel\Octane\RequestContext;
use Laravel\Octane\Swoole\Handlers\OnManagerStart;
use Laravel\Octane\Swoole\Handlers\OnServerShutdown;
use Laravel\Octane\Swoole\Handlers\OnServerStart;
use Laravel\Octane\Swoole\Handlers\OnWorkerStart;
use Laravel\Octane\Swoole\ServerStateFile;
Expand Down Expand Up @@ -162,8 +161,4 @@ $server->on('workerstop', function () use ($workerState) {
$workerState->worker->terminate();
});

$server->on('shutdown', fn () => (new OnServerShutdown(
new ServerStateFile($serverStateFile)
))());

$server->start();
19 changes: 19 additions & 0 deletions src/Exec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Laravel\Octane;

class Exec
{
/**
* Run the given command.
*
* @param string $command
* @return array
*/
public function run($command)
{
exec($command, $output);

return $output;
}
}
1 change: 1 addition & 0 deletions src/OctaneServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public function register()
return new SwooleServerProcessInspector(
$app->make(SignalDispatcher::class),
$app->make(SwooleServerStateFile::class),
$app->make(Exec::class),
);
});

Expand Down
22 changes: 0 additions & 22 deletions src/Swoole/Handlers/OnServerShutdown.php

This file was deleted.

7 changes: 5 additions & 2 deletions src/Swoole/ServerProcessInspector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

namespace Laravel\Octane\Swoole;

use Laravel\Octane\Exec;

class ServerProcessInspector
{
public function __construct(
protected SignalDispatcher $dispatcher,
protected ServerStateFile $serverStateFile
protected ServerStateFile $serverStateFile,
protected Exec $exec,
) {
}

Expand Down Expand Up @@ -53,7 +56,7 @@ public function stopServer(): bool
'managerProcessId' => $managerProcessId
] = $this->serverStateFile->read();

exec('pgrep -P '.$managerProcessId, $workerProcessIds);
$workerProcessIds = $this->exec->run('pgrep -P '.$managerProcessId);

foreach ([$masterProcessId, $managerProcessId, ...$workerProcessIds] as $processId) {
$this->dispatcher->signal($processId, SIGKILL);
Expand Down
36 changes: 32 additions & 4 deletions tests/SwooleServerProcessInspectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Laravel\Octane\Tests;

use Laravel\Octane\Exec;
use Laravel\Octane\Swoole\ServerProcessInspector;
use Laravel\Octane\Swoole\ServerStateFile;
use Laravel\Octane\Swoole\SignalDispatcher;
Expand All @@ -14,7 +15,8 @@ public function test_can_determine_if_swoole_server_process_is_running_when_mana
{
$inspector = new ServerProcessInspector(
$dispatcher = Mockery::mock(SignalDispatcher::class),
$processIdFile = new ServerStateFile(sys_get_temp_dir().'/swoole.pid')
$processIdFile = new ServerStateFile(sys_get_temp_dir().'/swoole.pid'),
Mockery::mock(Exec::class),
);

$dispatcher->shouldReceive('canCommunicateWith')->with(2)->andReturn(true);
Expand All @@ -31,7 +33,8 @@ public function test_can_determine_if_swoole_server_process_is_running_when_mana
{
$inspector = new ServerProcessInspector(
$dispatcher = Mockery::mock(SignalDispatcher::class),
$processIdFile = new ServerStateFile(sys_get_temp_dir().'/swoole.pid')
$processIdFile = new ServerStateFile(sys_get_temp_dir().'/swoole.pid'),
Mockery::mock(Exec::class),
);

$dispatcher->shouldReceive('canCommunicateWith')->with(2)->andReturn(false);
Expand All @@ -48,7 +51,8 @@ public function test_can_determine_if_swoole_server_process_is_running_when_only
{
$inspector = new ServerProcessInspector(
$dispatcher = Mockery::mock(SignalDispatcher::class),
$processIdFile = new ServerStateFile(sys_get_temp_dir().'/swoole.pid')
$processIdFile = new ServerStateFile(sys_get_temp_dir().'/swoole.pid'),
Mockery::mock(Exec::class),
);

$dispatcher->shouldReceive('canCommunicateWith')->with(1)->andReturn(true);
Expand All @@ -65,7 +69,8 @@ public function test_can_determine_if_swoole_server_process_is_running_when_mast
{
$inspector = new ServerProcessInspector(
$dispatcher = Mockery::mock(SignalDispatcher::class),
$processIdFile = new ServerStateFile(sys_get_temp_dir().'/swoole.pid')
$processIdFile = new ServerStateFile(sys_get_temp_dir().'/swoole.pid'),
Mockery::mock(Exec::class),
);

$dispatcher->shouldReceive('canCommunicateWith')->with(1)->andReturn(false);
Expand All @@ -76,4 +81,27 @@ public function test_can_determine_if_swoole_server_process_is_running_when_mast

$processIdFile->delete();
}

public function test_swoole_server_process_can_be_stop()
{
$inspector = new ServerProcessInspector(
$dispatcher = Mockery::mock(SignalDispatcher::class),
$processIdFile = new ServerStateFile(sys_get_temp_dir().'/swoole.pid'),
$exec = Mockery::mock(Exec::class),
);

$processIdFile->writeProcessIds(3, 2);
$exec->shouldReceive('run')->once()->with('pgrep -P 2')->andReturn([4, 5]);

collect([2, 3, 4, 5])->each(
fn ($processId) => $dispatcher
->shouldReceive('signal')
->with($processId, SIGKILL)
->once(),
);

$this->assertTrue($inspector->stopServer());

$processIdFile->delete();
}
}

0 comments on commit 3b5d6a6

Please sign in to comment.