Skip to content

Commit 3b5d6a6

Browse files
authored
Removes shutdown handler (#211)
1 parent e51bafc commit 3b5d6a6

File tree

6 files changed

+57
-33
lines changed

6 files changed

+57
-33
lines changed

bin/swoole-server

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
use Laravel\Octane\RequestContext;
55
use Laravel\Octane\Swoole\Handlers\OnManagerStart;
6-
use Laravel\Octane\Swoole\Handlers\OnServerShutdown;
76
use Laravel\Octane\Swoole\Handlers\OnServerStart;
87
use Laravel\Octane\Swoole\Handlers\OnWorkerStart;
98
use Laravel\Octane\Swoole\ServerStateFile;
@@ -162,8 +161,4 @@ $server->on('workerstop', function () use ($workerState) {
162161
$workerState->worker->terminate();
163162
});
164163

165-
$server->on('shutdown', fn () => (new OnServerShutdown(
166-
new ServerStateFile($serverStateFile)
167-
))());
168-
169164
$server->start();

src/Exec.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Laravel\Octane;
4+
5+
class Exec
6+
{
7+
/**
8+
* Run the given command.
9+
*
10+
* @param string $command
11+
* @return array
12+
*/
13+
public function run($command)
14+
{
15+
exec($command, $output);
16+
17+
return $output;
18+
}
19+
}

src/OctaneServiceProvider.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public function register()
6060
return new SwooleServerProcessInspector(
6161
$app->make(SignalDispatcher::class),
6262
$app->make(SwooleServerStateFile::class),
63+
$app->make(Exec::class),
6364
);
6465
});
6566

src/Swoole/Handlers/OnServerShutdown.php

Lines changed: 0 additions & 22 deletions
This file was deleted.

src/Swoole/ServerProcessInspector.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22

33
namespace Laravel\Octane\Swoole;
44

5+
use Laravel\Octane\Exec;
6+
57
class ServerProcessInspector
68
{
79
public function __construct(
810
protected SignalDispatcher $dispatcher,
9-
protected ServerStateFile $serverStateFile
11+
protected ServerStateFile $serverStateFile,
12+
protected Exec $exec,
1013
) {
1114
}
1215

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

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

5861
foreach ([$masterProcessId, $managerProcessId, ...$workerProcessIds] as $processId) {
5962
$this->dispatcher->signal($processId, SIGKILL);

tests/SwooleServerProcessInspectorTest.php

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Laravel\Octane\Tests;
44

5+
use Laravel\Octane\Exec;
56
use Laravel\Octane\Swoole\ServerProcessInspector;
67
use Laravel\Octane\Swoole\ServerStateFile;
78
use Laravel\Octane\Swoole\SignalDispatcher;
@@ -14,7 +15,8 @@ public function test_can_determine_if_swoole_server_process_is_running_when_mana
1415
{
1516
$inspector = new ServerProcessInspector(
1617
$dispatcher = Mockery::mock(SignalDispatcher::class),
17-
$processIdFile = new ServerStateFile(sys_get_temp_dir().'/swoole.pid')
18+
$processIdFile = new ServerStateFile(sys_get_temp_dir().'/swoole.pid'),
19+
Mockery::mock(Exec::class),
1820
);
1921

2022
$dispatcher->shouldReceive('canCommunicateWith')->with(2)->andReturn(true);
@@ -31,7 +33,8 @@ public function test_can_determine_if_swoole_server_process_is_running_when_mana
3133
{
3234
$inspector = new ServerProcessInspector(
3335
$dispatcher = Mockery::mock(SignalDispatcher::class),
34-
$processIdFile = new ServerStateFile(sys_get_temp_dir().'/swoole.pid')
36+
$processIdFile = new ServerStateFile(sys_get_temp_dir().'/swoole.pid'),
37+
Mockery::mock(Exec::class),
3538
);
3639

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

5458
$dispatcher->shouldReceive('canCommunicateWith')->with(1)->andReturn(true);
@@ -65,7 +69,8 @@ public function test_can_determine_if_swoole_server_process_is_running_when_mast
6569
{
6670
$inspector = new ServerProcessInspector(
6771
$dispatcher = Mockery::mock(SignalDispatcher::class),
68-
$processIdFile = new ServerStateFile(sys_get_temp_dir().'/swoole.pid')
72+
$processIdFile = new ServerStateFile(sys_get_temp_dir().'/swoole.pid'),
73+
Mockery::mock(Exec::class),
6974
);
7075

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

7782
$processIdFile->delete();
7883
}
84+
85+
public function test_swoole_server_process_can_be_stop()
86+
{
87+
$inspector = new ServerProcessInspector(
88+
$dispatcher = Mockery::mock(SignalDispatcher::class),
89+
$processIdFile = new ServerStateFile(sys_get_temp_dir().'/swoole.pid'),
90+
$exec = Mockery::mock(Exec::class),
91+
);
92+
93+
$processIdFile->writeProcessIds(3, 2);
94+
$exec->shouldReceive('run')->once()->with('pgrep -P 2')->andReturn([4, 5]);
95+
96+
collect([2, 3, 4, 5])->each(
97+
fn ($processId) => $dispatcher
98+
->shouldReceive('signal')
99+
->with($processId, SIGKILL)
100+
->once(),
101+
);
102+
103+
$this->assertTrue($inspector->stopServer());
104+
105+
$processIdFile->delete();
106+
}
79107
}

0 commit comments

Comments
 (0)