This commit is contained in:
2023-04-16 02:15:51 +08:00
parent 5ec3fed09d
commit a6d2d97fb5
8 changed files with 37 additions and 55 deletions
+6 -6
View File
@@ -14,13 +14,13 @@ use Psr\Log\LoggerInterface;
use ReflectionException; use ReflectionException;
use Kiri\Server\Config as SConfig; use Kiri\Server\Config as SConfig;
use Kiri\Di\LocalService; use Kiri\Di\LocalService;
use Swoole\Runtime;
use Swoole\Server; use Swoole\Server;
use Kiri\Server\ServerInterface; use Kiri\Server\ServerInterface;
use Kiri\Server\Constant; use Kiri\Server\Constant;
use Kiri\Events\EventDispatch; use Kiri\Events\EventDispatch;
use Kiri\Exception\NotFindClassException; use Kiri\Exception\NotFindClassException;
use Kiri\Server\Events\OnServerBeforeStart; use Kiri\Server\Events\OnServerBeforeStart;
use Kiri\Di\Inject\Container;
/** /**
* *
@@ -44,11 +44,11 @@ class AsyncServer implements ServerInterface
* @param LoggerInterface $logger * @param LoggerInterface $logger
* @param ProcessManager $processManager * @param ProcessManager $processManager
*/ */
public function __construct(public Config $config, public function __construct(#[Container(Config::class)] public Config $config,
public ContainerInterface $container, #[Container(ContainerInterface::class)] public ContainerInterface $container,
public EventDispatch $dispatch, #[Container(EventDispatch::class)] public EventDispatch $dispatch,
public LoggerInterface $logger, #[Container(LoggerInterface::class)] public LoggerInterface $logger,
public ProcessManager $processManager) #[Container(ProcessManager::class)] public ProcessManager $processManager)
{ {
} }
+3 -3
View File
@@ -12,10 +12,10 @@ use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface; use Psr\Container\NotFoundExceptionInterface;
use Swoole\Coroutine; use Swoole\Coroutine;
use Swoole\Process; use Swoole\Process;
use Kiri\Annotation\Inject;
use Psr\Container\ContainerInterface; use Psr\Container\ContainerInterface;
use Kiri\Events\EventProvider; use Kiri\Events\EventProvider;
use Kiri\Server\ServerInterface; use Kiri\Server\ServerInterface;
use Kiri\Di\Inject\Container;
use Kiri\Server\Events\OnServerBeforeStart; use Kiri\Server\Events\OnServerBeforeStart;
class ProcessManager extends Component class ProcessManager extends Component
@@ -29,11 +29,11 @@ class ProcessManager extends Component
/** /**
* @var ContainerInterface * @var ContainerInterface
*/ */
#[Inject(ContainerInterface::class)] #[Container(ContainerInterface::class)]
public ContainerInterface $container; public ContainerInterface $container;
#[Inject(EventProvider::class)] #[Container(EventProvider::class)]
public EventProvider $provider; public EventProvider $provider;
/** /**
+2 -4
View File
@@ -4,12 +4,10 @@
namespace Kiri\Server\Abstracts; namespace Kiri\Server\Abstracts;
use Kiri\Annotation\Inject;
use Exception; use Exception;
use Kiri\Abstracts\Config;
use Kiri\Exception\ConfigException;
use Kiri; use Kiri;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Kiri\Di\Inject\Container;
/** /**
@@ -23,7 +21,7 @@ abstract class Server
/** /**
* @var LoggerInterface * @var LoggerInterface
*/ */
#[Inject(LoggerInterface::class)] #[Container(LoggerInterface::class)]
public LoggerInterface $logger; public LoggerInterface $logger;
+6 -5
View File
@@ -4,6 +4,7 @@ namespace Kiri\Server;
use Exception; use Exception;
use Kiri\Abstracts\Config; use Kiri\Abstracts\Config;
use Kiri\Di\Inject\Container;
use Psr\Container\ContainerInterface; use Psr\Container\ContainerInterface;
use Kiri\Events\EventDispatch; use Kiri\Events\EventDispatch;
use Kiri\Exception\ConfigException; use Kiri\Exception\ConfigException;
@@ -50,11 +51,11 @@ class CoroutineServer implements ServerInterface
* @param LoggerInterface $logger * @param LoggerInterface $logger
* @param ProcessManager $processManager * @param ProcessManager $processManager
*/ */
public function __construct(public Config $config, public function __construct(#[Container(Config::class)] public Config $config,
public ContainerInterface $container, #[Container(ContainerInterface::class)] public ContainerInterface $container,
public EventDispatch $dispatch, #[Container(EventDispatch::class)] public EventDispatch $dispatch,
public LoggerInterface $logger, #[Container(LoggerInterface::class)] public LoggerInterface $logger,
public ProcessManager $processManager) #[Container(ProcessManager::class)] public ProcessManager $processManager)
{ {
} }
-2
View File
@@ -3,8 +3,6 @@
namespace Kiri\Server\Handler; namespace Kiri\Server\Handler;
use Exception; use Exception;
use Kiri\Annotation\Inject;
use Kiri\Events\EventDispatch;
use Kiri\Server\Abstracts\Server; use Kiri\Server\Abstracts\Server;
use Kiri\Server\Contract\OnPipeMessageInterface; use Kiri\Server\Contract\OnPipeMessageInterface;
-1
View File
@@ -3,7 +3,6 @@
namespace Kiri\Server\Handler; namespace Kiri\Server\Handler;
use Kiri; use Kiri;
use Kiri\Annotation\Inject;
use Kiri\Events\EventDispatch; use Kiri\Events\EventDispatch;
use Psr\Container\ContainerExceptionInterface; use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface; use Psr\Container\NotFoundExceptionInterface;
+1 -6
View File
@@ -7,8 +7,6 @@ use Kiri;
use Kiri\Abstracts\Config; use Kiri\Abstracts\Config;
use Kiri\Core\Help; use Kiri\Core\Help;
use Kiri\Events\EventDispatch; use Kiri\Events\EventDispatch;
use Kiri\Message\Handler\Router;
use Kiri\Server\Constant;
use Kiri\Server\Events\OnAfterWorkerStart; use Kiri\Server\Events\OnAfterWorkerStart;
use Kiri\Server\Events\OnBeforeWorkerStart; use Kiri\Server\Events\OnBeforeWorkerStart;
use Kiri\Server\Events\OnTaskerStart as OnTaskStart; use Kiri\Server\Events\OnTaskerStart as OnTaskStart;
@@ -19,12 +17,9 @@ use Kiri\Server\Events\OnWorkerStop;
use Psr\Container\ContainerExceptionInterface; use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface; use Psr\Container\NotFoundExceptionInterface;
use ReflectionException; use ReflectionException;
use Swoole\Runtime;
use Swoole\Server; use Swoole\Server;
use Kiri\Server\Abstracts\StatusEnum; use Kiri\Server\Abstracts\StatusEnum;
use Kiri\Server\WorkerStatus; use Kiri\Server\WorkerStatus;
use Swoole\Timer;
use Kiri\Annotation\Inject;
/** /**
@@ -134,7 +129,7 @@ class OnServerWorker extends \Kiri\Server\Abstracts\Server
Help::sendEmail($email, 'Service Error', $messageContent); Help::sendEmail($email, 'Service Error', $messageContent);
} }
} catch (\Throwable $e) { } catch (\Throwable $e) {
error($e, 'email'); error($e, ['email']);
} }
} }
+18 -27
View File
@@ -20,6 +20,7 @@ use Kiri\Server\Events\OnWorkerStop;
use Swoole\Coroutine; use Swoole\Coroutine;
use Kiri\Server\Abstracts\ProcessManager; use Kiri\Server\Abstracts\ProcessManager;
use Kiri\Server\Abstracts\AsyncServer; use Kiri\Server\Abstracts\AsyncServer;
use Kiri\Di\Inject\Container;
defined('PID_PATH') or define('PID_PATH', APP_PATH . 'storage/server.pid'); defined('PID_PATH') or define('PID_PATH', APP_PATH . 'storage/server.pid');
@@ -37,27 +38,23 @@ class Server
public AsyncServer|CoroutineServer $manager; public AsyncServer|CoroutineServer $manager;
#[Container(State::class)]
public State $state;
/** #[Container(ContainerInterface::class)]
* @param State $state public ContainerInterface $container;
* @param ContainerInterface $container
* @param ProcessManager $processManager #[Container(ProcessManager::class)]
* @param EventDispatch $dispatch public ProcessManager $processManager;
* @param EventProvider $provider
* @param Router $router #[Container(EventDispatch::class)]
* @param array $config public EventDispatch $dispatch;
* @throws Exception
*/ #[Container(EventProvider::class)]
public function __construct(public State $state, public EventProvider $provider;
public ContainerInterface $container,
public ProcessManager $processManager, #[Container(Router::class)]
public EventDispatch $dispatch, public Router $router;
public EventProvider $provider,
public Router $router,
array $config = [])
{
parent::__construct($config);
}
/** /**
@@ -67,7 +64,7 @@ class Server
*/ */
public function init(): void public function init(): void
{ {
$this->manager = $this->container->get(Config::get('server.type',AsyncServer::class)); $this->manager = $this->container->get(Config::get('server.type', AsyncServer::class));
$enable_coroutine = Config::get('server.settings.enable_coroutine', false); $enable_coroutine = Config::get('server.settings.enable_coroutine', false);
if (!$enable_coroutine) { if (!$enable_coroutine) {
@@ -160,14 +157,8 @@ class Server
public function onHotReload(): void public function onHotReload(): void
{ {
$this->onWorkerListener(); $this->onWorkerListener();
$reload = Config::get('reload.hot', false);
if ($reload !== false) {
$this->provider->on(OnWorkerStart::class, [$this->router, 'scan_build_route']);
$this->manager->addProcess(Kiri\Reload\Inotify::class);
} else {
$this->router->scan_build_route(); $this->router->scan_build_route();
} }
}
/** /**