Files
kiri-core/kiri-task/TaskExecute.php
T

109 lines
2.6 KiB
PHP
Raw Normal View History

2022-01-17 18:45:00 +08:00
<?php
namespace Kiri\Task;
use Exception;
use Kiri;
use Kiri\Abstracts\Component;
2022-06-16 17:38:23 +08:00
use Kiri\Server\ServerInterface;
2022-02-11 19:00:55 +08:00
use Psr\Container\ContainerExceptionInterface;
2022-06-16 17:38:23 +08:00
use Kiri\Di\ContainerInterface;
2022-02-11 19:00:55 +08:00
use Psr\Container\NotFoundExceptionInterface;
2022-06-16 17:38:23 +08:00
use Swoole\Coroutine;
use Swoole\Server;
2022-01-17 18:45:00 +08:00
/**
*
*/
2022-06-16 17:38:23 +08:00
class TaskExecute extends Component
2022-01-17 18:45:00 +08:00
{
2022-02-11 19:00:55 +08:00
/**
2022-06-16 17:38:23 +08:00
* @param TaskContainer $hashMap
2022-03-03 18:30:59 +08:00
* @param ContainerInterface $container
* @param array $config
* @throws Exception
2022-02-11 19:00:55 +08:00
*/
2022-06-16 17:38:23 +08:00
public function __construct(public TaskContainer $hashMap, public ContainerInterface $container, array $config = [])
2022-02-11 19:00:55 +08:00
{
2022-03-03 18:30:59 +08:00
parent::__construct($config);
2022-02-11 19:00:55 +08:00
}
2022-01-17 18:45:00 +08:00
/**
* @param OnTaskInterface|string $handler
* @param array $params
* @param int $workerId
2022-02-11 19:00:55 +08:00
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
2022-01-17 18:45:00 +08:00
* @throws Exception
*/
public function execute(OnTaskInterface|string $handler, array $params = [], int $workerId = -1)
{
2022-01-18 10:18:13 +08:00
if (is_string($handler)) {
$handler = $this->handle($handler, $params);
}
2022-06-16 17:38:23 +08:00
if ($this->container->has(ServerInterface::class)) {
$this->onAsyncExec($handler, $workerId);
} else {
Coroutine::create(fn() => $this->onCoronExec($handler));
2022-02-17 17:44:28 +08:00
}
}
2022-02-11 19:00:55 +08:00
2022-01-18 10:18:13 +08:00
2022-06-16 17:38:23 +08:00
/**
* @param OnTaskInterface|string $handler
* @param int $workerId
* @return bool
* @throws Exception
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
protected function onAsyncExec(OnTaskInterface|string $handler, int $workerId = -1): bool
{
/** @var Server $server */
$server = $this->container->get(ServerInterface::class);
if ($workerId < 0 || $workerId > $server->setting['task_worker_num']) {
$workerId = random_int(0, $server->setting['task_worker_num'] - 1);
}
return (bool)$server->task(serialize($handler), $workerId);
}
/**
* @param OnTaskInterface|string $handler
* @return bool
*/
protected function onCoronExec(OnTaskInterface|string $handler): bool
{
$handler->execute();
$handler->finish(null, Coroutine::getCid());
return true;
}
2022-02-17 17:44:28 +08:00
/**
* @param $handler
* @param $params
* @return object
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws \ReflectionException
* @throws Exception
*/
private function handle($handler, $params): object
{
if (!class_exists($handler) && $this->hashMap->has($handler)) {
$handler = $this->hashMap->get($handler);
2022-01-17 18:45:00 +08:00
}
2022-03-03 18:30:59 +08:00
$implements = $this->container->getReflect($handler);
2022-02-17 17:44:28 +08:00
if (!in_array(OnTaskInterface::class, $implements->getInterfaceNames())) {
throw new Exception('Task must instance ' . OnTaskInterface::class);
}
return $implements->newInstanceArgs($params);
2022-01-17 18:45:00 +08:00
}
}