Files
kiri-http-server/Task/Task.php
T

99 lines
2.2 KiB
PHP
Raw Normal View History

2023-04-22 02:04:31 +08:00
<?php
namespace Kiri\Server\Task;
2023-07-31 23:08:58 +08:00
use Kiri;
2023-04-22 02:04:31 +08:00
use Kiri\Server\Constant;
2023-06-12 15:31:44 +08:00
use Kiri\Server\ServerInterface;
2023-04-22 02:04:31 +08:00
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use ReflectionException;
use Swoole\Server;
class Task implements TaskInterface
{
2023-07-31 23:08:58 +08:00
/**
2023-07-31 23:13:44 +08:00
* @var ServerInterface
2023-07-31 23:08:58 +08:00
*/
2023-07-31 23:13:44 +08:00
public ServerInterface $server;
2023-04-22 02:04:31 +08:00
2023-07-31 23:08:58 +08:00
/**
2023-04-22 02:04:31 +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
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws ReflectionException
*/
2023-04-22 02:13:01 +08:00
public function onFinish(Server $server, int $task_id, mixed $data): void
2023-04-22 02:04:31 +08:00
{
event(new OnTaskFinish($task_id, $data));
}
/**
* @param Server $server
* @param int $task_id
* @param int $src_worker_id
* @param mixed $data
* @return mixed
* @throws ReflectionException
*/
2023-04-22 02:13:01 +08:00
public function onTask(Server $server, int $task_id, int $src_worker_id, mixed $data): mixed
2023-04-22 02:04:31 +08:00
{
$data = json_decode($data, true);
if (is_null($data)) {
return null;
}
2023-07-31 23:08:58 +08:00
$data[0] = Kiri::getDi()->get($data[0]);
2023-04-22 02:04:31 +08:00
return call_user_func($data, $task_id, $src_worker_id);
}
/**
2023-04-22 02:46:58 +08:00
* @param array|string|object $handler
2023-04-22 02:04:31 +08:00
* @param int|null $workerId
* @return void
2023-04-22 02:46:58 +08:00
* @throws ReflectionException
2023-04-22 02:04:31 +08:00
*/
2023-04-22 02:46:58 +08:00
public function dispatch(array|string|object $handler, ?int $workerId = null): void
2023-04-22 02:04:31 +08:00
{
if (is_null($workerId)) {
2023-07-31 23:08:58 +08:00
$workerId = rand(0, $this->server->setting[Constant::OPTION_TASK_WORKER_NUM] - 1);
2023-04-22 02:04:31 +08:00
}
2023-04-22 02:46:58 +08:00
if (is_string($handler)) {
2023-07-31 23:08:58 +08:00
$this->server->task(serialize([di($handler), 'handle']), $workerId);
2023-04-22 02:47:37 +08:00
} else if (is_array($handler)) {
if (is_string($handler[0])) {
$handler[0] = di($handler[0]);
}
2023-07-31 23:08:58 +08:00
$this->server->task(serialize($handler), $workerId);
2023-04-22 02:46:58 +08:00
} else {
2023-07-31 23:08:58 +08:00
$this->server->task(serialize([$handler, 'handle']), $workerId);
2023-04-22 02:46:58 +08:00
}
2023-04-22 02:04:31 +08:00
}
}