modify plugin name
This commit is contained in:
@@ -8,7 +8,6 @@ use Kiri\Abstracts\Component;
|
||||
use Kiri\Server\SwooleServerInterface;
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
use Swoole\Process;
|
||||
|
||||
|
||||
/**
|
||||
@@ -18,35 +17,17 @@ class AsyncTaskExecute extends Component
|
||||
{
|
||||
|
||||
|
||||
use TaskResolve;
|
||||
|
||||
|
||||
private int $total = 50;
|
||||
|
||||
/**
|
||||
* @param int $total
|
||||
*/
|
||||
public function setTotal(int $total): void
|
||||
{
|
||||
$this->total = $total;
|
||||
}
|
||||
public TaskManager $hashMap;
|
||||
|
||||
|
||||
/**
|
||||
* @return void
|
||||
* @throws Kiri\Exception\ConfigException
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
public function start()
|
||||
public function init()
|
||||
{
|
||||
$processManager = $this->getContainer()->get(Kiri\Server\ProcessManager::class);
|
||||
for ($i = 0; $i < $this->total; $i++) {
|
||||
$class = new TaskProcess();
|
||||
$class->name = 'task.' . $i;
|
||||
|
||||
$processManager->add($class, 'tasker');
|
||||
}
|
||||
$this->hashMap = $this->getContainer()->get(TaskManager::class);
|
||||
}
|
||||
|
||||
|
||||
@@ -56,7 +37,6 @@ class AsyncTaskExecute extends Component
|
||||
* @param int $workerId
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
* @throws \ReflectionException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function execute(OnTaskInterface|string $handler, array $params = [], int $workerId = -1)
|
||||
@@ -71,18 +51,29 @@ class AsyncTaskExecute extends Component
|
||||
$workerId = random_int(0, $server->setting['task_worker_num'] - 1);
|
||||
}
|
||||
$server->task(serialize($handler), $workerId);
|
||||
} else {
|
||||
if ($workerId < 0 || $workerId > $this->total) {
|
||||
$workerId = random_int(0, $this->total - 1);
|
||||
}
|
||||
|
||||
$processManager = $container->get(Kiri\Server\ProcessManager::class);
|
||||
|
||||
/** @var Process $process */
|
||||
$process = $processManager->get('task.' . $workerId, 'tasker');
|
||||
$process->write(serialize($handler));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @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);
|
||||
}
|
||||
$implements = $this->getContainer()->getReflect($handler);
|
||||
if (!in_array(OnTaskInterface::class, $implements->getInterfaceNames())) {
|
||||
throw new Exception('Task must instance ' . OnTaskInterface::class);
|
||||
}
|
||||
return $implements->newInstanceArgs($params);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user