This commit is contained in:
2021-08-17 16:38:58 +08:00
parent 36856814b0
commit 539ba488e9
2 changed files with 33 additions and 13 deletions
+1 -1
View File
@@ -65,7 +65,7 @@ class Server extends HttpService
*/ */
public function start(): string public function start(): string
{ {
$this->manager->initBaseServer(Config::get('server', [], true)); $this->manager->initBaseServer(Config::get('server', [], true), $this->daemon);
$rpcService = Config::get('rpc', []); $rpcService = Config::get('rpc', []);
if (!empty($rpcService)) { if (!empty($rpcService)) {
+25 -5
View File
@@ -95,11 +95,11 @@ class ServerManager
* @throws NotFindClassException * @throws NotFindClassException
* @throws ReflectionException * @throws ReflectionException
*/ */
public function initBaseServer($configs): void public function initBaseServer($configs, int $daemon = 0): void
{ {
$context = ServerManager::getContext(); $context = ServerManager::getContext();
foreach ($this->sortService($configs['ports']) as $config) { foreach ($this->sortService($configs['ports']) as $config) {
$this->startListenerHandler($context, $config); $this->startListenerHandler($context, $config, $daemon);
} }
$this->bindCallback($this->server, [Constant::PIPE_MESSAGE => [OnPipeMessage::class, 'onPipeMessage']]); $this->bindCallback($this->server, [Constant::PIPE_MESSAGE => [OnPipeMessage::class, 'onPipeMessage']]);
$this->bindCallback($this->server, $this->getSystemEvents($configs)); $this->bindCallback($this->server, $this->getSystemEvents($configs));
@@ -202,14 +202,35 @@ class ServerManager
/** /**
* @param ServerManager $context * @param ServerManager $context
* @param array $config * @param array $config
* @param int $daemon
* @throws ConfigException
* @throws NotFindClassException * @throws NotFindClassException
* @throws ReflectionException * @throws ReflectionException
* @throws Exception * @throws Exception
*/ */
private function startListenerHandler(ServerManager $context, array $config) private function startListenerHandler(ServerManager $context, array $config, int $daemon = 0)
{ {
if (!$this->server) { if (!$this->server) {
$config = $this->mergeConfig($config, $daemon);
}
$context->addListener(
$config['type'], $config['host'], $config['port'], $config['mode'],
$config);
}
/**
* @param $config
* @param $daemon
* @return array
* @throws Exception
*/
private function mergeConfig($config, $daemon): array
{
$config['settings'] = $config['settings'] ?? []; $config['settings'] = $config['settings'] ?? [];
if (!isset($config['settings']['daemonize']) || !$config['settings']['daemonize'] != $daemon) {
$config['settings']['daemonize'] = $daemon;
}
if (!isset($config['settings']['log_file'])) { if (!isset($config['settings']['log_file'])) {
$config['settings']['log_file'] = storage('system.log'); $config['settings']['log_file'] = storage('system.log');
} }
@@ -217,8 +238,7 @@ class ServerManager
$config['settings']['pid_file'] = storage('swoole.pid'); $config['settings']['pid_file'] = storage('swoole.pid');
} }
$config['events'] = $config['events'] ?? []; $config['events'] = $config['events'] ?? [];
} return $config;
$context->addListener($config['type'], $config['host'], $config['port'], $config['mode'], $config);
} }