Compare commits
55 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| d500bb78c8 | |||
| e2cac499b6 | |||
| bb557a9aa9 | |||
| 89cc6a04eb | |||
| 91e5c4d66a | |||
| 7e5301f02a | |||
| f76f4c02b6 | |||
| 2afe84694d | |||
| 8f80811da6 | |||
| e46ad90c46 | |||
| 4172349872 | |||
| 0849870234 | |||
| 9fcd47ecd1 | |||
| 3bfcaf8236 | |||
| 23110643e0 | |||
| d9980e9e1b | |||
| a580607ec8 | |||
| 1e5fe2412a | |||
| 2be7134ea6 | |||
| 125dd9e5b2 | |||
| 7fa1110a50 | |||
| b23c7eff83 | |||
| 2e86a73187 | |||
| 263b4d53eb | |||
| ddf25de2c5 | |||
| 54c34ccf8f | |||
| ad9f536c90 | |||
| c068c2db4f | |||
| c4bca4f88e | |||
| f6a00d89c1 | |||
| 7f93ab046b | |||
| 64514b069e | |||
| e669d80bb9 | |||
| b3777a64e3 | |||
| f858cecf1a | |||
| e0eda4b42c | |||
| b54d956a2d | |||
| bce6870538 | |||
| ab1a18a886 | |||
| 26bd43510e | |||
| cb73717c7f | |||
| 7360a8107b | |||
| cc98a527cd | |||
| 694bde36d9 | |||
| bbae787d4b | |||
| dc15054c86 | |||
| 787f035d62 | |||
| 09ec4eb9c3 | |||
| 60d08ccaca | |||
| 8c5eefb9a7 | |||
| 9cb6db5e26 | |||
| 82ff5c5d42 | |||
| a5378658ee | |||
| ac629ece15 | |||
| 42794168b5 |
@@ -0,0 +1,173 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Kiri\Server\Abstracts;
|
||||||
|
|
||||||
|
use Kiri\Di\Inject\Container;
|
||||||
|
use Kiri\Error\StdoutLogger;
|
||||||
|
use Kiri\Events\EventProvider;
|
||||||
|
use Kiri\Router\Router;
|
||||||
|
use Kiri\Server\Events\OnWorkerStart;
|
||||||
|
use Kiri\Server\ServerInterface;
|
||||||
|
use Psr\Log\LoggerInterface;
|
||||||
|
use Swoole\Event;
|
||||||
|
use Swoole\Process;
|
||||||
|
use Kiri\Server\Processes\AbstractProcess;
|
||||||
|
|
||||||
|
class HotReload extends AbstractProcess
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var mixed
|
||||||
|
*/
|
||||||
|
protected mixed $pipe;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var LoggerInterface|StdoutLogger
|
||||||
|
*/
|
||||||
|
#[Container(LoggerInterface::class)]
|
||||||
|
public StdoutLogger|LoggerInterface $logger;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var bool
|
||||||
|
*/
|
||||||
|
protected bool $enable_coroutine = false;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected array $watches = [];
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var bool
|
||||||
|
*/
|
||||||
|
protected bool $enable_queue = false;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var bool
|
||||||
|
*/
|
||||||
|
protected bool $reloading = false;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws \Exception
|
||||||
|
*/
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
di(EventProvider::class)->on(OnWorkerStart::class, [di(Router::class), 'scan_build_route']);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getName(): string
|
||||||
|
{
|
||||||
|
return 'hotReload';
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function onSigterm(): void
|
||||||
|
{
|
||||||
|
// TODO: Implement onSigterm() method.
|
||||||
|
$this->stop();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param ?Process $process
|
||||||
|
*/
|
||||||
|
public function process(Process|null $process): void
|
||||||
|
{
|
||||||
|
$this->pipe = inotify_init();
|
||||||
|
$this->addListen();
|
||||||
|
Event::add($this->pipe, function () use ($process) {
|
||||||
|
$read = inotify_read($this->pipe);
|
||||||
|
if (count($read) > 0) {
|
||||||
|
$this->reload();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
Event::cycle(function (): void {
|
||||||
|
if ($this->isStop()) {
|
||||||
|
Event::exit();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
Event::wait();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function reload(): void
|
||||||
|
{
|
||||||
|
if ($this->reloading) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
$this->reloading = true;
|
||||||
|
|
||||||
|
di(StdoutLogger::class)->println('reloading server[' . \config('id', 'system-service') . '], please waite.');
|
||||||
|
|
||||||
|
$this->clear();
|
||||||
|
di(ServerInterface::class)->reload();
|
||||||
|
$this->addListen();
|
||||||
|
|
||||||
|
$this->reloading = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
protected function addListen(): void
|
||||||
|
{
|
||||||
|
foreach (config('reload.listen') as $value) {
|
||||||
|
$this->readDirectory($value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
protected function clear(): void
|
||||||
|
{
|
||||||
|
$this->watches = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $directory
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function readFile(string $directory): void
|
||||||
|
{
|
||||||
|
if (str_ends_with($directory, '.php') === true) {
|
||||||
|
$wd = inotify_add_watch($this->pipe, $directory, IN_MODIFY | IN_MOVE | IN_CREATE | IN_DELETE);
|
||||||
|
|
||||||
|
$this->watches[] = $wd;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $directory
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function readDirectory(string $directory): void
|
||||||
|
{
|
||||||
|
foreach (glob($directory . '/*') as $data) {
|
||||||
|
if (is_dir($data)) {
|
||||||
|
$this->readDirectory($data);
|
||||||
|
} else {
|
||||||
|
$this->readFile($data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,7 +3,10 @@
|
|||||||
namespace Kiri\Server\Handler;
|
namespace Kiri\Server\Handler;
|
||||||
|
|
||||||
use Kiri;
|
use Kiri;
|
||||||
|
use Kiri\Abstracts\CoordinatorManager;
|
||||||
|
use Kiri\Coordinator;
|
||||||
use Kiri\Core\Help;
|
use Kiri\Core\Help;
|
||||||
|
use Kiri\Di\Inject\Container;
|
||||||
use Kiri\Events\EventDispatch;
|
use Kiri\Events\EventDispatch;
|
||||||
use Kiri\Server\Events\OnAfterWorkerStart;
|
use Kiri\Server\Events\OnAfterWorkerStart;
|
||||||
use Kiri\Server\Events\OnBeforeWorkerStart;
|
use Kiri\Server\Events\OnBeforeWorkerStart;
|
||||||
@@ -12,6 +15,7 @@ use Kiri\Server\Events\OnWorkerError;
|
|||||||
use Kiri\Server\Events\OnWorkerExit;
|
use Kiri\Server\Events\OnWorkerExit;
|
||||||
use Kiri\Server\Events\OnWorkerStart;
|
use Kiri\Server\Events\OnWorkerStart;
|
||||||
use Kiri\Server\Events\OnWorkerStop;
|
use Kiri\Server\Events\OnWorkerStop;
|
||||||
|
use Psr\Http\Message\RequestInterface;
|
||||||
use Swoole\Server;
|
use Swoole\Server;
|
||||||
use Swoole\Timer;
|
use Swoole\Timer;
|
||||||
use Throwable;
|
use Throwable;
|
||||||
@@ -25,6 +29,14 @@ use function config;
|
|||||||
class OnServerWorker extends Kiri\Server\Abstracts\Server
|
class OnServerWorker extends Kiri\Server\Abstracts\Server
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var EventDispatch
|
||||||
|
*/
|
||||||
|
#[Container(EventDispatch::class)]
|
||||||
|
public EventDispatch $dispatch;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
@@ -59,14 +71,15 @@ class OnServerWorker extends Kiri\Server\Abstracts\Server
|
|||||||
*/
|
*/
|
||||||
public function onWorkerStart(Server $server, int $workerId): void
|
public function onWorkerStart(Server $server, int $workerId): void
|
||||||
{
|
{
|
||||||
$dispatch = Kiri::getDi()->get(EventDispatch::class);
|
$this->dispatch->dispatch(new OnBeforeWorkerStart($server, $workerId));
|
||||||
$dispatch->dispatch(new OnBeforeWorkerStart($server, $workerId));
|
|
||||||
if ($workerId < $server->setting['worker_num']) {
|
if ($workerId < $server->setting['worker_num']) {
|
||||||
$dispatch->dispatch(new OnWorkerStart($server, $workerId));
|
CoordinatorManager::utility(Coordinator::WORKER_START)->waite();
|
||||||
|
|
||||||
|
$this->dispatch->dispatch(new OnWorkerStart($server, $workerId));
|
||||||
} else {
|
} else {
|
||||||
$dispatch->dispatch(new OnTaskerStart($server, $workerId));
|
$this->dispatch->dispatch(new OnTaskerStart($server, $workerId));
|
||||||
}
|
}
|
||||||
$dispatch->dispatch(new OnAfterWorkerStart($server, $workerId));
|
$this->dispatch->dispatch(new OnAfterWorkerStart($server, $workerId));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -117,9 +130,16 @@ class OnServerWorker extends Kiri\Server\Abstracts\Server
|
|||||||
event(new OnWorkerError($server, $worker_id, $worker_pid, $exit_code, $signal));
|
event(new OnWorkerError($server, $worker_id, $worker_pid, $exit_code, $signal));
|
||||||
|
|
||||||
debug_print_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT);
|
debug_print_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT);
|
||||||
$message = sprintf('Worker#%d::%d error stop. signal %d, exit_code %d, msg %s', $worker_id, $worker_pid, $signal, $exit_code, swoole_strerror(swoole_last_error(), $signal));
|
|
||||||
|
|
||||||
error($message);
|
/** @var RequestInterface $context */
|
||||||
|
$context = Kiri\Di\Context::get(RequestInterface::class);
|
||||||
|
if (is_null($context)) {
|
||||||
|
$message = sprintf('Worker#%d::%d error stop. signal %d, exit_code %d, msg %s', $worker_id, $worker_pid, $signal, $exit_code, swoole_strerror(swoole_last_error(), $signal));
|
||||||
|
} else {
|
||||||
|
$message = sprintf('Worker#%d::%d error stop. signal %d, exit_code %d, msg %s, method: %s, path: %s, query: %s', $worker_id, $worker_pid, $signal, $exit_code, swoole_strerror(swoole_last_error(), $signal),
|
||||||
|
$context->getMethod(), $context->getUri()->getPath(), $context->getUri()->getQuery());
|
||||||
|
}
|
||||||
|
error($message . PHP_EOL);
|
||||||
|
|
||||||
$this->system_mail($message);
|
$this->system_mail($message);
|
||||||
}
|
}
|
||||||
|
|||||||
+43
-9
@@ -6,16 +6,21 @@ namespace Kiri\Server;
|
|||||||
|
|
||||||
use Exception;
|
use Exception;
|
||||||
use Kiri;
|
use Kiri;
|
||||||
|
use Kiri\Server\Events\OnWorkerStart;
|
||||||
|
use Kiri\Events\EventProvider;
|
||||||
use Kiri\Events\EventDispatch;
|
use Kiri\Events\EventDispatch;
|
||||||
use Kiri\Router\Router;
|
use Kiri\Router\Router;
|
||||||
use Kiri\Server\Abstracts\AsyncServer;
|
use Kiri\Server\Abstracts\AsyncServer;
|
||||||
use Kiri\Server\Events\OnShutdown;
|
use Kiri\Server\Events\OnShutdown;
|
||||||
|
use Psr\Container\ContainerInterface;
|
||||||
use Symfony\Component\Console\Command\Command;
|
use Symfony\Component\Console\Command\Command;
|
||||||
use Symfony\Component\Console\Input\InputArgument;
|
use Symfony\Component\Console\Input\InputArgument;
|
||||||
use Symfony\Component\Console\Input\InputInterface;
|
use Symfony\Component\Console\Input\InputInterface;
|
||||||
use Symfony\Component\Console\Input\InputOption;
|
use Symfony\Component\Console\Input\InputOption;
|
||||||
use Symfony\Component\Console\Output\OutputInterface;
|
use Symfony\Component\Console\Output\OutputInterface;
|
||||||
use function config;
|
use function config;
|
||||||
|
use Kiri\Server\Abstracts\HotReload;
|
||||||
|
use Kiri\Di\Inject\Container;
|
||||||
|
|
||||||
defined('ROUTER_TYPE_HTTP') or define('ROUTER_TYPE_HTTP', 'http');
|
defined('ROUTER_TYPE_HTTP') or define('ROUTER_TYPE_HTTP', 'http');
|
||||||
defined('PID_PATH') or define('PID_PATH', APP_PATH . 'storage/server.pid');
|
defined('PID_PATH') or define('PID_PATH', APP_PATH . 'storage/server.pid');
|
||||||
@@ -31,6 +36,34 @@ class ServerCommand extends Command
|
|||||||
public State $state;
|
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
|
* @param string|null $name
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
@@ -92,12 +125,11 @@ class ServerCommand extends Command
|
|||||||
protected function stop(): int
|
protected function stop(): int
|
||||||
{
|
{
|
||||||
$configs = config('server', []);
|
$configs = config('server', []);
|
||||||
$instances = Kiri::getDi()->get(AsyncServer::class)->sortService($configs['ports'] ?? []);
|
$instances = $this->asyncServer->sortService($configs['ports'] ?? []);
|
||||||
foreach ($instances as $config) {
|
foreach ($instances as $config) {
|
||||||
$this->state->exit($config->port);
|
$this->state->exit($config->port);
|
||||||
}
|
}
|
||||||
$dispatch = Kiri::getDi()->get(EventDispatch::class);
|
$this->eventDispatch->dispatch(new OnShutdown());
|
||||||
$dispatch->dispatch(new OnShutdown());
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -109,12 +141,14 @@ class ServerCommand extends Command
|
|||||||
*/
|
*/
|
||||||
protected function start(InputInterface $input): int
|
protected function start(InputInterface $input): int
|
||||||
{
|
{
|
||||||
$manager = Kiri::getDi()->get(AsyncServer::class);
|
$this->asyncServer->addProcess(config('processes', []));
|
||||||
$router = Kiri::getDi()->get(Router::class);
|
if (\config('reload.hot', false) === true) {
|
||||||
$router->scan_build_route();
|
$this->asyncServer->addProcess([HotReload::class]);
|
||||||
$manager->addProcess(config('processes', []));
|
} else {
|
||||||
$manager->initCoreServers(config('server', []), (int)$input->getOption('daemon'));
|
di(Router::class)->scan_build_route();
|
||||||
$manager->start();
|
}
|
||||||
|
$this->asyncServer->initCoreServers(config('server', []), (int)$input->getOption('daemon'));
|
||||||
|
$this->asyncServer->start();
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user