2022-01-09 03:49:02 +08:00
|
|
|
<?php
|
|
|
|
|
|
2022-01-10 11:39:55 +08:00
|
|
|
namespace Kiri\Server\Tasker;
|
2022-01-09 03:49:02 +08:00
|
|
|
|
|
|
|
|
use Exception;
|
|
|
|
|
use Kiri\Abstracts\Component;
|
|
|
|
|
use Kiri\Core\HashMap;
|
2022-01-12 14:10:32 +08:00
|
|
|
use Kiri;
|
2022-01-09 03:49:02 +08:00
|
|
|
use ReflectionException;
|
2022-01-10 11:39:55 +08:00
|
|
|
use Kiri\Server\Contract\OnTaskInterface;
|
|
|
|
|
use Kiri\Server\SwooleServerInterface;
|
2022-01-10 18:57:54 +08:00
|
|
|
use Swoole\Server;
|
2022-01-09 03:49:02 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
class AsyncTaskExecute extends Component
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2022-01-10 18:57:54 +08:00
|
|
|
* @var Server|\Swoole\WebSocket\Server|\Swoole\Http\Server
|
2022-01-09 03:49:02 +08:00
|
|
|
*/
|
2022-01-10 18:57:54 +08:00
|
|
|
public mixed $server = null;
|
2022-01-09 03:49:02 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
private HashMap $hashMap;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
public function init()
|
|
|
|
|
{
|
|
|
|
|
$this->hashMap = new HashMap();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $key
|
|
|
|
|
* @param $handler
|
|
|
|
|
*/
|
|
|
|
|
public function reg(string $key, $handler)
|
|
|
|
|
{
|
|
|
|
|
$this->hashMap->put($key, $handler);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param OnTaskInterface|string $handler
|
|
|
|
|
* @param array $params
|
2022-01-10 19:03:55 +08:00
|
|
|
* @param int $workerId
|
2022-01-09 03:49:02 +08:00
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2022-01-10 19:03:55 +08:00
|
|
|
public function execute(OnTaskInterface|string $handler, array $params = [], int $workerId = -1)
|
2022-01-09 03:49:02 +08:00
|
|
|
{
|
|
|
|
|
if (!$this->server) {
|
|
|
|
|
$this->server = Kiri::getDi()->get(SwooleServerInterface::class);
|
|
|
|
|
}
|
2022-01-10 19:03:55 +08:00
|
|
|
if ($workerId < 0 || $workerId > $this->server->setting['task_worker_num']) {
|
|
|
|
|
$workerId = random_int(0, $this->server->setting['task_worker_num'] - 1);
|
2022-01-09 03:49:02 +08:00
|
|
|
}
|
|
|
|
|
if (is_string($handler)) {
|
|
|
|
|
$handler = $this->handle($handler, $params);
|
|
|
|
|
}
|
|
|
|
|
$this->server->task(serialize($handler), $workerId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $handler
|
|
|
|
|
* @param $params
|
|
|
|
|
* @return object
|
|
|
|
|
* @throws ReflectionException
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
private function handle($handler, $params): object
|
|
|
|
|
{
|
|
|
|
|
if (!class_exists($handler) && $this->hashMap->has($handler)) {
|
|
|
|
|
$handler = $this->hashMap->get($handler);
|
|
|
|
|
}
|
|
|
|
|
$implements = $this->container->getReflect($handler);
|
|
|
|
|
if (!in_array(OnTaskInterface::class, $implements->getInterfaceNames())) {
|
|
|
|
|
throw new Exception('Task must instance ' . OnTaskInterface::class);
|
|
|
|
|
}
|
|
|
|
|
return $implements->newInstanceArgs($params);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|