Skip to content

Commit b971f02

Browse files
committed
事件onStart onWorkerStart onFinish等事件支持自定义监听
1 parent 8d86a29 commit b971f02

File tree

7 files changed

+151
-9
lines changed

7 files changed

+151
-9
lines changed

config/swoole_http.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use Swoole\Table;
4+
use FastLaravel\Http\Server\Event;
45

56
return [
67
/*
@@ -178,6 +179,23 @@
178179
Illuminate\Pagination\PaginationServiceProvider::class,
179180
],
180181

182+
/*
183+
|--------------------------------------------------------------------------
184+
| The event listener mappings for the application.
185+
| event => listener
186+
|
187+
| @see: class FastLaravel\Http\Server\Event
188+
|--------------------------------------------------------------------------
189+
*/
190+
'listens' => [
191+
// Event::START => [
192+
// 'App\Listeners\StartListener',
193+
// ],
194+
// Event::WORKER_START => [
195+
// 'App\Listeners\WorkerStartListener',
196+
// ]
197+
],
198+
181199
/*
182200
|--------------------------------------------------------------------------
183201
| Define your swoole tables here.

src/Commands/HttpController.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Inhere\Console\Controller;
77
use Illuminate\Config\Repository;
88
use Illuminate\Events\Dispatcher;
9+
use Illuminate\Support\Facades\Event;
910
use Illuminate\Contracts\Queue\Factory as QueueFactoryContract;
1011
use FastLaravel\Http\Server\Manager;
1112

@@ -122,11 +123,21 @@ public function startCommand()
122123
'app' => $this->config('app'),
123124
'swoole_http' => $this->config('server')
124125
]));
125-
$this->laravelApp->singleton('events', function ($app) {
126-
return (new Dispatcher($app))->setQueueResolver(function () use ($app) {
126+
127+
// 注册swoole回调监听事件
128+
$listens = $this->config('server')['listens'] ?? [];
129+
$this->laravelApp->singleton('events', function ($app) use($listens) {
130+
$event = (new Dispatcher($app))->setQueueResolver(function () use ($app) {
127131
return $app->make(QueueFactoryContract::class);
128132
});
133+
foreach ($listens as $eventName => $listeners) {
134+
foreach ($listeners as $listener) {
135+
$event->listen($eventName, $listener);
136+
}
137+
}
138+
return $event;
129139
});
140+
130141
$this->output->table($this->getInfos(false), 'fast laravel http info.');
131142
(new Manager($this->laravelApp, 'laravel'))->start();
132143
}

src/Commands/HttpServerCommand.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,15 @@ public function publish()
118118
$basePath = base_path();
119119
$configPath = $basePath . '/config/swoole_http.php';
120120
$taskMiddlewarePath = $basePath . '/app/Http/Middleware/TaskWorker.php';
121+
$startListener = $basePath . '/app/Listeners/StartListener.php';
122+
$workerStartListener = $basePath . '/app/Listeners/WorkerStartListener.php';
123+
121124
$todoList = [
122125
['from' => realpath(__DIR__ . '/../../config/swoole_http.php'), 'to' => $configPath, 'mode' => 0644],
123126
['from' => realpath(__DIR__ . '/../../fast'), 'to' => $basePath . '/fast', 'mode' => 0755, 'link' => true],
124127
['from' => realpath(__DIR__ . '/../Middleware/TaskWorker.php'), 'to' => $taskMiddlewarePath, 'mode' => 0755, 'link' => true],
128+
['from' => realpath(__DIR__ . '/../Listeners/StartListener.php'), 'to' => $startListener, 'mode' => 0755, 'link' => true],
129+
['from' => realpath(__DIR__ . '/../Listeners/WorkerStartListener.php'), 'to' => $workerStartListener, 'mode' => 0755, 'link' => true],
125130
];
126131

127132
foreach ($todoList as $id => $todo) {

src/Listeners/StartListener.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace App\Listeners;
4+
5+
class StartListener
6+
{
7+
/**
8+
* Create the event listener.
9+
*/
10+
public function __construct()
11+
{
12+
//
13+
}
14+
15+
/**
16+
* Handle the event.
17+
* @param \Swoole\Http\Server $server
18+
*
19+
* @return void
20+
*/
21+
public function handle($server)
22+
{
23+
echo "In StartListener\n";
24+
}
25+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace App\Listeners;
4+
5+
class WorkerStartListener
6+
{
7+
/**
8+
* Create the event listener.
9+
*/
10+
public function __construct()
11+
{
12+
//
13+
}
14+
15+
/**
16+
* Handle the event.
17+
*
18+
* @param \Swoole\Http\Server $server
19+
* @params int $workerId
20+
*
21+
* @return void
22+
*/
23+
public function handle($server, $workerId)
24+
{
25+
echo "In workerStartListener:worker id:{$workerId}\n";
26+
}
27+
}

src/Server/Event.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace FastLaravel\Http\Server;
4+
5+
/**
6+
* Class Event
7+
* fast laravel支持监听的事件
8+
* @package FastLaravel\Http\Server
9+
*/
10+
class Event
11+
{
12+
/**
13+
* 对应 onStart 回调
14+
*/
15+
const START = 'start';
16+
17+
/**
18+
* 对应 onManagerStart 回调
19+
*/
20+
const MANAGER_START = 'managerStart';
21+
22+
/**
23+
* 对应 onWorkerStart 回调
24+
*/
25+
const WORKER_START = 'workerStart';
26+
27+
/**
28+
* 对应 onFinish 回调
29+
*/
30+
const FINISH = 'finish';
31+
32+
/**
33+
* 对应 onWorkerStop 回调
34+
*/
35+
const WORKER_STOP = 'workerStop';
36+
37+
/**
38+
* 对应 onShutdown 回调
39+
*/
40+
const SHUTDOWN = 'shutdown';
41+
}

src/Server/Manager.php

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use FastLaravel\Http\Process\HotReload;
1414
use FastLaravel\Http\Database\ConnectionResolver;
1515
use FastLaravel\Http\Facade\Show;
16+
use FastLaravel\Http\Server\Event;
1617
use Illuminate\Contracts\Container\Container;
1718
use Illuminate\Contracts\Debug\ExceptionHandler;
1819

@@ -264,7 +265,7 @@ protected function createTracker()
264265
*/
265266
public function onStart($server)
266267
{
267-
$this->container->make('events')->dispatch('start', func_get_args());
268+
$this->container->make('events')->dispatch(Event::START, func_get_args());
268269
$this->setProcessName('master process');
269270
$this->createPidFile();
270271
if (isTesting()) {
@@ -282,19 +283,20 @@ public function onStart($server)
282283
*/
283284
public function onManagerStart($server)
284285
{
285-
$this->container->make('events')->dispatch('managerStart', func_get_args());
286+
$this->container->make('events')->dispatch(Event::MANAGER_START, func_get_args());
286287
$this->setProcessName('manager process');
287288
}
288289

289290
/**
290291
* "onWorkerStart" callback.
291292
*
292293
* @param HttpServer $server
294+
* @param int $workerId
293295
* @throws
294296
*/
295-
public function onWorkerStart($server)
297+
public function onWorkerStart($server, $workerId)
296298
{
297-
$this->container->make('events')->dispatch('workerStart', func_get_args());
299+
$this->container->make('events')->dispatch(Event::WORKER_START, func_get_args());
298300
$this->clearCache();
299301

300302
// init laravel app in task workers
@@ -417,23 +419,36 @@ public function onTaskCo(HttpServer $server, $task)
417419

418420
/**
419421
* Set onFinish callback.
422+
* @param HttpServer $server
423+
* @param int $taskId
424+
* @param string $data
425+
* @throws
420426
*/
421-
public function onFinish(HttpServer $server, $taskId, $data)
427+
public function onFinish($server, $taskId, $data)
422428
{
429+
$this->container->make('events')->dispatch(Event::FINISH, func_get_args());
423430
}
424431

425432
/**
426433
* Set onWorkerStop callback.
434+
*
435+
* @param HttpServer $server
436+
* @param int $workerId
437+
* @throws
427438
*/
428-
public function onWorkerStop()
439+
public function onWorkerStop($server, $workerId)
429440
{
441+
$this->container->make('events')->dispatch(Event::WORKER_STOP, func_get_args());
430442
}
431443

432444
/**
433445
* Set onShutdown callback.
446+
* @param HttpServer $server
447+
* @throws
434448
*/
435-
public function onShutdown()
449+
public function onShutdown($server)
436450
{
451+
$this->container->make('events')->dispatch(Event::SHUTDOWN, func_get_args());
437452
$this->removePidFile();
438453
}
439454

0 commit comments

Comments
 (0)