2022-01-17 18:45:00 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Kiri\Task;
|
|
|
|
|
|
2022-03-03 18:30:59 +08:00
|
|
|
use Exception;
|
2022-02-17 17:44:28 +08:00
|
|
|
use JetBrains\PhpStorm\Pure;
|
2022-01-17 18:45:00 +08:00
|
|
|
use Kiri\Abstracts\Component;
|
|
|
|
|
use Kiri\Core\HashMap;
|
|
|
|
|
use Psr\Container\ContainerExceptionInterface;
|
2022-03-03 18:30:59 +08:00
|
|
|
use Psr\Container\ContainerInterface;
|
2022-01-17 18:45:00 +08:00
|
|
|
use Psr\Container\NotFoundExceptionInterface;
|
|
|
|
|
use Swoole\Server;
|
|
|
|
|
|
|
|
|
|
class TaskManager extends Component
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private HashMap $hashMap;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2022-03-03 18:30:59 +08:00
|
|
|
* @param ContainerInterface $container
|
|
|
|
|
* @param array $config
|
|
|
|
|
* @throws Exception
|
2022-01-17 18:45:00 +08:00
|
|
|
*/
|
2022-03-03 18:30:59 +08:00
|
|
|
public function __construct(public ContainerInterface $container, array $config = [])
|
2022-01-17 18:45:00 +08:00
|
|
|
{
|
2022-03-03 18:30:59 +08:00
|
|
|
parent::__construct($config);
|
2022-01-17 18:45:00 +08:00
|
|
|
$this->hashMap = new HashMap();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param Server $swollen
|
|
|
|
|
* @return void
|
|
|
|
|
* @throws ContainerExceptionInterface
|
|
|
|
|
* @throws NotFoundExceptionInterface
|
|
|
|
|
*/
|
|
|
|
|
public function taskListener(Server $swollen)
|
|
|
|
|
{
|
|
|
|
|
if (!isset($swollen->setting['task_worker_num']) || $swollen->setting['task_worker_num'] < 1) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$task_use_object = $swollen->setting['task_object'] ?? $swollen->setting['task_use_object'] ?? false;
|
2022-03-03 18:30:59 +08:00
|
|
|
$reflect = $this->container->get(OnServerTask::class);
|
2022-01-17 18:45:00 +08:00
|
|
|
|
|
|
|
|
$swollen->on('finish', [$reflect, 'onFinish']);
|
|
|
|
|
if ($task_use_object || $swollen->setting['task_enable_coroutine']) {
|
|
|
|
|
$swollen->on('task', [$reflect, 'onCoroutineTask']);
|
|
|
|
|
} else {
|
|
|
|
|
$swollen->on('task', [$reflect, 'onTask']);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $key
|
|
|
|
|
* @param $handler
|
|
|
|
|
*/
|
|
|
|
|
public function add(string $key, $handler)
|
|
|
|
|
{
|
|
|
|
|
$this->hashMap->put($key, $handler);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2022-02-17 17:44:28 +08:00
|
|
|
/**
|
|
|
|
|
* @param string $key
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
#[Pure] public function has(string $key): bool
|
|
|
|
|
{
|
|
|
|
|
return $this->hashMap->has($key);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2022-01-17 18:45:00 +08:00
|
|
|
/**
|
|
|
|
|
* @param string $key
|
|
|
|
|
* @return OnTaskInterface
|
|
|
|
|
* @throws ContainerExceptionInterface
|
|
|
|
|
* @throws NotFoundExceptionInterface
|
|
|
|
|
*/
|
|
|
|
|
public function get(string $key): OnTaskInterface
|
|
|
|
|
{
|
|
|
|
|
$task = $this->hashMap->get($key);
|
|
|
|
|
if (is_string($task)) {
|
2022-03-03 18:30:59 +08:00
|
|
|
$task = $this->container->get($task);
|
2022-02-17 17:44:28 +08:00
|
|
|
if (!empty($task)) {
|
|
|
|
|
$this->add($key, $task);
|
|
|
|
|
}
|
2022-01-17 18:45:00 +08:00
|
|
|
}
|
|
|
|
|
return $task;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|