Compare commits
40 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 |
@@ -1,10 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Abstracts;
|
||||
namespace Kiri\Server\Abstracts;
|
||||
|
||||
|
||||
use Kiri\Context;
|
||||
use Server\Contract\OnProcessInterface;
|
||||
use Kiri\Server\Broadcast\OnBroadcastInterface;
|
||||
use Kiri\Server\Contract\OnProcessInterface;
|
||||
use Swoole\Coroutine;
|
||||
use Swoole\Process;
|
||||
|
||||
@@ -48,9 +49,9 @@ abstract class BaseProcess implements OnProcessInterface
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
*/
|
||||
public function getRedirectStdinAndStdout(): bool
|
||||
{
|
||||
{
|
||||
return $this->redirect_stdin_and_stdout;
|
||||
}
|
||||
|
||||
@@ -105,6 +106,10 @@ abstract class BaseProcess implements OnProcessInterface
|
||||
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
|
||||
|
||||
|
||||
namespace Server\Abstracts;
|
||||
namespace Kiri\Server\Abstracts;
|
||||
|
||||
|
||||
use Kiri\Annotation\Inject;
|
||||
use Exception;
|
||||
use Kiri\Abstracts\Config;
|
||||
use Kiri\Exception\ConfigException;
|
||||
use Kiri\Kiri;
|
||||
use Kiri;
|
||||
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
|
||||
|
||||
|
||||
namespace Server;
|
||||
namespace Kiri\Server;
|
||||
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Contract;
|
||||
namespace Kiri\Server\Contract;
|
||||
|
||||
interface OnBeforeShutdown
|
||||
{
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Contract;
|
||||
namespace Kiri\Server\Contract;
|
||||
|
||||
use Swoole\Server;
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Contract;
|
||||
namespace Kiri\Server\Contract;
|
||||
|
||||
use Swoole\Server;
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Contract;
|
||||
namespace Kiri\Server\Contract;
|
||||
|
||||
use Swoole\Server;
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Contract;
|
||||
namespace Kiri\Server\Contract;
|
||||
|
||||
use Swoole\Http\Request;
|
||||
use Swoole\Http\Response;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Contract;
|
||||
namespace Kiri\Server\Contract;
|
||||
|
||||
use Kiri\Websocket\WebSocketInterface;
|
||||
use Swoole\WebSocket\Frame;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Contract;
|
||||
namespace Kiri\Server\Contract;
|
||||
|
||||
use Swoole\Http\Request;
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Contract;
|
||||
namespace Kiri\Server\Contract;
|
||||
|
||||
use Server\Abstracts\Server;
|
||||
use Kiri\Server\Abstracts\Server;
|
||||
|
||||
interface OnPacketInterface
|
||||
{
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Server\Contract;
|
||||
namespace Kiri\Server\Contract;
|
||||
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Server\Contract;
|
||||
namespace Kiri\Server\Contract;
|
||||
|
||||
|
||||
use Swoole\Process;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Contract;
|
||||
namespace Kiri\Server\Contract;
|
||||
|
||||
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
|
||||
|
||||
namespace Server\Events;
|
||||
namespace Kiri\Server\Events;
|
||||
|
||||
use Swoole\Server;
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Events;
|
||||
namespace Kiri\Server\Events;
|
||||
|
||||
class OnAfterWorkerStart
|
||||
{
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Events;
|
||||
namespace Kiri\Server\Events;
|
||||
|
||||
use Swoole\Server;
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Events;
|
||||
namespace Kiri\Server\Events;
|
||||
|
||||
use Swoole\Server;
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Events;
|
||||
namespace Kiri\Server\Events;
|
||||
|
||||
class OnBeforeWorkerStart
|
||||
{
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Events;
|
||||
namespace Kiri\Server\Events;
|
||||
|
||||
use Swoole\Server;
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Events;
|
||||
namespace Kiri\Server\Events;
|
||||
|
||||
use Swoole\Server;
|
||||
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Server\Events;
|
||||
|
||||
class OnProcessStart
|
||||
{
|
||||
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Events;
|
||||
namespace Kiri\Server\Events;
|
||||
|
||||
class OnServerBeforeStart
|
||||
{
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Events;
|
||||
namespace Kiri\Server\Events;
|
||||
|
||||
use Swoole\Server;
|
||||
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Events;
|
||||
namespace Kiri\Server\Events;
|
||||
|
||||
use Swoole\Server;
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Events;
|
||||
namespace Kiri\Server\Events;
|
||||
|
||||
use Swoole\Server;
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Events;
|
||||
namespace Kiri\Server\Events;
|
||||
|
||||
use Swoole\Server;
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Events;
|
||||
namespace Kiri\Server\Events;
|
||||
|
||||
use Swoole\Server;
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Events;
|
||||
namespace Kiri\Server\Events;
|
||||
|
||||
use Swoole\Server;
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Events;
|
||||
namespace Kiri\Server\Events;
|
||||
|
||||
use Swoole\Server;
|
||||
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Handler;
|
||||
namespace Kiri\Server\Handler;
|
||||
|
||||
use Kiri\Annotation\Inject;
|
||||
use Server\Abstracts\Server;
|
||||
use Exception;
|
||||
use Server\Contract\OnPipeMessageInterface;
|
||||
use Kiri\Annotation\Inject;
|
||||
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
|
||||
@@ -28,6 +24,9 @@ class OnPipeMessage extends Server
|
||||
*/
|
||||
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)) {
|
||||
return;
|
||||
}
|
||||
|
||||
+9
-15
@@ -1,15 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Handler;
|
||||
namespace Kiri\Server\Handler;
|
||||
|
||||
use Kiri\Annotation\Inject;
|
||||
use Kiri\Events\EventDispatch;
|
||||
use Kiri\Exception\ConfigException;
|
||||
use ReflectionException;
|
||||
use Server\Abstracts\Server;
|
||||
use Server\Events\OnBeforeShutdown;
|
||||
use Server\Events\OnShutdown;
|
||||
use Server\Events\OnStart;
|
||||
use Kiri\Server\Abstracts\Server;
|
||||
use Kiri\Server\Events\OnBeforeShutdown;
|
||||
use Kiri\Server\Events\OnShutdown;
|
||||
use Kiri\Server\Events\OnStart;
|
||||
|
||||
|
||||
/**
|
||||
@@ -18,13 +18,7 @@ use Server\Events\OnStart;
|
||||
*/
|
||||
class OnServer extends Server
|
||||
{
|
||||
|
||||
/**
|
||||
* @var EventDispatch
|
||||
*/
|
||||
#[Inject(EventDispatch::class)]
|
||||
public EventDispatch $eventDispatch;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @param \Swoole\Server $server
|
||||
@@ -35,7 +29,7 @@ class OnServer extends Server
|
||||
{
|
||||
$this->setProcessName(sprintf('start[%d].server', $server->master_pid));
|
||||
|
||||
$this->eventDispatch->dispatch(new OnStart($server));
|
||||
\Kiri::getDi()->get(EventDispatch::class)->dispatch(new OnStart($server));
|
||||
}
|
||||
|
||||
|
||||
@@ -45,7 +39,7 @@ class OnServer extends Server
|
||||
*/
|
||||
public function onBeforeShutdown(\Swoole\Server $server)
|
||||
{
|
||||
$this->eventDispatch->dispatch(new OnBeforeShutdown($server));
|
||||
\Kiri::getDi()->get(EventDispatch::class)->dispatch(new OnBeforeShutdown($server));
|
||||
}
|
||||
|
||||
|
||||
@@ -55,7 +49,7 @@ class OnServer extends Server
|
||||
*/
|
||||
public function onShutdown(\Swoole\Server $server)
|
||||
{
|
||||
$this->eventDispatch->dispatch(new OnShutdown($server));
|
||||
\Kiri::getDi()->get(EventDispatch::class)->dispatch(new OnShutdown($server));
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Handler;
|
||||
namespace Kiri\Server\Handler;
|
||||
|
||||
use Kiri\Annotation\Inject;
|
||||
use Kiri\Events\EventDispatch;
|
||||
use ReflectionException;
|
||||
use Server\Abstracts\Server;
|
||||
use Kiri\Server\Abstracts\Server;
|
||||
use Kiri\Exception\ConfigException;
|
||||
use Server\Events\OnManagerStart;
|
||||
use Server\Events\OnManagerStop;
|
||||
use Kiri\Server\Events\OnManagerStart;
|
||||
use Kiri\Server\Events\OnManagerStop;
|
||||
|
||||
|
||||
/**
|
||||
@@ -18,12 +18,6 @@ use Server\Events\OnManagerStop;
|
||||
class OnServerManager extends Server
|
||||
{
|
||||
|
||||
/**
|
||||
* @var EventDispatch
|
||||
*/
|
||||
#[Inject(EventDispatch::class)]
|
||||
public EventDispatch $eventDispatch;
|
||||
|
||||
|
||||
/**
|
||||
* @param \Swoole\Server $server
|
||||
@@ -33,7 +27,7 @@ class OnServerManager extends Server
|
||||
{
|
||||
$this->setProcessName(sprintf('manger[%d].0', $server->manager_pid));
|
||||
|
||||
$this->eventDispatch->dispatch(new OnManagerStart($server));
|
||||
\Kiri::getDi()->get(EventDispatch::class)->dispatch(new OnManagerStart($server));
|
||||
}
|
||||
|
||||
|
||||
@@ -43,7 +37,7 @@ class OnServerManager extends 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
|
||||
|
||||
namespace Server\Handler;
|
||||
namespace Kiri\Server\Handler;
|
||||
|
||||
use Kiri\Annotation\Inject;
|
||||
use Kiri\Events\EventDispatch;
|
||||
use Server\Events\OnAfterReload;
|
||||
use Server\Events\OnBeforeReload;
|
||||
use Kiri\Server\Events\OnAfterReload;
|
||||
use Kiri\Server\Events\OnBeforeReload;
|
||||
use Swoole\Server;
|
||||
|
||||
|
||||
@@ -16,20 +16,13 @@ class OnServerReload
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @var EventDispatch
|
||||
*/
|
||||
#[Inject(EventDispatch::class)]
|
||||
public EventDispatch $eventDispatch;
|
||||
|
||||
|
||||
/**
|
||||
* @param Server $server
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
public function onBeforeReload(Server $server)
|
||||
{
|
||||
$this->eventDispatch->dispatch(new OnBeforeReload($server));
|
||||
\Kiri::getDi()->get(EventDispatch::class)->dispatch(new OnBeforeReload($server));
|
||||
}
|
||||
|
||||
|
||||
@@ -39,7 +32,7 @@ class OnServerReload
|
||||
*/
|
||||
public function onAfterReload(Server $server)
|
||||
{
|
||||
$this->eventDispatch->dispatch(new OnAfterReload($server));
|
||||
\Kiri::getDi()->get(EventDispatch::class)->dispatch(new OnAfterReload($server));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+30
-23
@@ -1,20 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Handler;
|
||||
namespace Kiri\Server\Handler;
|
||||
|
||||
use Exception;
|
||||
use Kiri;
|
||||
use Kiri\Abstracts\Config;
|
||||
use Kiri\Annotation\Inject;
|
||||
use Kiri\Core\Help;
|
||||
use Kiri\Events\EventDispatch;
|
||||
use Kiri\Kiri;
|
||||
use Kiri\Annotation\Inject;
|
||||
use Server\Events\OnAfterWorkerStart;
|
||||
use Server\Events\OnBeforeWorkerStart;
|
||||
use Server\Events\OnTaskerStart as OnTaskStart;
|
||||
use Server\Events\OnWorkerError;
|
||||
use Server\Events\OnWorkerExit;
|
||||
use Server\Events\OnWorkerStart;
|
||||
use Server\Events\OnWorkerStop;
|
||||
use Kiri\Message\Handler\Router;
|
||||
use Kiri\Server\Events\OnAfterWorkerStart;
|
||||
use Kiri\Server\Events\OnBeforeWorkerStart;
|
||||
use Kiri\Server\Events\OnTaskerStart as OnTaskStart;
|
||||
use Kiri\Server\Events\OnWorkerError;
|
||||
use Kiri\Server\Events\OnWorkerExit;
|
||||
use Kiri\Server\Events\OnWorkerStart;
|
||||
use Kiri\Server\Events\OnWorkerStop;
|
||||
use Swoole\Server;
|
||||
use Swoole\Timer;
|
||||
|
||||
@@ -23,15 +24,20 @@ use Swoole\Timer;
|
||||
* Class OnServerWorker
|
||||
* @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 EventDispatch $eventDispatch;
|
||||
public function init()
|
||||
{
|
||||
$this->collector = Kiri::getDi()->get(Router::class);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
@@ -41,17 +47,20 @@ class OnServerWorker extends \Server\Abstracts\Server
|
||||
*/
|
||||
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']) {
|
||||
$this->eventDispatch->dispatch(new OnWorkerStart($server, $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);
|
||||
} else {
|
||||
$this->eventDispatch->dispatch(new OnTaskStart($server, $workerId));
|
||||
$dispatch->dispatch(new OnTaskStart($server, $workerId));
|
||||
$this->setProcessName(sprintf('Tasker[%d].%d', $server->worker_pid, $workerId));
|
||||
set_env('environmental', Kiri::TASK);
|
||||
}
|
||||
$this->eventDispatch->dispatch(new OnAfterWorkerStart());
|
||||
$dispatch->dispatch(new OnAfterWorkerStart());
|
||||
}
|
||||
|
||||
|
||||
@@ -63,7 +72,7 @@ class OnServerWorker extends \Server\Abstracts\Server
|
||||
public function onWorkerStop(Server $server, int $workerId)
|
||||
{
|
||||
Timer::clearAll();
|
||||
$this->eventDispatch->dispatch(new OnWorkerStop($server, $workerId));
|
||||
\Kiri::getDi()->get(EventDispatch::class)->dispatch(new OnWorkerStop($server, $workerId));
|
||||
}
|
||||
|
||||
|
||||
@@ -74,9 +83,7 @@ class OnServerWorker extends \Server\Abstracts\Server
|
||||
*/
|
||||
public function onWorkerExit(Server $server, int $workerId)
|
||||
{
|
||||
set_env('state', 'exit');
|
||||
|
||||
$this->eventDispatch->dispatch(new OnWorkerExit($server, $workerId));
|
||||
\Kiri::getDi()->get(EventDispatch::class)->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)
|
||||
{
|
||||
$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',
|
||||
$worker_id, $worker_pid, $signal, $exit_code, swoole_strerror(swoole_last_error(), 9)
|
||||
|
||||
@@ -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)));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
+39
-28
@@ -1,18 +1,22 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Server;
|
||||
namespace Kiri\Server;
|
||||
|
||||
use Exception;
|
||||
use Http\Handler\Abstracts\HttpService;
|
||||
use JetBrains\PhpStorm\Pure;
|
||||
use Kiri;
|
||||
use Kiri\Abstracts\Config;
|
||||
use Kiri\Annotation\Inject;
|
||||
use Kiri\Events\EventDispatch;
|
||||
use Kiri\Exception\ConfigException;
|
||||
use Kiri\Kiri;
|
||||
use Kiri\Annotation\Inject;
|
||||
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 Server\Events\OnShutdown;
|
||||
use ReflectionException;
|
||||
use Swoole\Coroutine;
|
||||
|
||||
|
||||
defined('PID_PATH') or define('PID_PATH', APP_PATH . 'storage/server.pid');
|
||||
@@ -38,11 +42,27 @@ class Server extends HttpService
|
||||
public State $state;
|
||||
|
||||
|
||||
public ServerManager $manager;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
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;
|
||||
}
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
@@ -60,25 +80,26 @@ class Server extends HttpService
|
||||
* @throws ConfigException
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
* @throws \ReflectionException
|
||||
* @throws ReflectionException
|
||||
* @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);
|
||||
|
||||
$rpcService = Config::get('rpc', []);
|
||||
if (!empty($rpcService)) {
|
||||
$this->manager()->addListener($rpcService['type'], $rpcService['host'], $rpcService['port'],
|
||||
$this->manager->addListener($rpcService['type'], $rpcService['host'], $rpcService['port'],
|
||||
$rpcService['mode'], $rpcService);
|
||||
}
|
||||
|
||||
$processes = array_merge($this->process, Config::get('processes', []));
|
||||
foreach ($processes as $process) {
|
||||
$this->manager()->addProcess($process);
|
||||
}
|
||||
|
||||
return $this->manager()->getServer()->start();
|
||||
$this->getContainer()->get(ProcessManager::class)->batch($processes);
|
||||
|
||||
$this->getEventDispatch()->dispatch(new OnServerBeforeStart());
|
||||
|
||||
return $this->manager->getServer()->start();
|
||||
}
|
||||
|
||||
|
||||
@@ -87,16 +108,16 @@ class Server extends HttpService
|
||||
* @throws ConfigException
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
* @throws \ReflectionException
|
||||
* @throws ReflectionException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function shutdown()
|
||||
{
|
||||
$configs = Config::get('server', [], true);
|
||||
foreach ($this->manager()->sortService($configs['ports'] ?? []) as $config) {
|
||||
foreach ($this->manager->sortService($configs['ports'] ?? []) as $config) {
|
||||
$this->state->exit($config['port']);
|
||||
}
|
||||
$this->container->get(EventDispatch::class)->dispatch(new OnShutdown());
|
||||
$this->getContainer()->get(EventDispatch::class)->dispatch(new OnShutdown());
|
||||
}
|
||||
|
||||
|
||||
@@ -127,20 +148,10 @@ class Server extends HttpService
|
||||
|
||||
/**
|
||||
* @return \Swoole\Http\Server|\Swoole\Server|\Swoole\WebSocket\Server|null
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
public function getServer(): \Swoole\Http\Server|\Swoole\Server|\Swoole\WebSocket\Server|null
|
||||
#[Pure] public function getServer(): \Swoole\Http\Server|\Swoole\Server|\Swoole\WebSocket\Server|null
|
||||
{
|
||||
return $this->manager()->getServer();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return ServerManager
|
||||
*/
|
||||
private function manager(): ServerManager
|
||||
{
|
||||
return Kiri::getDi()->get(ServerManager::class);
|
||||
return $this->manager->getServer();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+11
-54
@@ -1,14 +1,14 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Server;
|
||||
namespace Kiri\Server;
|
||||
|
||||
|
||||
use Exception;
|
||||
use Kiri\Abstracts\Config;
|
||||
use Kiri\Events\EventDispatch;
|
||||
use Kiri\Exception\ConfigException;
|
||||
use Kiri\Kiri;
|
||||
use Kiri;
|
||||
use Kiri\Annotation\Inject;
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
@@ -30,12 +30,6 @@ class ServerCommand extends Command
|
||||
const ACTIONS = ['start', 'stop', 'restart'];
|
||||
|
||||
|
||||
/**
|
||||
* @var EventDispatch
|
||||
*/
|
||||
#[Inject(EventDispatch::class)]
|
||||
public EventDispatch $eventProvider;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -63,58 +57,21 @@ class ServerCommand extends Command
|
||||
{
|
||||
$manager = Kiri::app()->getServer();
|
||||
$manager->setDaemon((int)!is_null($input->getOption('daemon')));
|
||||
if (is_null($input->getArgument('action'))) {
|
||||
|
||||
$action = $input->getArgument('action');
|
||||
if (is_null($action)) {
|
||||
throw new Exception('I don\'t know what I want to do.');
|
||||
}
|
||||
if (!in_array($input->getArgument('action'), self::ACTIONS)) {
|
||||
if (!in_array($action, self::ACTIONS)) {
|
||||
throw new Exception('I don\'t know what I want to do.');
|
||||
}
|
||||
if ($input->getArgument('action') == 'restart') {
|
||||
if ($action == 'restart' || $action == 'stop') {
|
||||
$manager->shutdown();
|
||||
if ($action == 'stop') {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
if ($input->getArgument('action') != 'stop') {
|
||||
return $this->generate_runtime_builder($manager);
|
||||
}
|
||||
$manager->shutdown();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @throws ConfigException
|
||||
*/
|
||||
private function configure_set()
|
||||
{
|
||||
$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;
|
||||
}
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @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;
|
||||
return (int)$manager->start();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+30
-133
@@ -1,43 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace Server;
|
||||
namespace Kiri\Server;
|
||||
|
||||
use Exception;
|
||||
use Kiri\Abstracts\Component;
|
||||
use Kiri\Abstracts\Config;
|
||||
use Kiri\Error\Logger;
|
||||
use Kiri\Events\EventDispatch;
|
||||
use Kiri\Exception\ConfigException;
|
||||
use Kiri\Kiri;
|
||||
use Kiri\Annotation\Inject;
|
||||
use Kiri\Error\Logger;
|
||||
use Kiri\Exception\ConfigException;
|
||||
use Kiri\Server\Contract\OnCloseInterface;
|
||||
use Kiri\Server\Contract\OnConnectInterface;
|
||||
use Kiri\Server\Contract\OnDisconnectInterface;
|
||||
use Kiri\Server\Contract\OnHandshakeInterface;
|
||||
use Kiri\Server\Contract\OnMessageInterface;
|
||||
use Kiri\Server\Contract\OnPacketInterface;
|
||||
use Kiri\Server\Contract\OnReceiveInterface;
|
||||
use Kiri\Server\Handler\OnPipeMessage;
|
||||
use Kiri\Server\Handler\OnServer;
|
||||
use Kiri\Server\Handler\OnServerWorker;
|
||||
use Kiri\Task\TaskManager;
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
use ReflectionException;
|
||||
use Server\Abstracts\BaseProcess;
|
||||
use Server\Contract\OnCloseInterface;
|
||||
use Server\Contract\OnConnectInterface;
|
||||
use Server\Contract\OnDisconnectInterface;
|
||||
use Server\Contract\OnHandshakeInterface;
|
||||
use Server\Contract\OnMessageInterface;
|
||||
use Server\Contract\OnPacketInterface;
|
||||
use Server\Contract\OnProcessInterface;
|
||||
use Server\Contract\OnReceiveInterface;
|
||||
use Server\Events\OnServerBeforeStart;
|
||||
use Server\Handler\OnPipeMessage;
|
||||
use Server\Handler\OnServer;
|
||||
use Server\Handler\OnServerManager;
|
||||
use Server\Handler\OnServerReload;
|
||||
use Server\Handler\OnServerWorker;
|
||||
use Server\Tasker\OnServerTask;
|
||||
use Swoole\Http\Server as HServer;
|
||||
use Swoole\Process;
|
||||
use Swoole\Server;
|
||||
use Swoole\Server\Port;
|
||||
use Swoole\WebSocket\Server as WServer;
|
||||
use Symfony\Component\Console\Helper\Table;
|
||||
use Symfony\Component\Console\Helper\TableSeparator;
|
||||
use Symfony\Component\Console\Output\ConsoleOutput;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
|
||||
/**
|
||||
@@ -71,24 +58,19 @@ class ServerManager extends Component
|
||||
private Server|null $server = null;
|
||||
|
||||
|
||||
protected array $initProcesses = [];
|
||||
|
||||
|
||||
const DEFAULT_EVENT = [
|
||||
Constant::WORKER_START => [OnServerWorker::class, 'onWorkerStart'],
|
||||
Constant::WORKER_EXIT => [OnServerWorker::class, 'onWorkerExit'],
|
||||
Constant::WORKER_STOP => [OnServerWorker::class, 'onWorkerStop'],
|
||||
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::BEFORE_SHUTDOWN => [OnServer::class, 'onBeforeShutdown'],
|
||||
Constant::SHUTDOWN => [OnServer::class, 'onShutdown'],
|
||||
];
|
||||
|
||||
|
||||
/**
|
||||
* @var array|string[]
|
||||
*/
|
||||
private array $eventInterface = [
|
||||
OnReceiveInterface::class => 'receive',
|
||||
OnPacketInterface::class => 'packet',
|
||||
@@ -102,11 +84,9 @@ class ServerManager extends Component
|
||||
|
||||
/**
|
||||
* @return Server|WServer|HServer|null
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
public function getServer(): Server|WServer|HServer|null
|
||||
{
|
||||
di(EventDispatch::class)->dispatch(new OnServerBeforeStart());
|
||||
return $this->server;
|
||||
}
|
||||
|
||||
@@ -147,40 +127,25 @@ class ServerManager extends Component
|
||||
foreach ($this->sortService($configs['ports']) as $config) {
|
||||
$this->startListenerHandler($context, $config, $daemon);
|
||||
}
|
||||
$this->bindCallback([Constant::PIPE_MESSAGE => [OnPipeMessage::class, 'onPipeMessage']]);
|
||||
$this->bindPipeMessage();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string|OnProcessInterface|BaseProcess $customProcess
|
||||
* @throws Exception
|
||||
* @return void
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
public function addProcess(string|OnProcessInterface|BaseProcess $customProcess)
|
||||
public function bindPipeMessage(): void
|
||||
{
|
||||
if (is_string($customProcess)) {
|
||||
$customProcess = Kiri::getDi()->get($customProcess);
|
||||
$pipeMessage = $this->getContainer()->get(OnPipeMessage::class);
|
||||
$this->server->on(Constant::PIPE_MESSAGE, [$pipeMessage, 'onPipeMessage']);
|
||||
|
||||
if (!isset($this->server->setting['task_worker_num']) || $this->server->setting['task_worker_num'] < 1) {
|
||||
return;
|
||||
}
|
||||
$system = sprintf('[%s].process', Config::get('id', 'system-service'));
|
||||
$this->logger->debug($system . ' ' . $customProcess->getName() . ' start.');
|
||||
$this->server->addProcess(new Process(function (Process $process) use ($customProcess, $system) {
|
||||
if (Kiri::getPlatform()->isLinux()) {
|
||||
$process->name($system . '(' . $customProcess->getName() . ')');
|
||||
}
|
||||
$customProcess->onSigterm()->process($process);
|
||||
},
|
||||
$customProcess->getRedirectStdinAndStdout(),
|
||||
$customProcess->getPipeType(),
|
||||
$customProcess->isEnableCoroutine()
|
||||
));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return array<string,Process>
|
||||
*/
|
||||
public function getProcesses(): array
|
||||
{
|
||||
return $this->initProcesses;
|
||||
$this->getContainer()->get(TaskManager::class)->taskListener($this->server);
|
||||
}
|
||||
|
||||
|
||||
@@ -291,20 +256,7 @@ class ServerManager extends Component
|
||||
$this->server = new $match($host, $port, SWOOLE_PROCESS, $mode);
|
||||
$this->server->set(array_merge(Config::get('server.settings', []), $settings['settings']));
|
||||
|
||||
$data = new Table($this->container->get(OutputInterface::class));
|
||||
$data->setHeaders(['key', 'value']);
|
||||
|
||||
$array = [];
|
||||
foreach ($this->server->setting as $key => $value) {
|
||||
$array[] = [$key, $value];
|
||||
$array[] = new TableSeparator();
|
||||
}
|
||||
|
||||
array_pop($array);
|
||||
|
||||
$data->setStyle('box-double');
|
||||
$data->setRows($array);
|
||||
$data->render();
|
||||
$this->getContainer()->setBindings(SwooleServerInterface::class, $this->server);
|
||||
|
||||
$id = Config::get('id', 'system-service');
|
||||
|
||||
@@ -321,10 +273,6 @@ class ServerManager extends Component
|
||||
*/
|
||||
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);
|
||||
if (!empty($settings['events']) && is_array($settings['events'])) {
|
||||
$this->addServiceEvents($settings['events'], $this->server);
|
||||
@@ -342,7 +290,7 @@ class ServerManager extends Component
|
||||
{
|
||||
foreach ($events as $name => $event) {
|
||||
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);
|
||||
}
|
||||
@@ -358,55 +306,4 @@ class ServerManager extends Component
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @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
|
||||
* @return void
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
private function addTaskListener(array $events = []): void
|
||||
{
|
||||
$task_use_object = $this->server->setting['task_object'] ?? $this->server->setting['task_use_object'] ?? false;
|
||||
$reflect = $this->container->get(OnServerTask::class);
|
||||
$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
|
||||
* @return void
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
public function bindCallback(?array $settings = [])
|
||||
{
|
||||
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
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Server;
|
||||
namespace Kiri\Server;
|
||||
|
||||
|
||||
use Exception;
|
||||
use Kiri\Abstracts\Providers;
|
||||
use Kiri\Application;
|
||||
use Kiri\Kiri;
|
||||
use Kiri;
|
||||
|
||||
/**
|
||||
* Class DatabasesProviders
|
||||
@@ -23,8 +23,6 @@ class ServerProviders extends Providers
|
||||
*/
|
||||
public function onImport(Application $application)
|
||||
{
|
||||
$application->set('server', ['class' => Server::class]);
|
||||
|
||||
$container = Kiri::getDi();
|
||||
|
||||
$console = $container->get(\Symfony\Component\Console\Application::class);
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace Server;
|
||||
namespace Kiri\Server;
|
||||
|
||||
use Exception;
|
||||
use Kiri\Abstracts\Component;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace Server;
|
||||
namespace Kiri\Server;
|
||||
|
||||
|
||||
use Swoole\Server;
|
||||
|
||||
@@ -1,92 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Tasker;
|
||||
|
||||
use Exception;
|
||||
use Kiri\Abstracts\Component;
|
||||
use Kiri\Core\HashMap;
|
||||
use Kiri\Kiri;
|
||||
use ReflectionException;
|
||||
use Server\Contract\OnTaskInterface;
|
||||
use Server\SwooleServerInterface;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
class AsyncTaskExecute extends Component
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @var SwooleServerInterface|null
|
||||
*/
|
||||
public ?SwooleServerInterface $server = null;
|
||||
|
||||
|
||||
private HashMap $hashMap;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
$this->hashMap = new HashMap();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @param $handler
|
||||
*/
|
||||
public function reg(string $key, $handler)
|
||||
{
|
||||
$this->hashMap->put($key, $handler);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param OnTaskInterface|string $handler
|
||||
* @param array $params
|
||||
* @param int|null $workerId
|
||||
* @throws Exception
|
||||
*/
|
||||
public function execute(OnTaskInterface|string $handler, array $params = [], int $workerId = null)
|
||||
{
|
||||
if (!$this->server) {
|
||||
$this->server = Kiri::getDi()->get(SwooleServerInterface::class);
|
||||
}
|
||||
if ($workerId === null || $workerId <= $this->server->setting['worker_num']) {
|
||||
$workerNum = $this->server->setting['worker_num'];
|
||||
$taskerNum = $workerNum + $this->server->setting['task_worker_num'];
|
||||
$workerId = random_int($workerNum, $taskerNum - 1);
|
||||
}
|
||||
if (is_string($handler)) {
|
||||
$handler = $this->handle($handler, $params);
|
||||
}
|
||||
$this->server->task(serialize($handler), $workerId);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $handler
|
||||
* @param $params
|
||||
* @return object
|
||||
* @throws ReflectionException
|
||||
* @throws Exception
|
||||
*/
|
||||
private function handle($handler, $params): object
|
||||
{
|
||||
if (!class_exists($handler) && $this->hashMap->has($handler)) {
|
||||
$handler = $this->hashMap->get($handler);
|
||||
}
|
||||
$implements = $this->container->getReflect($handler);
|
||||
if (!in_array(OnTaskInterface::class, $implements->getInterfaceNames())) {
|
||||
throw new Exception('Task must instance ' . OnTaskInterface::class);
|
||||
}
|
||||
return $implements->newInstanceArgs($params);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,94 +0,0 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Server\Tasker;
|
||||
|
||||
|
||||
use Kiri\Annotation\Inject;
|
||||
use Kiri\Abstracts\Logger;
|
||||
use Kiri\Exception\ConfigException;
|
||||
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
|
||||
*/
|
||||
private function resolve($data)
|
||||
{
|
||||
$execute = unserialize($data);
|
||||
if ($execute instanceof OnTaskInterface) {
|
||||
return $execute->execute();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @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);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace Server;
|
||||
namespace Kiri\Server;
|
||||
|
||||
trait TraitServer
|
||||
{
|
||||
|
||||
+1
-1
@@ -19,7 +19,7 @@
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Server\\": "./"
|
||||
"Kiri\\Server\\": "./"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user