This commit is contained in:
2021-08-12 14:08:40 +08:00
parent 3bd3a39642
commit 14cb8a45d3
12 changed files with 152 additions and 21 deletions
+18 -1
View File
@@ -21,11 +21,28 @@ abstract class Server
{ {
/**
* @param $prefix
* @throws ConfigException
*/
protected function setProcessName($prefix)
{
if (Kiri::getPlatform()->isMac()) {
return;
}
$name = Config::get('id', 'system-service');
if (!empty($prefix)) {
$name .= '.' . $prefix;
}
swoole_set_process_name($name);
}
/** /**
* Server constructor. * Server constructor.
* @throws Exception * @throws Exception
*/ */
public function __construct(protected \Swoole\Server|Port $server) public function __construct()
{ {
} }
+18
View File
@@ -0,0 +1,18 @@
<?php
namespace Server\Events;
use Swoole\Server;
class OnAfterReload
{
/**
* @param Server $server
*/
public function __construct(Server $server)
{
}
}
+18
View File
@@ -0,0 +1,18 @@
<?php
namespace Server\Events;
use Swoole\Server;
class OnBeforeReload
{
/**
* @param Server $server
*/
public function __construct(Server $server)
{
}
}
+16
View File
@@ -0,0 +1,16 @@
<?php
namespace Server\Events;
use Swoole\Server;
class OnManagerStart
{
/**
* @param Server $server
*/
public function __construct(Server $server)
{
}
}
+18
View File
@@ -0,0 +1,18 @@
<?php
namespace Server\Events;
use Swoole\Server;
class OnManagerStop
{
/**
* @param Server $server
*/
public function __construct(Server $server)
{
}
}
+18
View File
@@ -0,0 +1,18 @@
<?php
namespace Server\Events;
use Swoole\Server;
class OnShutdown
{
/**
* @param Server $server
*/
public function __construct(Server $server)
{
}
}
+18
View File
@@ -0,0 +1,18 @@
<?php
namespace Server\Events;
use Swoole\Server;
class OnStart
{
/**
* @param Server $server
*/
public function __construct(Server $server)
{
}
}
-2
View File
@@ -3,9 +3,7 @@
namespace Server\Manager; namespace Server\Manager;
use Annotation\Inject; use Annotation\Inject;
use Kafka\Message;
use Server\Abstracts\Server; use Server\Abstracts\Server;
use Server\Constant;
use Exception; use Exception;
use Server\Events\OnAfterRequest; use Server\Events\OnAfterRequest;
use Server\SInterface\PipeMessage; use Server\SInterface\PipeMessage;
+15 -5
View File
@@ -6,7 +6,11 @@ use Annotation\Inject;
use Exception; use Exception;
use Server\Abstracts\Server; use Server\Abstracts\Server;
use Server\Constant; use Server\Constant;
use Server\Events\OnAfterReload;
use Server\Events\OnAfterRequest; use Server\Events\OnAfterRequest;
use Server\Events\OnBeforeReload;
use Server\Events\OnShutdown;
use Server\Events\OnStart;
use Server\SInterface\PipeMessage; use Server\SInterface\PipeMessage;
use Kiri\Event; use Kiri\Event;
use Kiri\Events\EventDispatch; use Kiri\Events\EventDispatch;
@@ -20,8 +24,14 @@ use Kiri\Exception\ConfigException;
class OnServerDefault extends Server class OnServerDefault extends Server
{ {
/**
* @var EventDispatch
*/
#[Inject(EventDispatch::class)]
public EventDispatch $eventDispatch;
/**
/**
* @param \Swoole\Server $server * @param \Swoole\Server $server
* @throws ConfigException * @throws ConfigException
*/ */
@@ -29,7 +39,7 @@ class OnServerDefault extends Server
{ {
$this->setProcessName(sprintf('start[%d].server', $server->master_pid)); $this->setProcessName(sprintf('start[%d].server', $server->master_pid));
$this->runEvent(Constant::START, null, [$server]); $this->eventDispatch->dispatch(new OnStart($server));
} }
@@ -38,7 +48,7 @@ class OnServerDefault extends Server
*/ */
public function onShutdown(\Swoole\Server $server) public function onShutdown(\Swoole\Server $server)
{ {
$this->runEvent(Constant::SHUTDOWN, null, [$server]); $this->eventDispatch->dispatch(new OnShutdown($server));
} }
@@ -48,7 +58,7 @@ class OnServerDefault extends Server
*/ */
public function onBeforeReload(\Swoole\Server $server) public function onBeforeReload(\Swoole\Server $server)
{ {
$this->runEvent(Constant::BEFORE_RELOAD, null, [$server]); $this->eventDispatch->dispatch(new OnBeforeReload($server));
} }
@@ -57,7 +67,7 @@ class OnServerDefault extends Server
*/ */
public function onAfterReload(\Swoole\Server $server) public function onAfterReload(\Swoole\Server $server)
{ {
$this->runEvent(Constant::AFTER_RELOAD, null, [$server]); $this->eventDispatch->dispatch(new OnAfterReload($server));
} }
} }
+13 -4
View File
@@ -2,9 +2,12 @@
namespace Server\Manager; namespace Server\Manager;
use Annotation\Inject;
use Kiri\Events\EventDispatch;
use Server\Abstracts\Server; use Server\Abstracts\Server;
use Server\Constant;
use Kiri\Exception\ConfigException; use Kiri\Exception\ConfigException;
use Server\Events\OnManagerStart;
use Server\Events\OnManagerStop;
/** /**
@@ -14,8 +17,14 @@ use Kiri\Exception\ConfigException;
class OnServerManager extends Server class OnServerManager extends Server
{ {
/**
* @var EventDispatch
*/
#[Inject(EventDispatch::class)]
public EventDispatch $eventDispatch;
/**
/**
* @param \Swoole\Server $server * @param \Swoole\Server $server
* @throws ConfigException * @throws ConfigException
*/ */
@@ -23,7 +32,7 @@ class OnServerManager extends Server
{ {
$this->setProcessName(sprintf('manger[%d].0', $server->manager_pid)); $this->setProcessName(sprintf('manger[%d].0', $server->manager_pid));
$this->runEvent(Constant::MANAGER_START, null, [$server]); $this->eventDispatch->dispatch(new OnManagerStart($server));
} }
@@ -32,7 +41,7 @@ class OnServerManager extends Server
*/ */
public function onManagerStop(\Swoole\Server $server) public function onManagerStop(\Swoole\Server $server)
{ {
$this->runEvent(Constant::MANAGER_STOP, null, [$server]); $this->eventDispatch->dispatch(new OnManagerStop($server));
} }
-1
View File
@@ -57,7 +57,6 @@ class OnServerTask
* @param $data * @param $data
* @return null * @return null
* @throws ReflectionException * @throws ReflectionException
* @throws NotFindClassException
*/ */
private function resolve($data) private function resolve($data)
{ {
-8
View File
@@ -53,8 +53,6 @@ class OnServerWorker extends \Server\Abstracts\Server
$this->eventDispatch->dispatch(new OnWorkerStart($server, $workerId)); $this->eventDispatch->dispatch(new OnWorkerStart($server, $workerId));
$this->runEvent(Constant::WORKER_START, null, [$server, $workerId]);
$this->workerInitExecutor($server, $annotation, $workerId); $this->workerInitExecutor($server, $annotation, $workerId);
$this->interpretDirectory($annotation); $this->interpretDirectory($annotation);
@@ -141,8 +139,6 @@ class OnServerWorker extends \Server\Abstracts\Server
*/ */
public function onWorkerStop(Server $server, int $workerId) public function onWorkerStop(Server $server, int $workerId)
{ {
$this->runEvent(Constant::WORKER_STOP, null, [$server, $workerId]);
$this->eventDispatch->dispatch(new OnWorkerStop($server, $workerId)); $this->eventDispatch->dispatch(new OnWorkerStop($server, $workerId));
Timer::clearAll(); Timer::clearAll();
@@ -156,8 +152,6 @@ class OnServerWorker extends \Server\Abstracts\Server
*/ */
public function onWorkerExit(Server $server, int $workerId) public function onWorkerExit(Server $server, int $workerId)
{ {
$this->runEvent(Constant::WORKER_EXIT, null, [$server, $workerId]);
putenv('state=exit'); putenv('state=exit');
$this->eventDispatch->dispatch(new OnWorkerExit($server, $workerId)); $this->eventDispatch->dispatch(new OnWorkerExit($server, $workerId));
@@ -176,8 +170,6 @@ class OnServerWorker extends \Server\Abstracts\Server
*/ */
public function onWorkerError(Server $server, int $worker_id, int $worker_pid, int $exit_code, int $signal) public function onWorkerError(Server $server, int $worker_id, int $worker_pid, int $exit_code, int $signal)
{ {
$this->runEvent(Constant::WORKER_ERROR, null, [$server, $worker_id, $worker_pid, $exit_code, $signal]);
$this->eventDispatch->dispatch(new OnWorkerError($server, $worker_id, $worker_pid, $exit_code, $signal)); $this->eventDispatch->dispatch(new OnWorkerError($server, $worker_id, $worker_pid, $exit_code, $signal));
$message = sprintf('Worker#%d::%d error stop. signal %d, exit_code %d, msg %s', $message = sprintf('Worker#%d::%d error stop. signal %d, exit_code %d, msg %s',