This commit is contained in:
2021-08-18 11:56:19 +08:00
parent f375eec0a0
commit 99e2f36794
9 changed files with 21 additions and 14 deletions
+1 -1
View File
@@ -98,7 +98,7 @@ class ServerManager
*/ */
public function initBaseServer($configs, int $daemon = 0): void public function initBaseServer($configs, int $daemon = 0): void
{ {
$context = ServerManager::getContext(); $context = di(ServerManager::class);
foreach ($this->sortService($configs['ports']) as $config) { foreach ($this->sortService($configs['ports']) as $config) {
$this->startListenerHandler($context, $config, $daemon); $this->startListenerHandler($context, $config, $daemon);
} }
+4 -3
View File
@@ -213,11 +213,11 @@ abstract class BaseApplication extends Component
/** /**
* @param TaskExecute $execute * @param TaskExecute $execute
* @throws ReflectionException * @throws ReflectionException|NotFindClassException
*/ */
public function task(TaskExecute $execute): void public function task(TaskExecute $execute): void
{ {
ServerManager::getContext()->task($execute); di(ServerManager::class)->task($execute);
} }
@@ -400,10 +400,11 @@ abstract class BaseApplication extends Component
/** /**
* @return \Swoole\Http\Server|\Swoole\Server|\Swoole\WebSocket\Server|null * @return \Swoole\Http\Server|\Swoole\Server|\Swoole\WebSocket\Server|null
* @throws
*/ */
public function getSwoole(): \Swoole\Http\Server|\Swoole\Server|\Swoole\WebSocket\Server|null public function getSwoole(): \Swoole\Http\Server|\Swoole\Server|\Swoole\WebSocket\Server|null
{ {
return ServerManager::getContext()->getServer(); return di(ServerManager::class)->getServer();
} }
+1 -1
View File
@@ -39,7 +39,7 @@ class Async extends Component
*/ */
public function dispatch(string $name, array $params = []) public function dispatch(string $name, array $params = [])
{ {
$context = ServerManager::getContext(); $context = di(ServerManager::class);
$context->task(static::$_absences[$name], $params); $context->task(static::$_absences[$name], $params);
} }
+2 -2
View File
@@ -77,7 +77,7 @@ class Zookeeper implements CustomProcess
if (empty($handler = $redis->get('crontab:' . $value))) { if (empty($handler = $redis->get('crontab:' . $value))) {
return; return;
} }
$server = ServerManager::getContext()->getServer(); $server = di(ServerManager::class)->getServer();
$server->sendMessage(swoole_unserialize($handler), $this->getWorker()); $server->sendMessage(swoole_unserialize($handler), $this->getWorker());
} catch (Throwable $exception) { } catch (Throwable $exception) {
logger()->addError($exception); logger()->addError($exception);
@@ -92,7 +92,7 @@ class Zookeeper implements CustomProcess
private function getWorker(): int private function getWorker(): int
{ {
if ($this->workerNum == 0) { if ($this->workerNum == 0) {
$server = ServerManager::getContext()->getServer(); $server = di(ServerManager::class)->getServer();
$this->workerNum = $server->setting['worker_num'] + ($server->setting['task_worker_num'] ?? 0); $this->workerNum = $server->setting['worker_num'] + ($server->setting['task_worker_num'] ?? 0);
} }
+1 -1
View File
@@ -446,7 +446,7 @@ class Kiri
*/ */
public static function async(string $class, array $params = []) public static function async(string $class, array $params = [])
{ {
$manager = ServerManager::getContext(); $manager = di(ServerManager::class);
$manager->task(new $class(...$params)); $manager->task(new $class(...$params));
} }
+1 -1
View File
@@ -92,7 +92,7 @@ class Request extends HttpService implements RequestInterface
*/ */
public function getConnectInfo(): array|null public function getConnectInfo(): array|null
{ {
$server = ServerManager::getContext()->getServer(); $server = di(ServerManager::class)->getServer();
return $server->getClientInfo($this->getClientId()); return $server->getClientInfo($this->getClientId());
} }
+2 -1
View File
@@ -82,13 +82,14 @@ class Response extends HttpService implements ResponseInterface
/** /**
* @return mixed * @return mixed
* @throws
*/ */
public function getClientInfo(): mixed public function getClientInfo(): mixed
{ {
if (!empty($this->_clientInfo)) { if (!empty($this->_clientInfo)) {
return $this->_clientInfo; return $this->_clientInfo;
} }
$server = ServerManager::getContext()->getServer(); $server = di(ServerManager::class)->getServer();
return $server->getClientInfo($this->clientId, $this->reactorId); return $server->getClientInfo($this->clientId, $this->reactorId);
} }
+7 -2
View File
@@ -36,7 +36,13 @@ class Server extends HttpService
]; ];
private ServerManager $manager; /**
* @Inject ServerManager
* @var null|ServerManager
*/
#[Inject(ServerManager::class)]
private ?ServerManager $manager = null;
private mixed $daemon = 0; private mixed $daemon = 0;
@@ -50,7 +56,6 @@ class Server extends HttpService
*/ */
public function init() public function init()
{ {
$this->manager = ServerManager::getContext();
} }
+2 -2
View File
@@ -49,7 +49,7 @@ class Shutdown extends Component
return; return;
} }
$server = ServerManager::getContext()->getServer(); $server = di(ServerManager::class)->getServer();
$master_pid = $server->setting['pid_file'] ?? PID_PATH; $master_pid = $server->setting['pid_file'] ?? PID_PATH;
if (file_exists($master_pid)) { if (file_exists($master_pid)) {
$this->close(file_get_contents($master_pid)); $this->close(file_get_contents($master_pid));
@@ -77,7 +77,7 @@ class Shutdown extends Component
*/ */
public function isRunning(): bool public function isRunning(): bool
{ {
$server = ServerManager::getContext()->getServer(); $server = di(ServerManager::class)->getServer();
$master_pid = $server->setting['pid_file'] ?? PID_PATH; $master_pid = $server->setting['pid_file'] ?? PID_PATH;
if (!file_exists($master_pid)) { if (!file_exists($master_pid)) {