2023-04-22 02:04:31 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Kiri\Server\Task;
|
|
|
|
|
|
|
|
|
|
|
2023-07-31 23:08:58 +08:00
|
|
|
use Kiri;
|
2024-04-24 14:17:09 +08:00
|
|
|
use Kiri\Router\Base\ExceptionHandlerDispatcher;
|
|
|
|
|
use Kiri\Router\Interface\ExceptionHandlerInterface;
|
2023-04-22 02:04:31 +08:00
|
|
|
use Kiri\Server\Constant;
|
|
|
|
|
use Swoole\Server;
|
2023-07-31 23:13:57 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
*/
|
2023-08-17 17:04:55 +08:00
|
|
|
class Task
|
2023-04-22 02:04:31 +08:00
|
|
|
{
|
|
|
|
|
|
2024-04-24 14:17:09 +08:00
|
|
|
|
|
|
|
|
public ExceptionHandlerInterface $exception;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2024-08-29 17:01:07 +08:00
|
|
|
*
|
2024-04-24 14:17:09 +08:00
|
|
|
*/
|
2024-08-29 17:01:07 +08:00
|
|
|
public function __construct()
|
2024-04-24 14:17:09 +08:00
|
|
|
{
|
2025-12-18 15:39:40 +08:00
|
|
|
$exception = \config('servers.task.exception');
|
2024-04-24 14:17:09 +08:00
|
|
|
if (!in_array(ExceptionHandlerInterface::class, class_implements($exception))) {
|
|
|
|
|
$exception = ExceptionHandlerDispatcher::class;
|
|
|
|
|
}
|
2024-08-29 17:01:07 +08:00
|
|
|
$this->exception = Kiri::getDi()->get($exception);
|
2024-04-24 14:17:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2023-08-17 16:56:51 +08:00
|
|
|
/**
|
|
|
|
|
* @param Server $server
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function initTaskWorker(Server $server): void
|
|
|
|
|
{
|
|
|
|
|
if (!isset($server->setting[Constant::OPTION_TASK_WORKER_NUM])) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if ($server->setting[Constant::OPTION_TASK_WORKER_NUM] < 1) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
$server->on('finish', [$this, 'onFinish']);
|
|
|
|
|
$server->on('task', [$this, 'onTask']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param Server $server
|
|
|
|
|
* @param int $task_id
|
|
|
|
|
* @param mixed $data
|
|
|
|
|
* @return void
|
2023-12-12 15:35:34 +08:00
|
|
|
* @throws
|
2023-08-17 16:56:51 +08:00
|
|
|
*/
|
|
|
|
|
public function onFinish(Server $server, int $task_id, mixed $data): void
|
|
|
|
|
{
|
|
|
|
|
event(new OnTaskFinish($task_id, $data));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param Server $server
|
|
|
|
|
* @param int $task_id
|
|
|
|
|
* @param int $src_worker_id
|
|
|
|
|
* @param mixed $data
|
2025-07-11 11:50:54 +08:00
|
|
|
* @return void
|
2023-12-12 15:35:34 +08:00
|
|
|
* @throws
|
2023-08-17 16:56:51 +08:00
|
|
|
*/
|
2025-07-11 11:50:54 +08:00
|
|
|
public function onTask(Server $server, int $task_id, int $src_worker_id, mixed $data): void
|
2023-08-17 16:56:51 +08:00
|
|
|
{
|
2024-04-24 14:17:09 +08:00
|
|
|
try {
|
2025-07-11 15:04:25 +08:00
|
|
|
[$handler, $params] = [$data[0], $data[1]];
|
2024-12-27 18:26:49 +08:00
|
|
|
|
2025-07-11 15:04:25 +08:00
|
|
|
$handler = Kiri::getDi()->make($handler, $params);
|
|
|
|
|
if (!($handler instanceof OnTaskInterface)) {
|
|
|
|
|
throw new \Exception('Task process must implements ' . OnTaskInterface::class);
|
2025-07-11 11:50:54 +08:00
|
|
|
}
|
2025-07-11 15:04:25 +08:00
|
|
|
|
|
|
|
|
$response = call_user_func([$handler, 'process'], $task_id, $src_worker_id);
|
2024-04-24 14:17:09 +08:00
|
|
|
} catch (\Throwable $throwable) {
|
2025-12-31 00:19:28 +08:00
|
|
|
\Kiri::getLogger()->json_log($throwable, ['task_id' => $task_id, 'src_worker_id' => $src_worker_id, 'data' => $data]);
|
|
|
|
|
|
|
|
|
|
$response = throwable($throwable);
|
2025-07-11 11:50:54 +08:00
|
|
|
} finally {
|
|
|
|
|
$server->finish($response);
|
2023-08-17 16:56:51 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2023-04-22 02:04:31 +08:00
|
|
|
}
|