This commit is contained in:
xl
2023-07-06 16:00:02 +08:00
parent 136b0dc1a6
commit 6aa764fb86
2 changed files with 310 additions and 327 deletions
+38 -71
View File
@@ -5,97 +5,65 @@ namespace Kiri\Server;
use Exception; use Exception;
use Kiri\Di\Context; use Kiri\Di\Context;
use Kiri\Server\Abstracts\BaseProcess;
use Swoole\Coroutine;
use Swoole\Event; use Swoole\Event;
use Swoole\Process; use Swoole\Process;
use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
class HotReload extends Command class HotReload extends BaseProcess
{ {
public string $name = 'hot:reload';
private array $md5Map = [];
/**
* @var Process
*/
private mixed $process;
/** /**
* @var array|mixed * @var array|mixed
*/ */
private array $watchFiles = []; private array $watchFiles = [];
/** /**
* @return void * @var array|string[]
*/ */
protected function configure()
{
$this->setName('hot:load');
parent::configure(); // TODO: Change the autogenerated stub
}
private array $dirs = [APP_PATH . 'app/', APP_PATH . 'config/', APP_PATH . 'routes/']; private array $dirs = [APP_PATH . 'app/', APP_PATH . 'config/', APP_PATH . 'routes/'];
/** /**
* @param InputInterface $input * @return string
* @param OutputInterface $output */
* @return int public function getName(): string
{
return 'hot.load'; // TODO: Change the autogenerated stub
}
/**
* @return $this
*/
public function onSigterm(): static
{
// TODO: Implement onSigterm() method.
if (Context::inCoroutine()) {
Coroutine::create(fn() => $this->onShutdown(Coroutine::waitSignal(SIGTERM | SIGINT)));
} else {
Process::signal(SIGTERM | SIGINT, fn($data) => $this->onShutdown($data));
}
return $this;
}
/**
* @param Process|null $process
* @return void
* @throws Exception * @throws Exception
*/ */
protected function execute(InputInterface $input, OutputInterface $output): int public function process(?Process $process): void
{ {
$this->startProcess(); // TODO: Implement process() method.
Process::signal(SIGINT, fn() => $this->exit()); if (extension_loaded('inotify')) {
Process::signal(SIGTERM, fn() => $this->exit()); $this->onInotifyReload();
sleep(3); } else {
// if (extension_loaded('inotify')) {
// $this->onInotifyReload();
// } else {
$this->onCrontabReload(); $this->onCrontabReload();
// }
return 1;
} }
/**
* @return void
* @throws Exception
*/
public function exit(): void
{
$this->stopProcess();
}
/**
* @return void
*/
private function startProcess(): void
{
$this->process = proc_open([PHP_BINARY, APP_PATH . 'kiri.php', 'sw:server', 'restart'], [], $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;
} }
@@ -126,7 +94,7 @@ class HotReload extends Command
Event::add($init, fn() => $this->check($init)); Event::add($init, fn() => $this->check($init));
Event::cycle(function () use ($init) { Event::cycle(function () use ($init) {
$pid = (int)file_get_contents(storage('.swoole.pid')); $pid = (int)file_get_contents(storage('.swoole.pid'));
if ($pid <= 0 || !Process::kill($pid, 0)) { if ($pid <= 0 || !Process::kill($pid, 0) || $this->isStop()) {
Event::del($init); Event::del($init);
} }
}, true); }, true);
@@ -163,7 +131,7 @@ class HotReload extends Command
} }
$pid = (int)file_get_contents(storage('.swoole.pid')); $pid = (int)file_get_contents(storage('.swoole.pid'));
if ($pid <= 0 || !Process::kill($pid, 0)) { if ($pid <= 0 || !Process::kill($pid, 0) || $this->isStop()) {
return; return;
} }
@@ -307,8 +275,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;
$this->stopProcess(); di(ServerInterface::class)->reload(false);
$this->startProcess();
} }
+21 -5
View File
@@ -78,15 +78,30 @@ class Server
on(OnWorkerStart::class, [$this, 'setWorkerName']); on(OnWorkerStart::class, [$this, 'setWorkerName']);
on(OnTaskerStart::class, [$this, 'setTaskerName']); on(OnTaskerStart::class, [$this, 'setTaskerName']);
$manager = Kiri::getDi()->get(Router::class); if (\config('reload.hot') === false) {
$manager->scan_build_route(); $this->hotLoad();
} else {
on(OnWorkerStart::class, [$this, 'hotLoad']);
$this->addProcess(HotReload::class);
}
$manager = $this->manager(); $manager = $this->manager();
$manager->initCoreServers(\config('server', [], true), $this->daemon); $manager->initCoreServers(\config('server', []), $this->daemon);
$manager->start(); $manager->start();
} }
/**
* @return void
* @throws ReflectionException
*/
public function hotLoad(): void
{
$manager = Kiri::getDi()->get(Router::class);
$manager->scan_build_route();
}
/** /**
* @param OnWorkerStart $onWorkerStart * @param OnWorkerStart $onWorkerStart
*/ */
@@ -125,10 +140,11 @@ class Server
*/ */
public function shutdown(): void public function shutdown(): void
{ {
$configs = \config('server', [], true); $configs = \config('server', []);
$state = Kiri::getDi()->get(State::class); $state = Kiri::getDi()->get(State::class);
foreach ($this->manager()->sortService($configs['ports'] ?? []) as $config) { $instances = $this->manager()->sortService($configs['ports'] ?? []);
foreach ($instances as $config) {
$state->exit($config->port); $state->exit($config->port);
} }