This commit is contained in:
xl
2024-11-18 11:24:08 +08:00
parent ac629ece15
commit a5378658ee
3 changed files with 175 additions and 9 deletions
+129
View File
@@ -0,0 +1,129 @@
<?php
namespace Kiri\Server\Abstracts;
use Kiri\Server\ServerInterface;
use Swoole\Process;
use Kiri\Server\Processes\AbstractProcess;
use function GuzzleHttp\Psr7\uri_for;
class HotReload extends AbstractProcess
{
protected mixed $pipe;
/**
* @var bool
*/
protected bool $enable_coroutine = false;
/**
* @var bool
*/
protected bool $enable_queue = false;
protected bool $reloading = false;
/**
* @return string
*/
public function getName(): string
{
return 'custom.process';
}
/**
* @return void
*/
public function onSigterm(): void
{
// TODO: Implement onSigterm() method.
fclose(inotify_init());
}
/**
* @param ?Process $process
*/
public function process(Process|null $process): void
{
$this->pipe = inotify_init();
$this->readDirectory(APP_PATH . '/app/*');
$this->readDirectory(APP_PATH . '/routes/*');
while (!$this->isStop()) {
$read = inotify_read($this->pipe);
foreach ($read as $item) {
$this->reWatch($item['name']);
}
$this->reload();
}
}
/**
* @return void
*/
public function reload(): void
{
$this->reloading = true;
di(ServerInterface::class)->reload();
$this->reloading = false;
}
/**
* @param string $directory
* @return void
*/
public function readFile(string $directory): void
{
if (str_ends_with($directory, '.php') === true) {
inotify_add_watch($this->pipe, $directory, IN_MODIFY | IN_MOVE | IN_CREATE | IN_DELETE);
}
}
/**
* @param string $directory
* @return void
*/
public function reWatch(string $directory): void
{
if (str_ends_with($directory, '.php') === true) {
inotify_rm_watch($this->pipe, $directory);
inotify_add_watch($this->pipe, $directory, IN_MODIFY | IN_MOVE | IN_CREATE | IN_DELETE);
}
}
/**
* @param string $directory
* @return void
*/
public function readDirectory(string $directory): void
{
foreach (glob($directory . '/*') as $data) {
if (str_ends_with($data, '.php') === false) {
continue;
}
if (is_dir($data)) {
$this->readFile($data . DIRECTORY_SEPARATOR . '*');
} else {
$this->readFile($data);
}
}
}
}
+4
View File
@@ -3,6 +3,8 @@
namespace Kiri\Server\Handler;
use Kiri;
use Kiri\Abstracts\CoordinatorManager;
use Kiri\Coordinator;
use Kiri\Core\Help;
use Kiri\Events\EventDispatch;
use Kiri\Server\Events\OnAfterWorkerStart;
@@ -63,6 +65,8 @@ class OnServerWorker extends Kiri\Server\Abstracts\Server
$dispatch = Kiri::getDi()->get(EventDispatch::class);
$dispatch->dispatch(new OnBeforeWorkerStart($server, $workerId));
if ($workerId < $server->setting['worker_num']) {
CoordinatorManager::utility(Coordinator::WORKER_START)->waite();
$dispatch->dispatch(new OnWorkerStart($server, $workerId));
} else {
$dispatch->dispatch(new OnTaskerStart($server, $workerId));
+42 -9
View File
@@ -6,16 +6,21 @@ namespace Kiri\Server;
use Exception;
use Kiri;
use Kiri\Server\Events\OnWorkerStart;
use Kiri\Events\EventProvider;
use Kiri\Events\EventDispatch;
use Kiri\Router\Router;
use Kiri\Server\Abstracts\AsyncServer;
use Kiri\Server\Events\OnShutdown;
use Psr\Container\ContainerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use function config;
use Kiri\Server\Abstracts\HotReload;
use Kiri\Di\Inject\Container;
defined('ROUTER_TYPE_HTTP') or define('ROUTER_TYPE_HTTP', 'http');
defined('PID_PATH') or define('PID_PATH', APP_PATH . 'storage/server.pid');
@@ -31,6 +36,34 @@ class ServerCommand extends Command
public State $state;
/**
* @var ContainerInterface
*/
#[Container(ContainerInterface::class)]
public ContainerInterface $container;
/**
* @var AsyncServer
*/
#[Container(AsyncServer::class)]
public AsyncServer $asyncServer;
/**
* @var EventDispatch
*/
#[Container(EventDispatch::class)]
public EventDispatch $eventDispatch;
/**
* @var EventProvider
*/
#[Container(EventProvider::class)]
public EventProvider $eventProvider;
/**
* @param string|null $name
* @throws Exception
@@ -92,12 +125,11 @@ class ServerCommand extends Command
protected function stop(): int
{
$configs = config('server', []);
$instances = Kiri::getDi()->get(AsyncServer::class)->sortService($configs['ports'] ?? []);
$instances = $this->asyncServer->sortService($configs['ports'] ?? []);
foreach ($instances as $config) {
$this->state->exit($config->port);
}
$dispatch = Kiri::getDi()->get(EventDispatch::class);
$dispatch->dispatch(new OnShutdown());
$this->eventDispatch->dispatch(new OnShutdown());
return 1;
}
@@ -109,12 +141,13 @@ class ServerCommand extends Command
*/
protected function start(InputInterface $input): int
{
$manager = Kiri::getDi()->get(AsyncServer::class);
$router = Kiri::getDi()->get(Router::class);
$router->scan_build_route();
$manager->addProcess(config('processes', []));
$manager->initCoreServers(config('server', []), (int)$input->getOption('daemon'));
$manager->start();
$this->eventProvider->on(OnWorkerStart::class, [di(Router::class), 'scan_build_route']);
$this->asyncServer->addProcess(config('processes', []));
if (\config('reload.hot', false) === true) {
$this->asyncServer->addProcess([HotReload::class]);
}
$this->asyncServer->initCoreServers(config('server', []), (int)$input->getOption('daemon'));
$this->asyncServer->start();
return 1;
}