This commit is contained in:
2021-09-22 11:36:29 +08:00
parent 07e919a2d5
commit e3bf41c7c5
3 changed files with 93 additions and 80 deletions
+2 -1
View File
@@ -8,6 +8,7 @@ use Http\Handler\Context;
use Http\Message\ContentType; use Http\Message\ContentType;
use JetBrains\PhpStorm\Pure; use JetBrains\PhpStorm\Pure;
use Kiri\Abstracts\Config; use Kiri\Abstracts\Config;
use Kiri\Exception\ConfigException;
use Kiri\Kiri; use Kiri\Kiri;
use Psr\Http\Message\StreamInterface; use Psr\Http\Message\StreamInterface;
use Http\Message\ServerRequest as RequestMessage; use Http\Message\ServerRequest as RequestMessage;
@@ -30,7 +31,7 @@ class Response implements ResponseInterface
/** /**
* @throws \Kiri\Exception\ConfigException * @throws ConfigException
*/ */
public function __construct() public function __construct()
{ {
+91 -62
View File
@@ -4,10 +4,15 @@
namespace Server\Task; namespace Server\Task;
use ReflectionException; use Kiri\Abstracts\Config;
use Server\SInterface\TaskExecute; use Kiri\Exception\ConfigException;
use Kiri\Exception\NotFindClassException;
use Kiri\Kiri; use Kiri\Kiri;
use ReflectionException;
use Server\Constrict\Response;
use Server\Constrict\ResponseInterface;
use Server\ExceptionHandlerDispatcher;
use Server\ExceptionHandlerInterface;
use Server\SInterface\TaskExecute;
use Swoole\Server; use Swoole\Server;
@@ -19,71 +24,95 @@ class OnServerTask
{ {
/** /**
* @param Server $server * @var ExceptionHandlerInterface|null
* @param int $task_id */
* @param int $src_worker_id public ?ExceptionHandlerInterface $handler = null;
* @param mixed $data
*/
public function onTask(Server $server, int $task_id, int $src_worker_id, mixed $data)
{
try {
$data = $this->resolve($data);
} catch (\Throwable $exception) {
$data = [$exception->getMessage()];
} finally {
$server->finish($data);
}
}
/** /**
* @param Server $server * @throws ConfigException
* @param Server\Task $task */
*/ public function emit(\Throwable $exception, Response $response): ResponseInterface
public function onCoroutineTask(Server $server, Server\Task $task) {
{ if ($this->handler == null) {
try { $exceptionHandler = Config::get('exception.task', ExceptionHandlerDispatcher::class);
$data = $this->resolve($task->data); if (!in_array(ExceptionHandlerInterface::class, class_implements($exceptionHandler))) {
} catch (\Throwable $exception) { $exceptionHandler = ExceptionHandlerDispatcher::class;
$data = [$exception->getMessage()]; }
} finally { $this->handler = Kiri::getDi()->get($exceptionHandler);
$server->finish($data); }
} return $this->handler->emit($exception, $response);
} }
/** /**
* @param $data * @param Server $server
* @return null * @param int $task_id
* @throws ReflectionException * @param int $src_worker_id
*/ * @param mixed $data
private function resolve($data) * @throws ConfigException
{ */
[$class, $params] = json_encode($data, true); public function onTask(Server $server, int $task_id, int $src_worker_id, mixed $data)
{
$reflect = Kiri::getDi()->getReflect($class); try {
$data = $this->resolve($data);
if (!$reflect->isInstantiable()) { } catch (\Throwable $exception) {
return null; $data = $this->emit($exception, new Response());
} } finally {
$class = $reflect->newInstanceArgs($params); $server->finish($data);
return $class->execute(); }
} }
/** /**
* @param Server $server * @param Server $server
* @param int $task_id * @param Server\Task $task
* @param mixed $data * @throws ConfigException
*/ */
public function onFinish(Server $server, int $task_id, mixed $data) public function onCoroutineTask(Server $server, Server\Task $task)
{ {
if (!($data instanceof TaskExecute)) { try {
return; $data = $this->resolve($task->data);
} } catch (\Throwable $exception) {
$data->finish($server, $task_id); $data = $this->emit($exception, new Response());
} } 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 TaskExecute)) {
return;
}
$data->finish($server, $task_id);
}
} }
-17
View File
@@ -1,17 +0,0 @@
<?php
namespace Annotation;
interface Porters
{
/**
* @return mixed
*/
public function process(): mixed;
}