qqq
This commit is contained in:
+38
-71
@@ -5,97 +5,65 @@ namespace Kiri\Server;
|
||||
|
||||
use Exception;
|
||||
use Kiri\Di\Context;
|
||||
use Kiri\Server\Abstracts\BaseProcess;
|
||||
use Swoole\Coroutine;
|
||||
use Swoole\Event;
|
||||
use Swoole\Process;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
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
|
||||
*/
|
||||
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/'];
|
||||
|
||||
|
||||
/**
|
||||
* @param InputInterface $input
|
||||
* @param OutputInterface $output
|
||||
* @return int
|
||||
* @return string
|
||||
*/
|
||||
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
|
||||
*/
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
public function process(?Process $process): void
|
||||
{
|
||||
$this->startProcess();
|
||||
Process::signal(SIGINT, fn() => $this->exit());
|
||||
Process::signal(SIGTERM, fn() => $this->exit());
|
||||
sleep(3);
|
||||
// if (extension_loaded('inotify')) {
|
||||
// $this->onInotifyReload();
|
||||
// } else {
|
||||
// TODO: Implement process() method.
|
||||
if (extension_loaded('inotify')) {
|
||||
$this->onInotifyReload();
|
||||
} else {
|
||||
$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::cycle(function () use ($init) {
|
||||
$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);
|
||||
}
|
||||
}, true);
|
||||
@@ -163,7 +131,7 @@ class HotReload extends Command
|
||||
}
|
||||
|
||||
$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;
|
||||
}
|
||||
|
||||
@@ -307,8 +275,7 @@ class HotReload extends Command
|
||||
public function trigger_reload(): void
|
||||
{
|
||||
echo 'tigger server Reload' . PHP_EOL;
|
||||
$this->stopProcess();
|
||||
$this->startProcess();
|
||||
di(ServerInterface::class)->reload(false);
|
||||
}
|
||||
|
||||
|
||||
|
||||
+21
-5
@@ -78,15 +78,30 @@ class Server
|
||||
on(OnWorkerStart::class, [$this, 'setWorkerName']);
|
||||
on(OnTaskerStart::class, [$this, 'setTaskerName']);
|
||||
|
||||
$manager = Kiri::getDi()->get(Router::class);
|
||||
$manager->scan_build_route();
|
||||
if (\config('reload.hot') === false) {
|
||||
$this->hotLoad();
|
||||
} else {
|
||||
on(OnWorkerStart::class, [$this, 'hotLoad']);
|
||||
$this->addProcess(HotReload::class);
|
||||
}
|
||||
|
||||
$manager = $this->manager();
|
||||
$manager->initCoreServers(\config('server', [], true), $this->daemon);
|
||||
$manager->initCoreServers(\config('server', []), $this->daemon);
|
||||
$manager->start();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return void
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
public function hotLoad(): void
|
||||
{
|
||||
$manager = Kiri::getDi()->get(Router::class);
|
||||
$manager->scan_build_route();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param OnWorkerStart $onWorkerStart
|
||||
*/
|
||||
@@ -125,10 +140,11 @@ class Server
|
||||
*/
|
||||
public function shutdown(): void
|
||||
{
|
||||
$configs = \config('server', [], true);
|
||||
$configs = \config('server', []);
|
||||
|
||||
$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);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user