Compare commits
99 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| c2fde2e6d7 | |||
| 5ddd0bc6d7 | |||
| d22db391f3 | |||
| bcd78aba56 | |||
| c899f51f35 | |||
| 1419e96c89 | |||
| 224b52db49 | |||
| 248f4d7100 | |||
| 122f37d6ce | |||
| 9fe40ff78a | |||
| ae4d75f310 | |||
| 707379c0fd | |||
| b861e0b17b | |||
| 7b576476a2 | |||
| 711a819ebb | |||
| 971b1f1fb0 | |||
| 4831bc67f5 | |||
| 1b8c6ecde0 | |||
| a4c78874e4 | |||
| b6c4693ef6 | |||
| adfa1cf3f2 | |||
| 6362a5ce91 | |||
| edeedd2258 | |||
| 02d0c9a8fd | |||
| d707b7f384 | |||
| b6f1d8eaf7 | |||
| c67fc70550 | |||
| e55be953bf | |||
| aca8cac1a4 | |||
| e699907970 | |||
| 97dc2f559b | |||
| 33a01eda28 | |||
| b38704374a | |||
| 318377212c | |||
| ddbdae5013 | |||
| 6eb7aa1fb0 | |||
| 078e8338fe | |||
| 6bcd76333a | |||
| b2728947a3 | |||
| 8ec5b0e8d8 | |||
| 2939d58585 | |||
| 146da1d00c | |||
| 51babeddc1 | |||
| 550197de96 | |||
| b5ad99332d | |||
| e9b51ffb1e | |||
| 8c9de79027 | |||
| 85c973469f | |||
| db8d9a8376 | |||
| 18b4e79f5f | |||
| 0b7f380298 | |||
| ea400c4ea5 | |||
| 07cfb62bb8 | |||
| a5efa0813f | |||
| d1f2912324 | |||
| ba0acf8f00 | |||
| 2788008fa1 | |||
| 5beee8352d | |||
| 35859fead1 | |||
| 0031d73eff | |||
| 89807fe548 | |||
| 9c28ce91ae | |||
| 7ed01ba893 | |||
| 926a8c0719 | |||
| 688a30531a | |||
| 1ff9d7f210 | |||
| da038f4fd8 | |||
| de20fa9582 | |||
| 80cf72adc9 | |||
| dabcea452a | |||
| 2feda943af | |||
| bffe375230 | |||
| efb69d02f3 | |||
| 79e5fa0b08 | |||
| de7e005b46 | |||
| a64fa8f493 | |||
| c19e141f68 | |||
| 94a522588d | |||
| 308c992fd3 | |||
| 458c6cbd22 | |||
| b717a7e9f7 | |||
| 6786b313b5 | |||
| 14db451313 | |||
| ced92a10e1 | |||
| 0d12baf49f | |||
| b798270bb7 | |||
| 955f358577 | |||
| 97a5deda23 | |||
| 8ae395e9cd | |||
| 44a9a507f6 | |||
| 903ee0d6ce | |||
| a36943cd2d | |||
| 26e0ce7778 | |||
| 5a9f9da347 | |||
| a1bf157408 | |||
| 27834d37ed | |||
| c260ac6df4 | |||
| 7479bed4e4 | |||
| 39394f76d5 |
@@ -1,10 +1,11 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Server\Abstracts;
|
namespace Kiri\Server\Abstracts;
|
||||||
|
|
||||||
|
|
||||||
use JetBrains\PhpStorm\Pure;
|
use Kiri\Context;
|
||||||
use Server\Contract\OnProcessInterface;
|
use Kiri\Server\Broadcast\OnBroadcastInterface;
|
||||||
|
use Kiri\Server\Contract\OnProcessInterface;
|
||||||
use Swoole\Coroutine;
|
use Swoole\Coroutine;
|
||||||
use Swoole\Process;
|
use Swoole\Process;
|
||||||
|
|
||||||
@@ -17,7 +18,7 @@ abstract class BaseProcess implements OnProcessInterface
|
|||||||
protected bool $isStop = false;
|
protected bool $isStop = false;
|
||||||
|
|
||||||
|
|
||||||
protected mixed $redirect_stdin_and_stdout = null;
|
protected bool $redirect_stdin_and_stdout = FALSE;
|
||||||
|
|
||||||
|
|
||||||
protected int $pipe_type = SOCK_DGRAM;
|
protected int $pipe_type = SOCK_DGRAM;
|
||||||
@@ -47,9 +48,9 @@ abstract class BaseProcess implements OnProcessInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return mixed
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function getRedirectStdinAndStdout(): mixed
|
public function getRedirectStdinAndStdout(): bool
|
||||||
{
|
{
|
||||||
return $this->redirect_stdin_and_stdout;
|
return $this->redirect_stdin_and_stdout;
|
||||||
}
|
}
|
||||||
@@ -80,4 +81,36 @@ abstract class BaseProcess implements OnProcessInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public function onSigterm(): static
|
||||||
|
{
|
||||||
|
if (!Context::inCoroutine()) {
|
||||||
|
Process::signal(SIGTERM, fn($data) => $this->onShutdown($data));
|
||||||
|
} else {
|
||||||
|
Coroutine::create(function () {
|
||||||
|
$data = Coroutine::waitSignal(SIGTERM, -1);
|
||||||
|
if ($data) {
|
||||||
|
$this->onShutdown($data);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $data
|
||||||
|
*/
|
||||||
|
protected function onShutdown($data): void
|
||||||
|
{
|
||||||
|
$this->isStop = true;
|
||||||
|
$value = Context::getContext('waite:process:message');
|
||||||
|
if (Coroutine::exists($value)) {
|
||||||
|
Coroutine::cancel($value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,14 +1,14 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
|
||||||
namespace Server\Abstracts;
|
namespace Kiri\Server\Abstracts;
|
||||||
|
|
||||||
|
|
||||||
use Annotation\Inject;
|
use Kiri\Annotation\Inject;
|
||||||
use Exception;
|
use Exception;
|
||||||
use Kiri\Abstracts\Config;
|
use Kiri\Abstracts\Config;
|
||||||
use Kiri\Exception\ConfigException;
|
use Kiri\Exception\ConfigException;
|
||||||
use Kiri\Kiri;
|
use Kiri;
|
||||||
use Psr\Log\LoggerInterface;
|
use Psr\Log\LoggerInterface;
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,34 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Kiri\Server\Broadcast;
|
||||||
|
|
||||||
|
use Kiri;
|
||||||
|
use Kiri\Server\ProcessManager;
|
||||||
|
use Kiri\Server\SwooleServerInterface;
|
||||||
|
|
||||||
|
class Broadcast
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $message
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function broadcast($message)
|
||||||
|
{
|
||||||
|
$di = Kiri::getDi();
|
||||||
|
$di->get(ProcessManager::class)->push($message);
|
||||||
|
|
||||||
|
$server = $di->get(SwooleServerInterface::class);
|
||||||
|
|
||||||
|
$total = $server->setting['worker_num'] + $server->setting['task_worker_num'];
|
||||||
|
for ($i = 0; $i < $total; $i++) {
|
||||||
|
if ($i == env('environmental_workerId')) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$server->sendMessage(serialize(new Message($message)), $i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Kiri\Server\Broadcast;
|
||||||
|
|
||||||
|
|
||||||
|
use Kiri;
|
||||||
|
use Kiri\Server\Contract\OnPipeMessageInterface;
|
||||||
|
use Psr\Log\LoggerInterface;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
class Message implements OnPipeMessageInterface, OnBroadcastInterface
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param mixed $data
|
||||||
|
*/
|
||||||
|
public function __construct(public mixed $data)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function process(): void
|
||||||
|
{
|
||||||
|
$logger = Kiri::getDi()->get(LoggerInterface::class);
|
||||||
|
$logger->debug(env('environmental') . '::' . env('environmental_workerId', 0) . '::' . $this->data);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Kiri\Server\Broadcast;
|
||||||
|
|
||||||
|
interface OnBroadcastInterface
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function process(): void;
|
||||||
|
|
||||||
|
}
|
||||||
+1
-1
@@ -1,7 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
|
||||||
namespace Server;
|
namespace Kiri\Server;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Server\Contract;
|
namespace Kiri\Server\Contract;
|
||||||
|
|
||||||
interface OnBeforeShutdown
|
interface OnBeforeShutdown
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Server\Contract;
|
namespace Kiri\Server\Contract;
|
||||||
|
|
||||||
use Swoole\Server;
|
use Swoole\Server;
|
||||||
|
|
||||||
@@ -13,10 +13,9 @@ interface OnCloseInterface
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Server $server
|
|
||||||
* @param int $fd
|
* @param int $fd
|
||||||
*/
|
*/
|
||||||
public function onClose(Server $server, int $fd): void;
|
public function onClose(int $fd): void;
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Server\Contract;
|
namespace Kiri\Server\Contract;
|
||||||
|
|
||||||
use Swoole\Server;
|
use Swoole\Server;
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Server\Contract;
|
namespace Kiri\Server\Contract;
|
||||||
|
|
||||||
use Swoole\Server;
|
use Swoole\Server;
|
||||||
|
|
||||||
@@ -10,10 +10,9 @@ interface OnDisconnectInterface
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Server $server
|
|
||||||
* @param int $fd
|
* @param int $fd
|
||||||
*/
|
*/
|
||||||
public function onDisconnect(Server $server, int $fd): void;
|
public function onDisconnect(int $fd): void;
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Server\Contract;
|
|
||||||
|
|
||||||
use Swoole\Http\Response;
|
|
||||||
|
|
||||||
interface OnDownloadInterface
|
|
||||||
{
|
|
||||||
|
|
||||||
public function dispatch(Response $response);
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Server\Contract;
|
namespace Kiri\Server\Contract;
|
||||||
|
|
||||||
use Swoole\Http\Request;
|
use Swoole\Http\Request;
|
||||||
use Swoole\Http\Response;
|
use Swoole\Http\Response;
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Server\Contract;
|
namespace Kiri\Server\Contract;
|
||||||
|
|
||||||
use Swoole\Server;
|
use Kiri\Websocket\WebSocketInterface;
|
||||||
use Swoole\WebSocket\Frame;
|
use Swoole\WebSocket\Frame;
|
||||||
|
|
||||||
interface OnMessageInterface
|
interface OnMessageInterface
|
||||||
@@ -10,10 +10,9 @@ interface OnMessageInterface
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Server $server
|
|
||||||
* @param Frame $frame
|
* @param Frame $frame
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function onMessage(Server $server, Frame $frame): void;
|
public function onMessage(Frame $frame): void;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,18 +1,16 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Server\Contract;
|
namespace Kiri\Server\Contract;
|
||||||
|
|
||||||
use Swoole\Http\Request;
|
use Swoole\Http\Request;
|
||||||
use Swoole\WebSocket\Server;
|
|
||||||
|
|
||||||
interface OnOpenInterface
|
interface OnOpenInterface
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Server $server
|
* @param Request $request
|
||||||
* @param Request $request
|
*/
|
||||||
*/
|
public function onOpen(Request $request): void;
|
||||||
public function onOpen(Server $server, Request $request): void;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Server\Contract;
|
namespace Kiri\Server\Contract;
|
||||||
|
|
||||||
use Server\Abstracts\Server;
|
use Kiri\Server\Abstracts\Server;
|
||||||
|
|
||||||
interface OnPacketInterface
|
interface OnPacketInterface
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
|
||||||
namespace Server\Contract;
|
namespace Kiri\Server\Contract;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
|
||||||
namespace Server\Contract;
|
namespace Kiri\Server\Contract;
|
||||||
|
|
||||||
|
|
||||||
use Swoole\Process;
|
use Swoole\Process;
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Server\Contract;
|
namespace Kiri\Server\Contract;
|
||||||
|
|
||||||
use Swoole\Server;
|
use Swoole\Server;
|
||||||
|
|
||||||
|
|||||||
@@ -1,17 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
|
|
||||||
namespace Server\Contract;
|
|
||||||
|
|
||||||
|
|
||||||
use Swoole\Server;
|
|
||||||
|
|
||||||
interface OnTaskInterface
|
|
||||||
{
|
|
||||||
|
|
||||||
public function execute();
|
|
||||||
|
|
||||||
|
|
||||||
public function finish(Server $server, int $task_id);
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Server\Events;
|
namespace Kiri\Server\Events;
|
||||||
|
|
||||||
use Swoole\Server;
|
use Swoole\Server;
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Server\Events;
|
namespace Kiri\Server\Events;
|
||||||
|
|
||||||
class OnAfterWorkerStart
|
class OnAfterWorkerStart
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Server\Events;
|
namespace Kiri\Server\Events;
|
||||||
|
|
||||||
use Swoole\Server;
|
use Swoole\Server;
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Server\Events;
|
namespace Kiri\Server\Events;
|
||||||
|
|
||||||
use Swoole\Server;
|
use Swoole\Server;
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Server\Events;
|
namespace Kiri\Server\Events;
|
||||||
|
|
||||||
class OnBeforeWorkerStart
|
class OnBeforeWorkerStart
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Server\Events;
|
namespace Kiri\Server\Events;
|
||||||
|
|
||||||
use Swoole\Server;
|
use Swoole\Server;
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Server\Events;
|
namespace Kiri\Server\Events;
|
||||||
|
|
||||||
use Swoole\Server;
|
use Swoole\Server;
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Kiri\Server\Events;
|
||||||
|
|
||||||
|
class OnProcessStart
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Kiri\Server\Events;
|
||||||
|
|
||||||
|
class OnServerBeforeStart
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Server\Events;
|
namespace Kiri\Server\Events;
|
||||||
|
|
||||||
use Swoole\Server;
|
use Swoole\Server;
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Server\Events;
|
namespace Kiri\Server\Events;
|
||||||
|
|
||||||
use Swoole\Server;
|
use Swoole\Server;
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Server\Events;
|
namespace Kiri\Server\Events;
|
||||||
|
|
||||||
use Swoole\Server;
|
use Swoole\Server;
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Server\Events;
|
namespace Kiri\Server\Events;
|
||||||
|
|
||||||
use Swoole\Server;
|
use Swoole\Server;
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Server\Events;
|
namespace Kiri\Server\Events;
|
||||||
|
|
||||||
use Swoole\Server;
|
use Swoole\Server;
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Server\Events;
|
namespace Kiri\Server\Events;
|
||||||
|
|
||||||
use Swoole\Server;
|
use Swoole\Server;
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Server\Events;
|
namespace Kiri\Server\Events;
|
||||||
|
|
||||||
use Swoole\Server;
|
use Swoole\Server;
|
||||||
|
|
||||||
|
|||||||
@@ -1,12 +1,12 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Server\Handler;
|
namespace Kiri\Server\Handler;
|
||||||
|
|
||||||
use Annotation\Inject;
|
|
||||||
use Server\Abstracts\Server;
|
|
||||||
use Exception;
|
use Exception;
|
||||||
use Server\Contract\OnPipeMessageInterface;
|
use Kiri\Annotation\Inject;
|
||||||
use Kiri\Events\EventDispatch;
|
use Kiri\Events\EventDispatch;
|
||||||
|
use Kiri\Server\Abstracts\Server;
|
||||||
|
use Kiri\Server\Contract\OnPipeMessageInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@@ -15,10 +15,6 @@ class OnPipeMessage extends Server
|
|||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
/** @var EventDispatch */
|
|
||||||
#[Inject(EventDispatch::class)]
|
|
||||||
public EventDispatch $eventDispatch;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param \Swoole\Server $server
|
* @param \Swoole\Server $server
|
||||||
@@ -28,6 +24,9 @@ class OnPipeMessage extends Server
|
|||||||
*/
|
*/
|
||||||
public function onPipeMessage(\Swoole\Server $server, int $src_worker_id, mixed $message)
|
public function onPipeMessage(\Swoole\Server $server, int $src_worker_id, mixed $message)
|
||||||
{
|
{
|
||||||
|
if (is_string($message)) {
|
||||||
|
$message = unserialize($message);
|
||||||
|
}
|
||||||
if (!is_object($message) || !($message instanceof OnPipeMessageInterface)) {
|
if (!is_object($message) || !($message instanceof OnPipeMessageInterface)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
+13
-15
@@ -1,14 +1,15 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Server\Handler;
|
namespace Kiri\Server\Handler;
|
||||||
|
|
||||||
use Annotation\Inject;
|
use Kiri\Annotation\Inject;
|
||||||
use Kiri\Events\EventDispatch;
|
use Kiri\Events\EventDispatch;
|
||||||
use Kiri\Exception\ConfigException;
|
use Kiri\Exception\ConfigException;
|
||||||
use Server\Abstracts\Server;
|
use ReflectionException;
|
||||||
use Server\Events\OnBeforeShutdown;
|
use Kiri\Server\Abstracts\Server;
|
||||||
use Server\Events\OnShutdown;
|
use Kiri\Server\Events\OnBeforeShutdown;
|
||||||
use Server\Events\OnStart;
|
use Kiri\Server\Events\OnShutdown;
|
||||||
|
use Kiri\Server\Events\OnStart;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -18,40 +19,37 @@ use Server\Events\OnStart;
|
|||||||
class OnServer extends Server
|
class OnServer extends Server
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
|
||||||
* @var EventDispatch
|
|
||||||
*/
|
|
||||||
#[Inject(EventDispatch::class)]
|
|
||||||
public EventDispatch $eventDispatch;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param \Swoole\Server $server
|
* @param \Swoole\Server $server
|
||||||
* @throws ConfigException
|
* @throws ConfigException
|
||||||
|
* @throws ReflectionException
|
||||||
*/
|
*/
|
||||||
public function onStart(\Swoole\Server $server)
|
public function onStart(\Swoole\Server $server)
|
||||||
{
|
{
|
||||||
$this->setProcessName(sprintf('start[%d].server', $server->master_pid));
|
$this->setProcessName(sprintf('start[%d].server', $server->master_pid));
|
||||||
|
|
||||||
$this->eventDispatch->dispatch(new OnStart($server));
|
\Kiri::getDi()->get(EventDispatch::class)->dispatch(new OnStart($server));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param \Swoole\Server $server
|
* @param \Swoole\Server $server
|
||||||
|
* @throws ReflectionException
|
||||||
*/
|
*/
|
||||||
public function onBeforeShutdown(\Swoole\Server $server)
|
public function onBeforeShutdown(\Swoole\Server $server)
|
||||||
{
|
{
|
||||||
$this->eventDispatch->dispatch(new OnBeforeShutdown($server));
|
\Kiri::getDi()->get(EventDispatch::class)->dispatch(new OnBeforeShutdown($server));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param \Swoole\Server $server
|
* @param \Swoole\Server $server
|
||||||
|
* @throws ReflectionException
|
||||||
*/
|
*/
|
||||||
public function onShutdown(\Swoole\Server $server)
|
public function onShutdown(\Swoole\Server $server)
|
||||||
{
|
{
|
||||||
$this->eventDispatch->dispatch(new OnShutdown($server));
|
\Kiri::getDi()->get(EventDispatch::class)->dispatch(new OnShutdown($server));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+11
-15
@@ -1,13 +1,14 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Server\Handler;
|
namespace Kiri\Server\Handler;
|
||||||
|
|
||||||
use Annotation\Inject;
|
use Kiri\Annotation\Inject;
|
||||||
use Kiri\Events\EventDispatch;
|
use Kiri\Events\EventDispatch;
|
||||||
use Server\Abstracts\Server;
|
use ReflectionException;
|
||||||
|
use Kiri\Server\Abstracts\Server;
|
||||||
use Kiri\Exception\ConfigException;
|
use Kiri\Exception\ConfigException;
|
||||||
use Server\Events\OnManagerStart;
|
use Kiri\Server\Events\OnManagerStart;
|
||||||
use Server\Events\OnManagerStop;
|
use Kiri\Server\Events\OnManagerStop;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -17,31 +18,26 @@ use Server\Events\OnManagerStop;
|
|||||||
class OnServerManager extends Server
|
class OnServerManager extends Server
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
|
||||||
* @var EventDispatch
|
|
||||||
*/
|
|
||||||
#[Inject(EventDispatch::class)]
|
|
||||||
public EventDispatch $eventDispatch;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param \Swoole\Server $server
|
* @param \Swoole\Server $server
|
||||||
* @throws ConfigException
|
* @throws ConfigException|ReflectionException
|
||||||
*/
|
*/
|
||||||
public function onManagerStart(\Swoole\Server $server)
|
public function onManagerStart(\Swoole\Server $server)
|
||||||
{
|
{
|
||||||
$this->setProcessName(sprintf('manger[%d].0', $server->manager_pid));
|
$this->setProcessName(sprintf('manger[%d].0', $server->manager_pid));
|
||||||
|
|
||||||
$this->eventDispatch->dispatch(new OnManagerStart($server));
|
\Kiri::getDi()->get(EventDispatch::class)->dispatch(new OnManagerStart($server));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param \Swoole\Server $server
|
* @param \Swoole\Server $server
|
||||||
|
* @throws ReflectionException
|
||||||
*/
|
*/
|
||||||
public function onManagerStop(\Swoole\Server $server)
|
public function onManagerStop(\Swoole\Server $server)
|
||||||
{
|
{
|
||||||
$this->eventDispatch->dispatch(new OnManagerStop($server));
|
\Kiri::getDi()->get(EventDispatch::class)->dispatch(new OnManagerStop($server));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Server\Handler;
|
namespace Kiri\Server\Handler;
|
||||||
|
|
||||||
use Annotation\Inject;
|
use Kiri\Annotation\Inject;
|
||||||
use Kiri\Events\EventDispatch;
|
use Kiri\Events\EventDispatch;
|
||||||
use Server\Events\OnAfterReload;
|
use Kiri\Server\Events\OnAfterReload;
|
||||||
use Server\Events\OnBeforeReload;
|
use Kiri\Server\Events\OnBeforeReload;
|
||||||
use Swoole\Server;
|
use Swoole\Server;
|
||||||
|
|
||||||
|
|
||||||
@@ -16,28 +16,23 @@ class OnServerReload
|
|||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var EventDispatch
|
|
||||||
*/
|
|
||||||
#[Inject(EventDispatch::class)]
|
|
||||||
public EventDispatch $eventDispatch;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Server $server
|
* @param Server $server
|
||||||
|
* @throws \ReflectionException
|
||||||
*/
|
*/
|
||||||
public function onBeforeReload(Server $server)
|
public function onBeforeReload(Server $server)
|
||||||
{
|
{
|
||||||
$this->eventDispatch->dispatch(new OnBeforeReload($server));
|
\Kiri::getDi()->get(EventDispatch::class)->dispatch(new OnBeforeReload($server));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Server $server
|
* @param Server $server
|
||||||
|
* @throws \ReflectionException
|
||||||
*/
|
*/
|
||||||
public function onAfterReload(Server $server)
|
public function onAfterReload(Server $server)
|
||||||
{
|
{
|
||||||
$this->eventDispatch->dispatch(new OnAfterReload($server));
|
\Kiri::getDi()->get(EventDispatch::class)->dispatch(new OnAfterReload($server));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,101 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
|
|
||||||
namespace Server\Handler;
|
|
||||||
|
|
||||||
|
|
||||||
use Annotation\Inject;
|
|
||||||
use Kiri\Abstracts\Logger;
|
|
||||||
use Kiri\Exception\ConfigException;
|
|
||||||
use Kiri\Kiri;
|
|
||||||
use ReflectionException;
|
|
||||||
use Server\Contract\OnTaskInterface;
|
|
||||||
use Swoole\Server;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Class OnServerTask
|
|
||||||
* @package Server\Task
|
|
||||||
*/
|
|
||||||
class OnServerTask
|
|
||||||
{
|
|
||||||
|
|
||||||
|
|
||||||
#[Inject(Logger::class)]
|
|
||||||
public Logger $logger;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param Server $server
|
|
||||||
* @param int $task_id
|
|
||||||
* @param int $src_worker_id
|
|
||||||
* @param mixed $data
|
|
||||||
* @throws ConfigException
|
|
||||||
*/
|
|
||||||
public function onTask(Server $server, int $task_id, int $src_worker_id, mixed $data)
|
|
||||||
{
|
|
||||||
try {
|
|
||||||
$data = $this->resolve($data);
|
|
||||||
} catch (\Throwable $exception) {
|
|
||||||
$data = jTraceEx($exception);
|
|
||||||
|
|
||||||
$this->logger->error('task', [$data]);
|
|
||||||
} finally {
|
|
||||||
$server->finish($data);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param Server $server
|
|
||||||
* @param Server\Task $task
|
|
||||||
* @throws ConfigException
|
|
||||||
*/
|
|
||||||
public function onCoroutineTask(Server $server, Server\Task $task)
|
|
||||||
{
|
|
||||||
try {
|
|
||||||
$data = $this->resolve($task->data);
|
|
||||||
} catch (\Throwable $exception) {
|
|
||||||
$data = jTraceEx($exception);
|
|
||||||
|
|
||||||
$this->logger->error('task', [$data]);
|
|
||||||
} finally {
|
|
||||||
$server->finish($data);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $data
|
|
||||||
* @return null
|
|
||||||
* @throws ReflectionException
|
|
||||||
*/
|
|
||||||
private function resolve($data)
|
|
||||||
{
|
|
||||||
[$class, $params] = json_encode($data, true);
|
|
||||||
|
|
||||||
$reflect = Kiri::getDi()->getReflect($class);
|
|
||||||
|
|
||||||
if (!$reflect->isInstantiable()) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
$class = $reflect->newInstanceArgs($params);
|
|
||||||
return $class->execute();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param Server $server
|
|
||||||
* @param int $task_id
|
|
||||||
* @param mixed $data
|
|
||||||
*/
|
|
||||||
public function onFinish(Server $server, int $task_id, mixed $data)
|
|
||||||
{
|
|
||||||
if (!($data instanceof OnTaskInterface)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
$data->finish($server, $task_id);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
+34
-27
@@ -1,20 +1,21 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Server\Handler;
|
namespace Kiri\Server\Handler;
|
||||||
|
|
||||||
use Annotation\Inject;
|
|
||||||
use Exception;
|
use Exception;
|
||||||
|
use Kiri;
|
||||||
use Kiri\Abstracts\Config;
|
use Kiri\Abstracts\Config;
|
||||||
|
use Kiri\Annotation\Inject;
|
||||||
use Kiri\Core\Help;
|
use Kiri\Core\Help;
|
||||||
use Kiri\Events\EventDispatch;
|
use Kiri\Events\EventDispatch;
|
||||||
use Kiri\Kiri;
|
use Kiri\Message\Handler\Router;
|
||||||
use Server\Events\OnAfterWorkerStart;
|
use Kiri\Server\Events\OnAfterWorkerStart;
|
||||||
use Server\Events\OnBeforeWorkerStart;
|
use Kiri\Server\Events\OnBeforeWorkerStart;
|
||||||
use Server\Events\OnTaskerStart as OnTaskStart;
|
use Kiri\Server\Events\OnTaskerStart as OnTaskStart;
|
||||||
use Server\Events\OnWorkerError;
|
use Kiri\Server\Events\OnWorkerError;
|
||||||
use Server\Events\OnWorkerExit;
|
use Kiri\Server\Events\OnWorkerExit;
|
||||||
use Server\Events\OnWorkerStart;
|
use Kiri\Server\Events\OnWorkerStart;
|
||||||
use Server\Events\OnWorkerStop;
|
use Kiri\Server\Events\OnWorkerStop;
|
||||||
use Swoole\Server;
|
use Swoole\Server;
|
||||||
use Swoole\Timer;
|
use Swoole\Timer;
|
||||||
|
|
||||||
@@ -23,15 +24,20 @@ use Swoole\Timer;
|
|||||||
* Class OnServerWorker
|
* Class OnServerWorker
|
||||||
* @package Server\Worker
|
* @package Server\Worker
|
||||||
*/
|
*/
|
||||||
class OnServerWorker extends \Server\Abstracts\Server
|
class OnServerWorker extends \Kiri\Server\Abstracts\Server
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
|
public Router $collector;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var EventDispatch
|
* @return void
|
||||||
*/
|
*/
|
||||||
#[Inject(EventDispatch::class)]
|
public function init()
|
||||||
public EventDispatch $eventDispatch;
|
{
|
||||||
|
$this->collector = Kiri::getDi()->get(Router::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -41,17 +47,20 @@ class OnServerWorker extends \Server\Abstracts\Server
|
|||||||
*/
|
*/
|
||||||
public function onWorkerStart(Server $server, int $workerId)
|
public function onWorkerStart(Server $server, int $workerId)
|
||||||
{
|
{
|
||||||
$this->eventDispatch->dispatch(new OnBeforeWorkerStart($workerId));
|
$dispatch = \Kiri::getDi()->get(EventDispatch::class);
|
||||||
|
$dispatch->dispatch(new OnBeforeWorkerStart($workerId));
|
||||||
|
set_env('environmental_workerId', $workerId);
|
||||||
if ($workerId < $server->setting['worker_num']) {
|
if ($workerId < $server->setting['worker_num']) {
|
||||||
$this->eventDispatch->dispatch(new OnWorkerStart($server, $workerId));
|
|
||||||
$this->setProcessName(sprintf('Worker[%d].%d', $server->worker_pid, $workerId));
|
$this->setProcessName(sprintf('Worker[%d].%d', $server->worker_pid, $workerId));
|
||||||
|
$this->collector->scan_build_route();
|
||||||
|
$dispatch->dispatch(new OnWorkerStart($server, $workerId));
|
||||||
set_env('environmental', Kiri::WORKER);
|
set_env('environmental', Kiri::WORKER);
|
||||||
} else {
|
} else {
|
||||||
$this->eventDispatch->dispatch(new OnTaskStart($server, $workerId));
|
$dispatch->dispatch(new OnTaskStart($server, $workerId));
|
||||||
$this->setProcessName(sprintf('Tasker[%d].%d', $server->worker_pid, $workerId));
|
$this->setProcessName(sprintf('Tasker[%d].%d', $server->worker_pid, $workerId));
|
||||||
set_env('environmental', Kiri::TASK);
|
set_env('environmental', Kiri::TASK);
|
||||||
}
|
}
|
||||||
$this->eventDispatch->dispatch(new OnAfterWorkerStart());
|
$dispatch->dispatch(new OnAfterWorkerStart());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -62,8 +71,8 @@ class OnServerWorker extends \Server\Abstracts\Server
|
|||||||
*/
|
*/
|
||||||
public function onWorkerStop(Server $server, int $workerId)
|
public function onWorkerStop(Server $server, int $workerId)
|
||||||
{
|
{
|
||||||
Timer::clearAll();
|
Timer::clearAll();
|
||||||
$this->eventDispatch->dispatch(new OnWorkerStop($server, $workerId));
|
\Kiri::getDi()->get(EventDispatch::class)->dispatch(new OnWorkerStop($server, $workerId));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -74,10 +83,8 @@ class OnServerWorker extends \Server\Abstracts\Server
|
|||||||
*/
|
*/
|
||||||
public function onWorkerExit(Server $server, int $workerId)
|
public function onWorkerExit(Server $server, int $workerId)
|
||||||
{
|
{
|
||||||
set_env('state', 'exit');
|
\Kiri::getDi()->get(EventDispatch::class)->dispatch(new OnWorkerExit($server, $workerId));
|
||||||
|
}
|
||||||
$this->eventDispatch->dispatch(new OnWorkerExit($server, $workerId));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -90,7 +97,7 @@ class OnServerWorker extends \Server\Abstracts\Server
|
|||||||
*/
|
*/
|
||||||
public function onWorkerError(Server $server, int $worker_id, int $worker_pid, int $exit_code, int $signal)
|
public function onWorkerError(Server $server, int $worker_id, int $worker_pid, int $exit_code, int $signal)
|
||||||
{
|
{
|
||||||
$this->eventDispatch->dispatch(new OnWorkerError($server, $worker_id, $worker_pid, $exit_code, $signal));
|
\Kiri::getDi()->get(EventDispatch::class)->dispatch(new OnWorkerError($server, $worker_id, $worker_pid, $exit_code, $signal));
|
||||||
|
|
||||||
$message = sprintf('Worker#%d::%d error stop. signal %d, exit_code %d, msg %s',
|
$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(), 9)
|
$worker_id, $worker_pid, $signal, $exit_code, swoole_strerror(swoole_last_error(), 9)
|
||||||
@@ -98,7 +105,7 @@ class OnServerWorker extends \Server\Abstracts\Server
|
|||||||
|
|
||||||
$this->logger->error($message);
|
$this->logger->error($message);
|
||||||
|
|
||||||
// $this->system_mail($message);
|
$this->system_mail($message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -109,7 +116,7 @@ class OnServerWorker extends \Server\Abstracts\Server
|
|||||||
protected function system_mail($messageContent)
|
protected function system_mail($messageContent)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$email = Config::get('email');
|
$email = Config::get('email', ['enable' => false]);
|
||||||
if (!empty($email) && ($email['enable'] ?? false) == true) {
|
if (!empty($email) && ($email['enable'] ?? false) == true) {
|
||||||
Help::sendEmail($email, 'Service Error', $messageContent);
|
Help::sendEmail($email, 'Service Error', $messageContent);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,125 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Kiri\Server;
|
||||||
|
|
||||||
|
use Kiri;
|
||||||
|
use Kiri\Abstracts\Config;
|
||||||
|
use Kiri\Annotation\Inject;
|
||||||
|
use Kiri\Context;
|
||||||
|
use Kiri\Events\EventDispatch;
|
||||||
|
use Kiri\Exception\ConfigException;
|
||||||
|
use Kiri\Server\Abstracts\BaseProcess;
|
||||||
|
use Kiri\Server\Broadcast\Message;
|
||||||
|
use Kiri\Server\Contract\OnProcessInterface;
|
||||||
|
use Kiri\Server\Events\OnProcessStart;
|
||||||
|
use Psr\Log\LoggerInterface;
|
||||||
|
use Swoole\Coroutine;
|
||||||
|
use Swoole\Process;
|
||||||
|
|
||||||
|
class ProcessManager
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
/** @var array<string, Process> */
|
||||||
|
private array $_process = [];
|
||||||
|
|
||||||
|
|
||||||
|
#[Inject(LoggerInterface::class)]
|
||||||
|
public LoggerInterface $logger;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string|OnProcessInterface|BaseProcess $customProcess
|
||||||
|
* @return void
|
||||||
|
* @throws ConfigException
|
||||||
|
*/
|
||||||
|
public function add(string|OnProcessInterface|BaseProcess $customProcess)
|
||||||
|
{
|
||||||
|
$server = Kiri::getDi()->get(SwooleServerInterface::class);
|
||||||
|
if (is_string($customProcess)) {
|
||||||
|
$customProcess = Kiri::getDi()->get($customProcess);
|
||||||
|
}
|
||||||
|
|
||||||
|
$system = sprintf('[%s].process', Config::get('id', 'system-service'));
|
||||||
|
|
||||||
|
$this->logger->debug($system . ' ' . $customProcess->getName() . ' start.');
|
||||||
|
$process = $this->parse($customProcess, $system);
|
||||||
|
if (Context::inCoroutine()) {
|
||||||
|
Coroutine::create(function () use ($process) {
|
||||||
|
$process->start();
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
$server->addProcess($process = $this->parse($customProcess, $system));
|
||||||
|
}
|
||||||
|
$this->_process[$customProcess->getName()] = $process;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $customProcess
|
||||||
|
* @param $system
|
||||||
|
* @return Process
|
||||||
|
*/
|
||||||
|
private function parse($customProcess, $system): Process
|
||||||
|
{
|
||||||
|
return new Process(function (Process $process) use ($customProcess, $system) {
|
||||||
|
if (Kiri::getPlatform()->isLinux()) {
|
||||||
|
$process->name($system . '(' . $customProcess->getName() . ')');
|
||||||
|
}
|
||||||
|
|
||||||
|
Kiri::getDi()->get(EventDispatch::class)->dispatch(new OnProcessStart());
|
||||||
|
|
||||||
|
set_env('environmental', Kiri::PROCESS);
|
||||||
|
$channel = Coroutine::create(function () use ($process, $customProcess) {
|
||||||
|
while (!$customProcess->isStop()) {
|
||||||
|
$message = $process->read();
|
||||||
|
if (!empty($message)) {
|
||||||
|
$message = unserialize($message);
|
||||||
|
}
|
||||||
|
if (is_null($message)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$customProcess->onBroadcast($message);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
Context::setContext('waite:process:message', $channel);
|
||||||
|
|
||||||
|
$customProcess->onSigterm()->process($process);
|
||||||
|
},
|
||||||
|
$customProcess->getRedirectStdinAndStdout(),
|
||||||
|
$customProcess->getPipeType(),
|
||||||
|
$customProcess->isEnableCoroutine()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $processes
|
||||||
|
* @return void
|
||||||
|
* @throws ConfigException
|
||||||
|
*/
|
||||||
|
public function batch(array $processes)
|
||||||
|
{
|
||||||
|
foreach ($processes as $process) {
|
||||||
|
$this->add($process);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $message
|
||||||
|
* @param string $name
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function push(string $message, string $name = '')
|
||||||
|
{
|
||||||
|
$processes = $this->_process;
|
||||||
|
if (!empty($this->_process[$name])) {
|
||||||
|
$processes = [$this->_process[$name]];
|
||||||
|
}
|
||||||
|
foreach ($processes as $process) {
|
||||||
|
$process->write(serialize(new Message($message)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
+48
-26
@@ -1,16 +1,22 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
|
||||||
namespace Server;
|
namespace Kiri\Server;
|
||||||
|
|
||||||
use Annotation\Inject;
|
|
||||||
use Exception;
|
use Exception;
|
||||||
use Http\Handler\Abstracts\HttpService;
|
|
||||||
use JetBrains\PhpStorm\Pure;
|
use JetBrains\PhpStorm\Pure;
|
||||||
|
use Kiri;
|
||||||
use Kiri\Abstracts\Config;
|
use Kiri\Abstracts\Config;
|
||||||
|
use Kiri\Annotation\Inject;
|
||||||
use Kiri\Events\EventDispatch;
|
use Kiri\Events\EventDispatch;
|
||||||
use Kiri\Exception\ConfigException;
|
use Kiri\Exception\ConfigException;
|
||||||
use Server\Events\OnShutdown;
|
use Kiri\Message\Handler\Abstracts\HttpService;
|
||||||
|
use Kiri\Server\Events\OnShutdown;
|
||||||
|
use Psr\Container\ContainerExceptionInterface;
|
||||||
|
use Kiri\Server\Events\OnServerBeforeStart;
|
||||||
|
use Psr\Container\NotFoundExceptionInterface;
|
||||||
|
use ReflectionException;
|
||||||
|
use Swoole\Coroutine;
|
||||||
|
|
||||||
|
|
||||||
defined('PID_PATH') or define('PID_PATH', APP_PATH . 'storage/server.pid');
|
defined('PID_PATH') or define('PID_PATH', APP_PATH . 'storage/server.pid');
|
||||||
@@ -26,19 +32,17 @@ class Server extends HttpService
|
|||||||
];
|
];
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @Inject ServerManager
|
|
||||||
* @var null|ServerManager
|
|
||||||
*/
|
|
||||||
#[Inject(ServerManager::class)]
|
|
||||||
public ?ServerManager $manager = null;
|
|
||||||
|
|
||||||
private mixed $daemon = 0;
|
private mixed $daemon = 0;
|
||||||
|
|
||||||
|
|
||||||
/** @var EventDispatch */
|
/**
|
||||||
#[Inject(EventDispatch::class)]
|
* @var State
|
||||||
public EventDispatch $eventDispatch;
|
*/
|
||||||
|
#[Inject(State::class)]
|
||||||
|
public State $state;
|
||||||
|
|
||||||
|
|
||||||
|
public ServerManager $manager;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -46,6 +50,19 @@ class Server extends HttpService
|
|||||||
*/
|
*/
|
||||||
public function init()
|
public function init()
|
||||||
{
|
{
|
||||||
|
$this->manager = Kiri::getContainer()->get(ServerManager::class);
|
||||||
|
$enable_coroutine = Config::get('servers.settings.enable_coroutine', false);
|
||||||
|
Config::set('servers.settings.enable_coroutine', true);
|
||||||
|
if ($enable_coroutine != true) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Coroutine::set([
|
||||||
|
'hook_flags' => SWOOLE_HOOK_ALL ^ SWOOLE_HOOK_BLOCKING_FUNCTION,
|
||||||
|
'enable_deadlock_check' => FALSE,
|
||||||
|
'exit_condition' => function () {
|
||||||
|
return Coroutine::stats()['coroutine_num'] === 0;
|
||||||
|
}
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -59,13 +76,14 @@ class Server extends HttpService
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string start server
|
* @return string
|
||||||
*
|
|
||||||
* start server
|
|
||||||
* @throws ConfigException
|
* @throws ConfigException
|
||||||
|
* @throws ContainerExceptionInterface
|
||||||
|
* @throws NotFoundExceptionInterface
|
||||||
|
* @throws ReflectionException
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function start(): string
|
public function start(): mixed
|
||||||
{
|
{
|
||||||
$this->manager->initBaseServer(Config::get('server', [], true), $this->daemon);
|
$this->manager->initBaseServer(Config::get('server', [], true), $this->daemon);
|
||||||
|
|
||||||
@@ -76,9 +94,10 @@ class Server extends HttpService
|
|||||||
}
|
}
|
||||||
|
|
||||||
$processes = array_merge($this->process, Config::get('processes', []));
|
$processes = array_merge($this->process, Config::get('processes', []));
|
||||||
foreach ($processes as $process) {
|
|
||||||
$this->manager->addProcess($process);
|
$this->getContainer()->get(ProcessManager::class)->batch($processes);
|
||||||
}
|
|
||||||
|
$this->getEventDispatch()->dispatch(new OnServerBeforeStart());
|
||||||
|
|
||||||
return $this->manager->getServer()->start();
|
return $this->manager->getServer()->start();
|
||||||
}
|
}
|
||||||
@@ -86,27 +105,30 @@ class Server extends HttpService
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @return void
|
* @return void
|
||||||
*
|
* @throws ConfigException
|
||||||
* start server
|
* @throws ContainerExceptionInterface
|
||||||
|
* @throws NotFoundExceptionInterface
|
||||||
|
* @throws ReflectionException
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function shutdown()
|
public function shutdown()
|
||||||
{
|
{
|
||||||
$configs = Config::get('server', [], true);
|
$configs = Config::get('server', [], true);
|
||||||
foreach ($this->manager->sortService($configs['ports'] ?? []) as $config) {
|
foreach ($this->manager->sortService($configs['ports'] ?? []) as $config) {
|
||||||
$this->manager->stopServer($config['port']);
|
$this->state->exit($config['port']);
|
||||||
}
|
}
|
||||||
$this->eventDispatch->dispatch(new OnShutdown());
|
$this->getContainer()->get(EventDispatch::class)->dispatch(new OnShutdown());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return bool
|
* @return bool
|
||||||
* @throws ConfigException
|
* @throws ConfigException
|
||||||
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function isRunner(): bool
|
public function isRunner(): bool
|
||||||
{
|
{
|
||||||
return $this->manager->isRunner();
|
return $this->state->isRunner();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+21
-57
@@ -1,15 +1,17 @@
|
|||||||
<?php
|
<?php
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Server;
|
namespace Kiri\Server;
|
||||||
|
|
||||||
|
|
||||||
use Annotation\Inject;
|
|
||||||
use Exception;
|
use Exception;
|
||||||
use Kiri\Abstracts\Config;
|
use Kiri\Abstracts\Config;
|
||||||
use Kiri\Events\EventProvider;
|
use Kiri\Events\EventDispatch;
|
||||||
use Kiri\Exception\ConfigException;
|
use Kiri\Exception\ConfigException;
|
||||||
use Kiri\Kiri;
|
use Kiri;
|
||||||
|
use Kiri\Annotation\Inject;
|
||||||
|
use Psr\Container\ContainerExceptionInterface;
|
||||||
|
use Psr\Container\NotFoundExceptionInterface;
|
||||||
use Swoole\Coroutine;
|
use Swoole\Coroutine;
|
||||||
use Symfony\Component\Console\Command\Command;
|
use Symfony\Component\Console\Command\Command;
|
||||||
use Symfony\Component\Console\Input\InputArgument;
|
use Symfony\Component\Console\Input\InputArgument;
|
||||||
@@ -28,12 +30,6 @@ class ServerCommand extends Command
|
|||||||
const ACTIONS = ['start', 'stop', 'restart'];
|
const ACTIONS = ['start', 'stop', 'restart'];
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var EventProvider
|
|
||||||
*/
|
|
||||||
#[Inject(EventProvider::class)]
|
|
||||||
public EventProvider $eventProvider;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@@ -51,63 +47,31 @@ class ServerCommand extends Command
|
|||||||
* @param InputInterface $input
|
* @param InputInterface $input
|
||||||
* @param OutputInterface $output
|
* @param OutputInterface $output
|
||||||
* @return int
|
* @return int
|
||||||
|
* @throws ConfigException
|
||||||
|
* @throws ContainerExceptionInterface
|
||||||
|
* @throws NotFoundExceptionInterface
|
||||||
|
* @throws \ReflectionException
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function execute(InputInterface $input, OutputInterface $output): int
|
public function execute(InputInterface $input, OutputInterface $output): int
|
||||||
{
|
{
|
||||||
$manager = Kiri::app()->getServer();
|
$manager = Kiri::app()->getServer();
|
||||||
$manager->setDaemon((int)!is_null($input->getOption('daemon')));
|
$manager->setDaemon((int)!is_null($input->getOption('daemon')));
|
||||||
if (is_null($input->getArgument('action'))) {
|
|
||||||
$input->setArgument('action', 'restart');
|
$action = $input->getArgument('action');
|
||||||
}
|
if (is_null($action)) {
|
||||||
if (!in_array($input->getArgument('action'), self::ACTIONS)) {
|
|
||||||
throw new Exception('I don\'t know what I want to do.');
|
throw new Exception('I don\'t know what I want to do.');
|
||||||
}
|
}
|
||||||
if ($manager->isRunner() && $input->getArgument('action') == 'start') {
|
if (!in_array($action, self::ACTIONS)) {
|
||||||
throw new Exception('Service is running. Please use restart.');
|
throw new Exception('I don\'t know what I want to do.');
|
||||||
}
|
}
|
||||||
$manager->shutdown();
|
if ($action == 'restart' || $action == 'stop') {
|
||||||
if ($input->getArgument('action') == 'stop') {
|
$manager->shutdown();
|
||||||
return 0;
|
if ($action == 'stop') {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return $this->generate_runtime_builder($manager);
|
return (int)$manager->start();
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @throws ConfigException
|
|
||||||
*/
|
|
||||||
private function configure_set()
|
|
||||||
{
|
|
||||||
$enable_coroutine = Config::get('servers.settings.enable_coroutine', false);
|
|
||||||
if ($enable_coroutine != true) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
Coroutine::set([
|
|
||||||
'hook_flags' => SWOOLE_HOOK_ALL ^ SWOOLE_HOOK_BLOCKING_FUNCTION,
|
|
||||||
'enable_deadlock_check' => FALSE,
|
|
||||||
'exit_condition' => function () {
|
|
||||||
return Coroutine::stats()['coroutine_num'] === 0;
|
|
||||||
}
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $manager
|
|
||||||
* @return int
|
|
||||||
* @throws ConfigException
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
private function generate_runtime_builder($manager): int
|
|
||||||
{
|
|
||||||
$this->configure_set();
|
|
||||||
|
|
||||||
Kiri::app()->getRouter()->read_files();
|
|
||||||
|
|
||||||
$manager->start();
|
|
||||||
|
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+56
-189
@@ -1,33 +1,27 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Server;
|
namespace Kiri\Server;
|
||||||
|
|
||||||
use Annotation\Inject;
|
|
||||||
use Exception;
|
use Exception;
|
||||||
|
use Kiri\Abstracts\Component;
|
||||||
use Kiri\Abstracts\Config;
|
use Kiri\Abstracts\Config;
|
||||||
use Kiri\Di\ContainerInterface;
|
use Kiri\Annotation\Inject;
|
||||||
use Kiri\Error\Logger;
|
use Kiri\Error\Logger;
|
||||||
use Kiri\Exception\ConfigException;
|
use Kiri\Exception\ConfigException;
|
||||||
use Kiri\Kiri;
|
use Kiri\Server\Contract\OnCloseInterface;
|
||||||
use ReflectionException;
|
use Kiri\Server\Contract\OnConnectInterface;
|
||||||
use Server\Abstracts\BaseProcess;
|
use Kiri\Server\Contract\OnDisconnectInterface;
|
||||||
use Server\Contract\OnCloseInterface;
|
use Kiri\Server\Contract\OnHandshakeInterface;
|
||||||
use Server\Contract\OnConnectInterface;
|
use Kiri\Server\Contract\OnMessageInterface;
|
||||||
use Server\Contract\OnDisconnectInterface;
|
use Kiri\Server\Contract\OnPacketInterface;
|
||||||
use Server\Contract\OnHandshakeInterface;
|
use Kiri\Server\Contract\OnReceiveInterface;
|
||||||
use Server\Contract\OnMessageInterface;
|
use Kiri\Server\Handler\OnPipeMessage;
|
||||||
use Server\Contract\OnPacketInterface;
|
use Kiri\Server\Handler\OnServer;
|
||||||
use Server\Contract\OnProcessInterface;
|
use Kiri\Server\Handler\OnServerWorker;
|
||||||
use Server\Contract\OnReceiveInterface;
|
use Kiri\Task\TaskManager;
|
||||||
use Server\Contract\OnTaskInterface;
|
use Psr\Container\ContainerExceptionInterface;
|
||||||
use Server\Handler\OnPipeMessage;
|
use Psr\Container\NotFoundExceptionInterface;
|
||||||
use Server\Handler\OnServer;
|
|
||||||
use Server\Handler\OnServerManager;
|
|
||||||
use Server\Handler\OnServerReload;
|
|
||||||
use Server\Handler\OnServerTask;
|
|
||||||
use Server\Handler\OnServerWorker;
|
|
||||||
use Swoole\Http\Server as HServer;
|
use Swoole\Http\Server as HServer;
|
||||||
use Swoole\Process;
|
|
||||||
use Swoole\Server;
|
use Swoole\Server;
|
||||||
use Swoole\Server\Port;
|
use Swoole\Server\Port;
|
||||||
use Swoole\WebSocket\Server as WServer;
|
use Swoole\WebSocket\Server as WServer;
|
||||||
@@ -37,7 +31,7 @@ use Swoole\WebSocket\Server as WServer;
|
|||||||
* Class OnServerManager
|
* Class OnServerManager
|
||||||
* @package Http\Service
|
* @package Http\Service
|
||||||
*/
|
*/
|
||||||
class ServerManager
|
class ServerManager extends Component
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
@@ -49,10 +43,12 @@ class ServerManager
|
|||||||
public int $port = 0;
|
public int $port = 0;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var Logger
|
||||||
|
*/
|
||||||
#[Inject(Logger::class)]
|
#[Inject(Logger::class)]
|
||||||
public Logger $logger;
|
public Logger $logger;
|
||||||
|
|
||||||
|
|
||||||
/** @var array<string,Port> */
|
/** @var array<string,Port> */
|
||||||
public array $ports = [];
|
public array $ports = [];
|
||||||
|
|
||||||
@@ -62,28 +58,19 @@ class ServerManager
|
|||||||
private Server|null $server = null;
|
private Server|null $server = null;
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var ContainerInterface
|
|
||||||
*/
|
|
||||||
#[Inject(ContainerInterface::class)]
|
|
||||||
public ContainerInterface $container;
|
|
||||||
|
|
||||||
|
|
||||||
const DEFAULT_EVENT = [
|
const DEFAULT_EVENT = [
|
||||||
Constant::WORKER_START => [OnServerWorker::class, 'onWorkerStart'],
|
Constant::WORKER_START => [OnServerWorker::class, 'onWorkerStart'],
|
||||||
Constant::WORKER_EXIT => [OnServerWorker::class, 'onWorkerExit'],
|
Constant::WORKER_EXIT => [OnServerWorker::class, 'onWorkerExit'],
|
||||||
Constant::WORKER_STOP => [OnServerWorker::class, 'onWorkerStop'],
|
Constant::WORKER_STOP => [OnServerWorker::class, 'onWorkerStop'],
|
||||||
Constant::WORKER_ERROR => [OnServerWorker::class, 'onWorkerError'],
|
Constant::WORKER_ERROR => [OnServerWorker::class, 'onWorkerError'],
|
||||||
Constant::MANAGER_START => [OnServerManager::class, 'onManagerStart'],
|
|
||||||
Constant::MANAGER_STOP => [OnServerManager::class, 'onManagerStop'],
|
|
||||||
Constant::BEFORE_RELOAD => [OnServerReload::class, 'onBeforeReload'],
|
|
||||||
Constant::AFTER_RELOAD => [OnServerReload::class, 'onAfterReload'],
|
|
||||||
Constant::START => [OnServer::class, 'onStart'],
|
Constant::START => [OnServer::class, 'onStart'],
|
||||||
Constant::BEFORE_SHUTDOWN => [OnServer::class, 'onBeforeShutdown'],
|
|
||||||
Constant::SHUTDOWN => [OnServer::class, 'onShutdown'],
|
Constant::SHUTDOWN => [OnServer::class, 'onShutdown'],
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var array|string[]
|
||||||
|
*/
|
||||||
private array $eventInterface = [
|
private array $eventInterface = [
|
||||||
OnReceiveInterface::class => 'receive',
|
OnReceiveInterface::class => 'receive',
|
||||||
OnPacketInterface::class => 'packet',
|
OnPacketInterface::class => 'packet',
|
||||||
@@ -110,9 +97,9 @@ class ServerManager
|
|||||||
* @param int $port
|
* @param int $port
|
||||||
* @param int $mode
|
* @param int $mode
|
||||||
* @param array $settings
|
* @param array $settings
|
||||||
* @throws ReflectionException
|
|
||||||
* @throws ConfigException
|
* @throws ConfigException
|
||||||
* @throws Exception
|
* @throws ContainerExceptionInterface
|
||||||
|
* @throws NotFoundExceptionInterface
|
||||||
*/
|
*/
|
||||||
public function addListener(string $type, string $host, int $port, int $mode, array $settings = [])
|
public function addListener(string $type, string $host, int $port, int $mode, array $settings = [])
|
||||||
{
|
{
|
||||||
@@ -128,8 +115,11 @@ class ServerManager
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @throws ReflectionException
|
* @param $configs
|
||||||
|
* @param int $daemon
|
||||||
* @throws ConfigException
|
* @throws ConfigException
|
||||||
|
* @throws ContainerExceptionInterface
|
||||||
|
* @throws NotFoundExceptionInterface
|
||||||
*/
|
*/
|
||||||
public function initBaseServer($configs, int $daemon = 0): void
|
public function initBaseServer($configs, int $daemon = 0): void
|
||||||
{
|
{
|
||||||
@@ -137,46 +127,25 @@ class ServerManager
|
|||||||
foreach ($this->sortService($configs['ports']) as $config) {
|
foreach ($this->sortService($configs['ports']) as $config) {
|
||||||
$this->startListenerHandler($context, $config, $daemon);
|
$this->startListenerHandler($context, $config, $daemon);
|
||||||
}
|
}
|
||||||
$this->bindCallback([Constant::PIPE_MESSAGE => [OnPipeMessage::class, 'onPipeMessage']]);
|
$this->bindPipeMessage();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return bool
|
* @return void
|
||||||
* @throws ConfigException
|
* @throws ContainerExceptionInterface
|
||||||
* @throws Exception
|
* @throws NotFoundExceptionInterface
|
||||||
*/
|
*/
|
||||||
public function isRunner(): bool
|
public function bindPipeMessage(): void
|
||||||
{
|
{
|
||||||
$configs = Config::get('server', [], true);
|
$pipeMessage = $this->getContainer()->get(OnPipeMessage::class);
|
||||||
foreach ($this->sortService($configs['ports']) as $config) {
|
$this->server->on(Constant::PIPE_MESSAGE, [$pipeMessage, 'onPipeMessage']);
|
||||||
if (checkPortIsAlready($config['port'])) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if (!isset($this->server->setting['task_worker_num']) || $this->server->setting['task_worker_num'] < 1) {
|
||||||
/**
|
return;
|
||||||
* @param string|OnProcessInterface|BaseProcess $customProcess
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public function addProcess(string|OnProcessInterface|BaseProcess $customProcess)
|
|
||||||
{
|
|
||||||
if (is_string($customProcess)) {
|
|
||||||
$customProcess = Kiri::getDi()->get($customProcess);
|
|
||||||
}
|
}
|
||||||
$system = sprintf('[%s].process', Config::get('id', 'system-service'));
|
|
||||||
$process = new Process(function (Process $process) use ($customProcess, $system) {
|
$this->getContainer()->get(TaskManager::class)->taskListener($this->server);
|
||||||
if (Kiri::getPlatform()->isLinux()) {
|
|
||||||
$process->name($system . '(' . $customProcess->getName() . ')');
|
|
||||||
}
|
|
||||||
$customProcess->process($process);
|
|
||||||
}, $customProcess->getRedirectStdinAndStdout(), $customProcess->getPipeType(), $customProcess->isEnableCoroutine());
|
|
||||||
$this->logger->debug($system . ' ' . $customProcess->getName() . ' start.');
|
|
||||||
$this->container->setBindings($customProcess->getName(), $process);
|
|
||||||
$this->server->addProcess($process);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -189,22 +158,13 @@ class ServerManager
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $key
|
|
||||||
* @param string|int $value
|
|
||||||
*/
|
|
||||||
public static function setEnv(string $key, string|int $value): void
|
|
||||||
{
|
|
||||||
putenv(sprintf('%s=%s', $key, (string)$value));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param ServerManager $context
|
* @param ServerManager $context
|
||||||
* @param array $config
|
* @param array $config
|
||||||
* @param int $daemon
|
* @param int $daemon
|
||||||
* @throws ConfigException
|
* @throws ConfigException
|
||||||
* @throws ReflectionException
|
* @throws ContainerExceptionInterface
|
||||||
|
* @throws NotFoundExceptionInterface
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
private function startListenerHandler(ServerManager $context, array $config, int $daemon = 0)
|
private function startListenerHandler(ServerManager $context, array $config, int $daemon = 0)
|
||||||
@@ -244,13 +204,16 @@ class ServerManager
|
|||||||
* @param int $port
|
* @param int $port
|
||||||
* @param int $mode
|
* @param int $mode
|
||||||
* @param array $settings
|
* @param array $settings
|
||||||
|
* @throws ConfigException
|
||||||
|
* @throws ContainerExceptionInterface
|
||||||
|
* @throws NotFoundExceptionInterface
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
private function addNewListener(string $type, string $host, int $port, int $mode, array $settings = [])
|
private function addNewListener(string $type, string $host, int $port, int $mode, array $settings = [])
|
||||||
{
|
{
|
||||||
$id = Config::get('id', 'system-service');
|
$id = Config::get('id', 'system-service');
|
||||||
|
|
||||||
$this->logger->debug(sprintf('[%s]' . $type . ' service %s::%d start', $id, $host, $port));
|
$this->logger->debug(sprintf('[%s].' . $type . ' service %s::%d start', $id, $host, $port));
|
||||||
|
|
||||||
/** @var Server\Port $service */
|
/** @var Server\Port $service */
|
||||||
$this->ports[$port] = $this->server->addlistener($host, $port, $mode);
|
$this->ports[$port] = $this->server->addlistener($host, $port, $mode);
|
||||||
@@ -277,8 +240,9 @@ class ServerManager
|
|||||||
* @param int $port
|
* @param int $port
|
||||||
* @param int $mode
|
* @param int $mode
|
||||||
* @param array $settings
|
* @param array $settings
|
||||||
* @throws ReflectionException
|
|
||||||
* @throws ConfigException
|
* @throws ConfigException
|
||||||
|
* @throws ContainerExceptionInterface
|
||||||
|
* @throws NotFoundExceptionInterface
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
private function createBaseServer(string $type, string $host, int $port, int $mode, array $settings = [])
|
private function createBaseServer(string $type, string $host, int $port, int $mode, array $settings = [])
|
||||||
@@ -292,41 +256,23 @@ class ServerManager
|
|||||||
$this->server = new $match($host, $port, SWOOLE_PROCESS, $mode);
|
$this->server = new $match($host, $port, SWOOLE_PROCESS, $mode);
|
||||||
$this->server->set(array_merge(Config::get('server.settings', []), $settings['settings']));
|
$this->server->set(array_merge(Config::get('server.settings', []), $settings['settings']));
|
||||||
|
|
||||||
|
$this->getContainer()->setBindings(SwooleServerInterface::class, $this->server);
|
||||||
|
|
||||||
$id = Config::get('id', 'system-service');
|
$id = Config::get('id', 'system-service');
|
||||||
|
|
||||||
$this->logger->debug(sprintf('[%s]' . $type . ' service %s::%d start', $id, $host, $port));
|
$this->logger->debug(sprintf('[%s].' . $type . ' service %s::%d start', $id, $host, $port));
|
||||||
|
|
||||||
$this->addDefaultListener($settings);
|
$this->addDefaultListener($settings);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param int $port
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public function stopServer(int $port)
|
|
||||||
{
|
|
||||||
if (!($pid = checkPortIsAlready($port))) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
while (checkPortIsAlready($port)) {
|
|
||||||
Process::kill($pid, SIGTERM);
|
|
||||||
usleep(300);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $settings
|
* @param array $settings
|
||||||
* @throws ReflectionException
|
* @throws ContainerExceptionInterface
|
||||||
* @throws Exception
|
* @throws NotFoundExceptionInterface
|
||||||
*/
|
*/
|
||||||
private function addDefaultListener(array $settings): void
|
private function addDefaultListener(array $settings): void
|
||||||
{
|
{
|
||||||
if (($this->server->setting['task_worker_num'] ?? 0) > 0) {
|
|
||||||
$this->addTaskListener($settings['events']);
|
|
||||||
}
|
|
||||||
$this->container->setBindings(SwooleServerInterface::class, $this->server);
|
|
||||||
$this->addServiceEvents(ServerManager::DEFAULT_EVENT, $this->server);
|
$this->addServiceEvents(ServerManager::DEFAULT_EVENT, $this->server);
|
||||||
if (!empty($settings['events']) && is_array($settings['events'])) {
|
if (!empty($settings['events']) && is_array($settings['events'])) {
|
||||||
$this->addServiceEvents($settings['events'], $this->server);
|
$this->addServiceEvents($settings['events'], $this->server);
|
||||||
@@ -337,12 +283,14 @@ class ServerManager
|
|||||||
/**
|
/**
|
||||||
* @param array $events
|
* @param array $events
|
||||||
* @param Server|Port $server
|
* @param Server|Port $server
|
||||||
|
* @throws ContainerExceptionInterface
|
||||||
|
* @throws NotFoundExceptionInterface
|
||||||
*/
|
*/
|
||||||
private function addServiceEvents(array $events, Server|Port $server)
|
private function addServiceEvents(array $events, Server|Port $server)
|
||||||
{
|
{
|
||||||
foreach ($events as $name => $event) {
|
foreach ($events as $name => $event) {
|
||||||
if (is_array($event) && is_string($event[0])) {
|
if (is_array($event) && is_string($event[0])) {
|
||||||
$event[0] = $this->container->get($event[0]);
|
$event[0] = $this->getContainer()->get($event[0]);
|
||||||
}
|
}
|
||||||
$server->on($name, $event);
|
$server->on($name, $event);
|
||||||
}
|
}
|
||||||
@@ -358,85 +306,4 @@ class ServerManager
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $class
|
|
||||||
* @return object
|
|
||||||
*/
|
|
||||||
private function getNewInstance(string $class): object
|
|
||||||
{
|
|
||||||
return $this->container->create($class);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param OnTaskInterface|string $handler
|
|
||||||
* @param array $params
|
|
||||||
* @param int|null $workerId
|
|
||||||
* @throws ReflectionException
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public function task(OnTaskInterface|string $handler, array $params = [], int $workerId = null)
|
|
||||||
{
|
|
||||||
if ($workerId === null || $workerId <= $this->server->setting['worker_num']) {
|
|
||||||
$workerId = random_int($this->server->setting['worker_num'] + 1,
|
|
||||||
$this->server->setting['worker_num'] + 1 + $this->server->setting['task_worker_num']);
|
|
||||||
}
|
|
||||||
if (is_string($handler)) {
|
|
||||||
$implements = $this->container->getReflect($handler);
|
|
||||||
if (!in_array(OnTaskInterface::class, $implements->getInterfaceNames())) {
|
|
||||||
throw new Exception('Task must instance ' . OnTaskInterface::class);
|
|
||||||
}
|
|
||||||
$handler = $implements->newInstanceArgs($params);
|
|
||||||
}
|
|
||||||
$this->server->task(serialize($handler), $workerId);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param mixed $message
|
|
||||||
* @param int $workerId
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function sendMessage(mixed $message, int $workerId): mixed
|
|
||||||
{
|
|
||||||
return $this->server?->sendMessage($message, $workerId);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param array $events
|
|
||||||
* @throws ReflectionException
|
|
||||||
*/
|
|
||||||
private function addTaskListener(array $events = []): void
|
|
||||||
{
|
|
||||||
$task_use_object = $this->server->setting['task_object'] ?? $this->server->setting['task_use_object'] ?? false;
|
|
||||||
$reflect = $this->container->getReflect(OnServerTask::class)?->newInstance();
|
|
||||||
$this->server->on('finish', $events[Constant::FINISH] ?? [$reflect, 'onFinish']);
|
|
||||||
if ($task_use_object || $this->server->setting['task_enable_coroutine']) {
|
|
||||||
$this->server->on('task', $events[Constant::TASK] ?? [$reflect, 'onCoroutineTask']);
|
|
||||||
} else {
|
|
||||||
$this->server->on('task', $events[Constant::TASK] ?? [$reflect, 'onTask']);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param array|null $settings
|
|
||||||
*/
|
|
||||||
public function bindCallback(?array $settings = [])
|
|
||||||
{
|
|
||||||
// TODO: Implement bindCallback() method.
|
|
||||||
if (count($settings) < 1) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
foreach ($settings as $event_type => $callback) {
|
|
||||||
if ($this->server->getCallback($event_type) !== null) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (is_array($callback) && !is_object($callback[0])) {
|
|
||||||
$callback[0] = $this->container->get($callback[0]);
|
|
||||||
}
|
|
||||||
$this->server->on($event_type, $callback);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-4
@@ -1,13 +1,13 @@
|
|||||||
<?php
|
<?php
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Server;
|
namespace Kiri\Server;
|
||||||
|
|
||||||
|
|
||||||
use Exception;
|
use Exception;
|
||||||
use Kiri\Abstracts\Providers;
|
use Kiri\Abstracts\Providers;
|
||||||
use Kiri\Application;
|
use Kiri\Application;
|
||||||
use Kiri\Kiri;
|
use Kiri;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class DatabasesProviders
|
* Class DatabasesProviders
|
||||||
@@ -23,8 +23,6 @@ class ServerProviders extends Providers
|
|||||||
*/
|
*/
|
||||||
public function onImport(Application $application)
|
public function onImport(Application $application)
|
||||||
{
|
{
|
||||||
$application->set('server', ['class' => Server::class]);
|
|
||||||
|
|
||||||
$container = Kiri::getDi();
|
$container = Kiri::getDi();
|
||||||
|
|
||||||
$console = $container->get(\Symfony\Component\Console\Application::class);
|
$console = $container->get(\Symfony\Component\Console\Application::class);
|
||||||
|
|||||||
@@ -0,0 +1,56 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Kiri\Server;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
use Kiri\Abstracts\Component;
|
||||||
|
use Kiri\Abstracts\Config;
|
||||||
|
use Swoole\Process;
|
||||||
|
|
||||||
|
class State extends Component
|
||||||
|
{
|
||||||
|
|
||||||
|
use TraitServer;
|
||||||
|
|
||||||
|
|
||||||
|
public array $servers = [];
|
||||||
|
|
||||||
|
|
||||||
|
public function init()
|
||||||
|
{
|
||||||
|
$this->servers = Config::get('server.ports');
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return bool
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function isRunner(): bool
|
||||||
|
{
|
||||||
|
$ports = $this->sortService($this->servers);
|
||||||
|
foreach ($ports as $config) {
|
||||||
|
if (checkPortIsAlready($config['port'])) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $port
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function exit($port)
|
||||||
|
{
|
||||||
|
if (!($pid = checkPortIsAlready($port))) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
while (checkPortIsAlready($port)) {
|
||||||
|
Process::kill($pid, SIGTERM);
|
||||||
|
usleep(300);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Server;
|
namespace Kiri\Server;
|
||||||
|
|
||||||
|
|
||||||
use Swoole\Server;
|
use Swoole\Server;
|
||||||
|
|||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Server;
|
namespace Kiri\Server;
|
||||||
|
|
||||||
trait TraitServer
|
trait TraitServer
|
||||||
{
|
{
|
||||||
|
|||||||
+2
-2
@@ -15,11 +15,11 @@
|
|||||||
"psr/http-server-middleware": "^1.0",
|
"psr/http-server-middleware": "^1.0",
|
||||||
"psr/http-message": "^1.0",
|
"psr/http-message": "^1.0",
|
||||||
"psr/event-dispatcher": "^1.0",
|
"psr/event-dispatcher": "^1.0",
|
||||||
"game-worker/kiri-http-message": "^v1.2"
|
"game-worker/kiri-http-message": "~v2.0"
|
||||||
},
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-4": {
|
"psr-4": {
|
||||||
"Server\\": "./"
|
"Kiri\\Server\\": "./"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user