This commit is contained in:
2021-10-19 15:07:02 +08:00
parent ea5b473638
commit 95b4a3e80b
5 changed files with 69 additions and 35 deletions
+1
View File
@@ -13,6 +13,7 @@ class Constant
const START = 'Start'; const START = 'Start';
const SHUTDOWN = 'Shutdown'; const SHUTDOWN = 'Shutdown';
const BEFORE_SHUTDOWN = 'beforeShutdown';
const WORKER_START = 'WorkerStart'; const WORKER_START = 'WorkerStart';
const WORKER_STOP = 'WorkerStop'; const WORKER_STOP = 'WorkerStop';
const WORKER_EXIT = 'WorkerExit'; const WORKER_EXIT = 'WorkerExit';
+18
View File
@@ -0,0 +1,18 @@
<?php
namespace Server\Events;
use Swoole\Server;
class OnBeforeShutdown
{
/**
* @param Server|null $server
*/
public function __construct(?Server $server = null)
{
}
}
+18 -10
View File
@@ -3,11 +3,12 @@
namespace Server\Manager; namespace Server\Manager;
use Annotation\Inject; use Annotation\Inject;
use Server\Abstracts\Server;
use Server\Events\OnShutdown;
use Server\Events\OnStart;
use Kiri\Events\EventDispatch; use Kiri\Events\EventDispatch;
use Kiri\Exception\ConfigException; use Kiri\Exception\ConfigException;
use Server\Abstracts\Server;
use Server\Events\OnBeforeShutdown;
use Server\Events\OnShutdown;
use Server\Events\OnStart;
/** /**
@@ -25,18 +26,26 @@ class OnServer extends Server
/** /**
* @param \Swoole\Server $server * @param \Swoole\Server $server
* @throws ConfigException * @throws ConfigException
*/ */
public function onStart(\Swoole\Server $server) public function onStart(\Swoole\Server $server)
{ {
$this->setProcessName(sprintf('start[%d].server', $server->master_pid)); $this->setProcessName(sprintf('start[%d].server', $server->master_pid));
$this->eventDispatch->dispatch(new OnStart($server)); $this->eventDispatch->dispatch(new OnStart($server));
}
/**
* @param \Swoole\Server $server
*/
public function onBeforeShutdown(\Swoole\Server $server)
{
$this->eventDispatch->dispatch(new OnBeforeShutdown($server));
} }
/** /**
* @param \Swoole\Server $server * @param \Swoole\Server $server
*/ */
@@ -46,5 +55,4 @@ class OnServer extends Server
} }
} }
@@ -0,0 +1,8 @@
<?php
namespace Server\SInterface;
interface OnBeforeShutdown
{
}
+24 -25
View File
@@ -14,13 +14,13 @@ use Server\Manager\OnPipeMessage;
use Server\Manager\OnServer; use Server\Manager\OnServer;
use Server\Manager\OnServerManager; use Server\Manager\OnServerManager;
use Server\Manager\OnServerReload; use Server\Manager\OnServerReload;
use Server\SInterface\OnProcessInterface;
use Server\SInterface\OnCloseInterface; use Server\SInterface\OnCloseInterface;
use Server\SInterface\OnConnectInterface; use Server\SInterface\OnConnectInterface;
use Server\SInterface\OnDisconnectInterface; use Server\SInterface\OnDisconnectInterface;
use Server\SInterface\OnHandshakeInterface; use Server\SInterface\OnHandshakeInterface;
use Server\SInterface\OnMessageInterface; use Server\SInterface\OnMessageInterface;
use Server\SInterface\OnPacketInterface; use Server\SInterface\OnPacketInterface;
use Server\SInterface\OnProcessInterface;
use Server\SInterface\OnReceiveInterface; use Server\SInterface\OnReceiveInterface;
use Server\SInterface\OnTaskInterface; use Server\SInterface\OnTaskInterface;
use Server\Task\OnServerTask; use Server\Task\OnServerTask;
@@ -62,30 +62,29 @@ class ServerManager
const DEFAULT_EVENT = [ const DEFAULT_EVENT = [
Constant::WORKER_START => [OnServerWorker::class, 'onWorkerStart'], Constant::WORKER_START => [OnServerWorker::class, 'onWorkerStart'],
Constant::WORKER_EXIT => [OnServerWorker::class, 'onWorkerExit'], Constant::WORKER_EXIT => [OnServerWorker::class, 'onWorkerExit'],
Constant::WORKER_STOP => [OnServerWorker::class, 'onWorkerStop'], Constant::WORKER_STOP => [OnServerWorker::class, 'onWorkerStop'],
Constant::WORKER_ERROR => [OnServerWorker::class, 'onWorkerError'], Constant::WORKER_ERROR => [OnServerWorker::class, 'onWorkerError'],
Constant::MANAGER_START => [OnServerManager::class, 'onManagerStart'], Constant::MANAGER_START => [OnServerManager::class, 'onManagerStart'],
Constant::MANAGER_STOP => [OnServerManager::class, 'onManagerStop'], Constant::MANAGER_STOP => [OnServerManager::class, 'onManagerStop'],
Constant::BEFORE_RELOAD => [OnServerReload::class, 'onBeforeReload'], Constant::BEFORE_RELOAD => [OnServerReload::class, 'onBeforeReload'],
Constant::AFTER_RELOAD => [OnServerReload::class, 'onAfterReload'], Constant::AFTER_RELOAD => [OnServerReload::class, 'onAfterReload'],
Constant::START => [OnServer::class, 'onStart'], Constant::START => [OnServer::class, 'onStart'],
Constant::SHUTDOWN => [OnServer::class, 'onShutdown'], Constant::BEFORE_SHUTDOWN => [OnServer::class, 'onBeforeShutdown'],
Constant::SHUTDOWN => [OnServer::class, 'onShutdown'],
]; ];
private array $eventInterface = [
private array $eventInterface = [ OnReceiveInterface::class => 'receive',
OnReceiveInterface::class => 'receive', OnPacketInterface::class => 'packet',
OnPacketInterface::class =>'packet', OnHandshakeInterface::class => 'handshake',
OnHandshakeInterface::class => 'handshake', OnMessageInterface::class => 'message',
OnMessageInterface::class =>'message', OnConnectInterface::class => 'connect',
OnConnectInterface::class =>'connect', OnCloseInterface::class => 'close',
OnCloseInterface::class =>'close', OnDisconnectInterface::class => 'disconnect'
OnDisconnectInterface::class =>'disconnect' ];
];
/** /**
@@ -189,9 +188,9 @@ class ServerManager
} }
$name = $customProcess->getProcessName($soloProcess); $name = $customProcess->getProcessName($soloProcess);
if (is_enable_file_modification_listening()) { if (is_enable_file_modification_listening()) {
scan_directory(directory('app'), 'App'); scan_directory(directory('app'), 'App');
} }
$system = sprintf('%s.process[%d]', Config::get('id', 'system-service'), $soloProcess->pid); $system = sprintf('%s.process[%d]', Config::get('id', 'system-service'), $soloProcess->pid);
if (Kiri::getPlatform()->isLinux()) { if (Kiri::getPlatform()->isLinux()) {