This commit is contained in:
2023-04-19 13:15:21 +08:00
parent c739912494
commit 3b0d556700
5 changed files with 30 additions and 19 deletions
+5 -5
View File
@@ -142,8 +142,6 @@ class AsyncServer implements ServerInterface
/** /**
* @param SConfig $config * @param SConfig $config
* @return void * @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws Exception * @throws Exception
*/ */
public function addListener(SConfig $config): void public function addListener(SConfig $config): void
@@ -217,12 +215,14 @@ class AsyncServer implements ServerInterface
/** /**
* @return void * @return void
* @throws ContainerExceptionInterface * @throws
* @throws NotFoundExceptionInterface
* @throws ReflectionException
*/ */
public function start(): void public function start(): void
{ {
$pid = (int)file_get_contents(storage('.swoole.pid'));
if (posix_kill($pid, 0)) {
posix_kill($pid, SIGTERM);
}
$processManager = Kiri::getDi()->get(EventDispatch::class); $processManager = Kiri::getDi()->get(EventDispatch::class);
$processManager->dispatch(new OnServerBeforeStart()); $processManager->dispatch(new OnServerBeforeStart());
$this->server->start(); $this->server->start();
+20 -10
View File
@@ -49,10 +49,10 @@ class HotReload extends Command
*/ */
protected function execute(InputInterface $input, OutputInterface $output) protected function execute(InputInterface $input, OutputInterface $output)
{ {
$this->startProcess();
pcntl_signal(SIGINT | SIGQUIT | SIGTERM, function () { pcntl_signal(SIGINT | SIGQUIT | SIGTERM, function () {
Process::wait(); $this->stopProcess();
}); });
$this->startProcess();
if (extension_loaded('inotify')) { if (extension_loaded('inotify')) {
$this->onInotifyReload(); $this->onInotifyReload();
} else { } else {
@@ -70,6 +70,23 @@ class HotReload extends Command
$this->process = proc_open([PHP_BINARY, APP_PATH . 'kiri.php', 'sw:server', 'start'], [], $pipes); $this->process = proc_open([PHP_BINARY, APP_PATH . 'kiri.php', 'sw:server', 'start'], [], $pipes);
} }
/**
* @return void
* @throws Exception
*/
private function stopProcess(): void
{
if (!is_resource($this->process)) {
return;
}
$pid = (int)file_get_contents(storage('.swoole.pid'));
if (posix_kill($pid, 0)) {
posix_kill($pid, SIGTERM);
}
proc_close($this->process);
$this->process = null;
}
/** /**
* @return void * @return void
@@ -268,14 +285,7 @@ class HotReload extends Command
public function trigger_reload(): void public function trigger_reload(): void
{ {
echo 'tigger server Reload' . PHP_EOL; echo 'tigger server Reload' . PHP_EOL;
if (is_resource($this->process)) { $this->stopProcess();
$pid = (int)file_get_contents(storage('.swoole.pid'));
if (posix_kill($pid, 0)) {
posix_kill($pid, SIGTERM);
}
proc_close($this->process);
}
$this->process = null;
$this->startProcess(); $this->startProcess();
} }
-2
View File
@@ -135,7 +135,6 @@ class Server
/** /**
* @param OnTaskerStart $onWorkerStart * @param OnTaskerStart $onWorkerStart
* @throws ConfigException
*/ */
public function setTaskerName(OnTaskerStart $onWorkerStart): void public function setTaskerName(OnTaskerStart $onWorkerStart): void
{ {
@@ -151,7 +150,6 @@ class Server
/** /**
* @return void * @return void
* @throws ConfigException
* @throws Exception * @throws Exception
*/ */
public function onHotReload(): void public function onHotReload(): void
+3 -1
View File
@@ -5,6 +5,7 @@ namespace Kiri\Server;
use Exception; use Exception;
use Kiri\Abstracts\Component; use Kiri\Abstracts\Component;
use Kiri\Abstracts\Config; use Kiri\Abstracts\Config;
use Kiri\Abstracts\Logger;
use Kiri\Server\Abstracts\TraitServer; use Kiri\Server\Abstracts\TraitServer;
use Swoole\Process; use Swoole\Process;
@@ -22,6 +23,7 @@ class State extends Component
*/ */
public function init(): void public function init(): void
{ {
Logger::emergency();
$this->servers = Config::get('server.ports'); $this->servers = Config::get('server.ports');
} }
@@ -46,7 +48,7 @@ class State extends Component
* @param $port * @param $port
* @throws Exception * @throws Exception
*/ */
public function exit($port) public function exit($port): void
{ {
if (!($pid = checkPortIsAlready($port))) { if (!($pid = checkPortIsAlready($port))) {
return; return;
+2 -1
View File
@@ -16,7 +16,8 @@
"psr/http-message": "^1.0", "psr/http-message": "^1.0",
"psr/event-dispatcher": "^1.0", "psr/event-dispatcher": "^1.0",
"ext-inotify": "*", "ext-inotify": "*",
"ext-posix": "*" "ext-posix": "*",
"ext-pcntl": "*"
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {