Files
kiri-core/kiri-task/TaskManager.php
T
2022-02-17 17:44:28 +08:00

92 lines
1.7 KiB
PHP

<?php
namespace Kiri\Task;
use JetBrains\PhpStorm\Pure;
use Kiri\Abstracts\Component;
use Kiri\Core\HashMap;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Swoole\Server;
class TaskManager extends Component
{
private HashMap $hashMap;
/**
*
*/
public function init()
{
$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;
$reflect = $this->getContainer()->get(OnServerTask::class);
$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);
}
/**
* @param string $key
* @return bool
*/
#[Pure] public function has(string $key): bool
{
return $this->hashMap->has($key);
}
/**
* @param string $key
* @return OnTaskInterface
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function get(string $key): OnTaskInterface
{
$task = $this->hashMap->get($key);
if (is_string($task)) {
$task = $this->getContainer()->get($task);
if (!empty($task)) {
$this->add($key, $task);
}
}
return $task;
}
}