Files
kiri-core/HttpServer/Events/OnTask.php
T

152 lines
3.0 KiB
PHP
Raw Normal View History

2020-08-31 01:27:08 +08:00
<?php
2020-10-29 18:17:25 +08:00
declare(strict_types=1);
2020-08-31 01:27:08 +08:00
2020-09-02 11:38:47 +08:00
namespace HttpServer\Events;
2020-08-31 01:27:08 +08:00
2020-09-04 01:05:33 +08:00
use HttpServer\Abstracts\Callback;
2020-10-29 18:17:25 +08:00
use HttpServer\IInterface\Task;
2020-08-31 01:27:08 +08:00
use HttpServer\IInterface\Task as ITask;
use Snowflake\Event;
use Snowflake\Snowflake;
2021-02-26 17:53:51 +08:00
use Swoole\Coroutine;
use Swoole\Process;
2020-08-31 01:27:08 +08:00
use Swoole\Server;
use Swoole\Timer;
use Exception;
2020-09-02 18:36:53 +08:00
/**
* Class OnTask
* @package HttpServer\Events
*/
2020-08-31 01:27:08 +08:00
class OnTask extends Callback
{
/**
* @throws Exception
*/
public function onHandler()
{
$parameter = func_get_args();
2020-09-02 18:36:53 +08:00
if (func_num_args() < 4) {
$this->onContinueTask(...$parameter);
2020-08-31 01:27:08 +08:00
} else {
2020-09-02 18:36:53 +08:00
$this->onTask(...$parameter);
2020-08-31 01:27:08 +08:00
}
}
/**
* @param Server $server
* @param int $task_id
* @param int $from_id
* @param string $data
*
2020-12-14 15:34:59 +08:00
* @return mixed
2020-10-29 18:17:25 +08:00
* @throws Exception 异步任务
2020-08-31 01:27:08 +08:00
*/
2020-12-14 15:34:59 +08:00
public function onTask(Server $server, int $task_id, int $from_id, string $data): mixed
2020-08-31 01:27:08 +08:00
{
$time = microtime(TRUE);
if (empty($data)) {
return $server->finish('null data');
}
2021-02-26 17:53:51 +08:00
2020-08-31 01:27:08 +08:00
$finish = $this->runTaskHandler($data);
if (!$finish) {
$finish = [];
}
$finish['runTime'] = [
'startTime' => $time,
'runTime' => microtime(TRUE) - $time,
'endTime' => microtime(TRUE),
];
2020-12-14 15:34:59 +08:00
return $server->finish(json_encode($finish));
2020-08-31 01:27:08 +08:00
}
/**
* @param Server $server
* @param Server\Task $task
2020-12-14 15:34:59 +08:00
* @return mixed
* @throws Exception 异步任务
2020-08-31 01:27:08 +08:00
*/
2020-12-14 15:34:59 +08:00
public function onContinueTask(Server $server, Server\Task $task): mixed
2020-08-31 01:27:08 +08:00
{
$time = microtime(TRUE);
if (empty($task->data)) {
return $task->finish('null data');
}
2021-02-27 04:25:59 +08:00
$finish = $this->runTaskHandler($task->data);
if (!$finish) {
2020-08-31 01:27:08 +08:00
$finish = [];
}
$finish['runTime'] = [
'startTime' => $time,
'runTime' => microtime(TRUE) - $time,
'endTime' => microtime(TRUE),
];
2020-12-14 15:34:59 +08:00
return $task->finish(json_encode($finish));
2020-08-31 01:27:08 +08:00
}
2021-02-26 17:53:51 +08:00
2020-08-31 01:27:08 +08:00
/**
* @param $data
* @return array|null
* @throws Exception
*/
2020-12-14 15:34:59 +08:00
private function runTaskHandler($data): ?array
2020-08-31 01:27:08 +08:00
{
2021-02-27 04:36:10 +08:00
$app = Snowflake::app()->increment();
try {
2021-02-26 19:53:28 +08:00
$serialize = $this->before($data);
2020-08-31 01:27:08 +08:00
$params = $serialize->getParams();
if (is_object($params)) {
$params = get_object_vars($params);
}
$finish['class'] = get_class($serialize);
$finish['params'] = $params;
$finish['status'] = 'success';
2020-10-29 18:17:25 +08:00
$finish['info'] = $serialize->onHandler();
2020-08-31 01:27:08 +08:00
} catch (\Throwable $exception) {
$finish['status'] = 'error';
$finish['info'] = $this->format($exception);
$this->error($exception, 'Task');
}
2021-02-27 04:36:10 +08:00
$event = Snowflake::app()->getEvent();
$event->trigger(Event::SYSTEM_RESOURCE_CLEAN);
2021-02-27 04:36:36 +08:00
$app->decrement();
2021-02-27 04:36:10 +08:00
Timer::clearAll();
return $finish;
2020-08-31 01:27:08 +08:00
}
2021-02-27 04:38:12 +08:00
2020-08-31 01:27:08 +08:00
/**
* @param $data
* @return ITask|null
*/
2020-10-30 01:12:09 +08:00
protected function before($data): ?Task
2020-08-31 01:27:08 +08:00
{
if (empty($serialize = unserialize($data))) {
return null;
}
if (!($serialize instanceof ITask)) {
return null;
}
return $serialize;
}
/**
* @param $exception
* @return string
*/
2020-12-14 15:34:59 +08:00
private function format($exception): string
2020-08-31 01:27:08 +08:00
{
return $exception->getMessage() . " on line " . $exception->getLine() . " at file " . $exception->getFile();
}
}