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

155 lines
3.1 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;
2021-08-11 01:04:57 +08:00
use Kiri\Abstracts\Config;
use Kiri\Event;
use Kiri\Kiri;
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
{
2021-03-01 17:27:07 +08:00
/**
* @throws Exception
*/
public function onHandler()
{
2021-08-11 01:04:57 +08:00
$setting = Kiri::app()->getSwoole();
2021-03-01 18:14:00 +08:00
$isCoroutineTask = $setting->setting['task_enable_coroutine'] ?? false;
if ($isCoroutineTask === true) {
call_user_func([$this, 'onContinueTask'], ...func_get_args());
2021-03-01 17:27:07 +08:00
} else {
2021-03-01 18:14:00 +08:00
call_user_func([$this, 'onTask'], ...func_get_args());
2021-03-01 17:27:07 +08:00
}
}
/**
* @param Server $server
* @param int $task_id
* @param int $from_id
* @param string $data
*
* @return mixed
* @throws Exception 异步任务
*/
public function onTask(Server $server, int $task_id, int $from_id, string $data): mixed
{
if (empty($data)) {
return $server->finish('null data');
}
$time = microtime(TRUE);
$finish = $this->runTaskHandler($data);
if (!$finish) {
$finish = [];
}
$finish['runTime'] = [
'startTime' => $time,
'runTime' => microtime(TRUE) - $time,
'endTime' => microtime(TRUE),
];
return $server->finish(json_encode($finish));
}
/**
* @param Server $server
* @param Server\Task $task
* @return mixed
* @throws Exception 异步任务
*/
public function onContinueTask(Server $server, Server\Task $task): mixed
{
if (empty($task->data)) {
return $task->finish('null data');
}
$time = microtime(TRUE);
$finish = $this->runTaskHandler($task->data);
if (!$finish) {
$finish = [];
}
$finish['runTime'] = [
'startTime' => $time,
'runTime' => microtime(TRUE) - $time,
'endTime' => microtime(TRUE),
];
return $task->finish(json_encode($finish));
}
/**
* @param $data
* @return array|null
* @throws Exception
*/
private function runTaskHandler($data): ?array
{
try {
2021-07-02 21:20:35 +08:00
defer(fn() => fire(Event::SYSTEM_RESOURCE_CLEAN));
2021-05-04 01:33:08 +08:00
2021-04-23 03:25:03 +08:00
$serialize = $this->before($data);
2021-03-24 17:57:08 +08:00
if ($serialize === null) {
throw new Exception('unpack error.');
}
2021-03-01 17:27:07 +08:00
$params = $serialize->getParams();
if (is_object($params)) {
$params = get_object_vars($params);
}
2021-04-25 11:22:23 +08:00
$finish['class'] = $serialize::class;
2021-03-01 17:27:07 +08:00
$finish['params'] = $params;
$finish['status'] = 'success';
$finish['info'] = $serialize->onHandler();
2021-03-24 17:57:08 +08:00
return $finish;
2021-03-01 17:27:07 +08:00
} catch (\Throwable $exception) {
$finish['status'] = 'error';
$finish['info'] = $this->format($exception);
2021-03-30 11:56:29 +08:00
$this->addError($exception, 'Task');
2021-03-24 17:57:08 +08:00
return $finish;
2021-03-01 17:27:07 +08:00
}
}
/**
* @param $data
* @return ITask|null
*/
protected function before($data): ?Task
{
2021-03-24 17:57:08 +08:00
if (empty($serialize = swoole_unserialize($data))) {
2021-03-01 17:27:07 +08:00
return null;
}
if (!($serialize instanceof ITask)) {
return null;
}
return $serialize;
}
/**
* @param $exception
* @return string
*/
private function format($exception): string
{
return $exception->getMessage() . " on line " . $exception->getLine() . " at file " . $exception->getFile();
}
2020-08-31 01:27:08 +08:00
}