modify plugin name
This commit is contained in:
@@ -8,7 +8,6 @@ use Kiri\Abstracts\Component;
|
|||||||
use Kiri\Server\SwooleServerInterface;
|
use Kiri\Server\SwooleServerInterface;
|
||||||
use Psr\Container\ContainerExceptionInterface;
|
use Psr\Container\ContainerExceptionInterface;
|
||||||
use Psr\Container\NotFoundExceptionInterface;
|
use Psr\Container\NotFoundExceptionInterface;
|
||||||
use Swoole\Process;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -18,35 +17,17 @@ class AsyncTaskExecute extends Component
|
|||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
use TaskResolve;
|
public TaskManager $hashMap;
|
||||||
|
|
||||||
|
|
||||||
private int $total = 50;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param int $total
|
|
||||||
*/
|
|
||||||
public function setTotal(int $total): void
|
|
||||||
{
|
|
||||||
$this->total = $total;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return void
|
* @return void
|
||||||
* @throws Kiri\Exception\ConfigException
|
|
||||||
* @throws ContainerExceptionInterface
|
* @throws ContainerExceptionInterface
|
||||||
* @throws NotFoundExceptionInterface
|
* @throws NotFoundExceptionInterface
|
||||||
*/
|
*/
|
||||||
public function start()
|
public function init()
|
||||||
{
|
{
|
||||||
$processManager = $this->getContainer()->get(Kiri\Server\ProcessManager::class);
|
$this->hashMap = $this->getContainer()->get(TaskManager::class);
|
||||||
for ($i = 0; $i < $this->total; $i++) {
|
|
||||||
$class = new TaskProcess();
|
|
||||||
$class->name = 'task.' . $i;
|
|
||||||
|
|
||||||
$processManager->add($class, 'tasker');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -56,7 +37,6 @@ class AsyncTaskExecute extends Component
|
|||||||
* @param int $workerId
|
* @param int $workerId
|
||||||
* @throws ContainerExceptionInterface
|
* @throws ContainerExceptionInterface
|
||||||
* @throws NotFoundExceptionInterface
|
* @throws NotFoundExceptionInterface
|
||||||
* @throws \ReflectionException
|
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function execute(OnTaskInterface|string $handler, array $params = [], int $workerId = -1)
|
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);
|
$workerId = random_int(0, $server->setting['task_worker_num'] - 1);
|
||||||
}
|
}
|
||||||
$server->task(serialize($handler), $workerId);
|
$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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,95 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Kiri\Task;
|
|
||||||
|
|
||||||
use Kiri\Abstracts\Component;
|
|
||||||
use Kiri\Core\HashMap;
|
|
||||||
use Kiri\Exception\ConfigException;
|
|
||||||
use ReflectionException;
|
|
||||||
use Swoole\Coroutine;
|
|
||||||
use Swoole\Server\Task;
|
|
||||||
|
|
||||||
class CoroutineTaskExecute extends Component
|
|
||||||
{
|
|
||||||
|
|
||||||
use TaskResolve;
|
|
||||||
|
|
||||||
|
|
||||||
private HashMap $hashMap;
|
|
||||||
|
|
||||||
|
|
||||||
private Coroutine\Channel $channel;
|
|
||||||
|
|
||||||
|
|
||||||
private OnServerTask $taskServer;
|
|
||||||
|
|
||||||
|
|
||||||
private int $total = 50;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param int $total
|
|
||||||
*/
|
|
||||||
public function setTotal(int $total): void
|
|
||||||
{
|
|
||||||
$this->total = $total;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public function start()
|
|
||||||
{
|
|
||||||
$this->hashMap = new HashMap();
|
|
||||||
|
|
||||||
$this->channel = new Coroutine\Channel($this->total);
|
|
||||||
|
|
||||||
$this->taskServer = \Kiri::getDi()->get(OnServerTask::class);
|
|
||||||
|
|
||||||
Coroutine::create(function () {
|
|
||||||
$barrier = Coroutine\Barrier::make();
|
|
||||||
for ($i = 0; $i < $this->total; $i++) {
|
|
||||||
Coroutine::create(function () {
|
|
||||||
$this->handler();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
Coroutine\Barrier::wait($barrier);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return void
|
|
||||||
* @throws ConfigException
|
|
||||||
*/
|
|
||||||
protected function handler()
|
|
||||||
{
|
|
||||||
Coroutine\defer(function () {
|
|
||||||
$this->handler();
|
|
||||||
});
|
|
||||||
$data = $this->channel->pop(-1);
|
|
||||||
|
|
||||||
$task = new Task();
|
|
||||||
$task->data = $data;
|
|
||||||
|
|
||||||
$this->taskServer->onCoroutineTask(null, $task);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param OnTaskInterface|string $handler
|
|
||||||
* @param array $params
|
|
||||||
* @param int $workerId
|
|
||||||
* @return void
|
|
||||||
* @throws ReflectionException
|
|
||||||
*/
|
|
||||||
public function execute(OnTaskInterface|string $handler, array $params = [], int $workerId = -1)
|
|
||||||
{
|
|
||||||
if (is_string($handler)) {
|
|
||||||
$handler = $this->handle($handler, $params);
|
|
||||||
}
|
|
||||||
$this->channel->push(serialize($handler));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace Kiri\Task;
|
namespace Kiri\Task;
|
||||||
|
|
||||||
|
use JetBrains\PhpStorm\Pure;
|
||||||
use Kiri\Abstracts\Component;
|
use Kiri\Abstracts\Component;
|
||||||
use Kiri\Core\HashMap;
|
use Kiri\Core\HashMap;
|
||||||
use Psr\Container\ContainerExceptionInterface;
|
use Psr\Container\ContainerExceptionInterface;
|
||||||
@@ -58,6 +59,16 @@ class TaskManager extends Component
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $key
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
#[Pure] public function has(string $key): bool
|
||||||
|
{
|
||||||
|
return $this->hashMap->has($key);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $key
|
* @param string $key
|
||||||
* @return OnTaskInterface
|
* @return OnTaskInterface
|
||||||
@@ -69,6 +80,9 @@ class TaskManager extends Component
|
|||||||
$task = $this->hashMap->get($key);
|
$task = $this->hashMap->get($key);
|
||||||
if (is_string($task)) {
|
if (is_string($task)) {
|
||||||
$task = $this->getContainer()->get($task);
|
$task = $this->getContainer()->get($task);
|
||||||
|
if (!empty($task)) {
|
||||||
|
$this->add($key, $task);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return $task;
|
return $task;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,63 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Kiri\Task;
|
|
||||||
|
|
||||||
use Kiri\Server\Abstracts\BaseProcess;
|
|
||||||
use Swoole\Process;
|
|
||||||
|
|
||||||
class TaskProcess extends BaseProcess
|
|
||||||
{
|
|
||||||
|
|
||||||
|
|
||||||
protected bool $enable_coroutine = false;
|
|
||||||
|
|
||||||
|
|
||||||
public int $index = 0;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param int $index
|
|
||||||
*/
|
|
||||||
public function setIndex(int $index): void
|
|
||||||
{
|
|
||||||
$this->index = $index;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getName(): string
|
|
||||||
{
|
|
||||||
return 'task.' . $this->index;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param Process $process
|
|
||||||
* @throws \Exception
|
|
||||||
*/
|
|
||||||
public function process(Process $process): void
|
|
||||||
{
|
|
||||||
$task = \Kiri::getContainer()->get(OnServerTask::class);
|
|
||||||
while (!$this->isStop()) {
|
|
||||||
$read = $process->read();
|
|
||||||
|
|
||||||
$task->onTask(null, 0, 0, $read);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return $this
|
|
||||||
*/
|
|
||||||
public function onSigterm(): static
|
|
||||||
{
|
|
||||||
pcntl_signal(SIGTERM, function () {
|
|
||||||
$this->isStop = true;
|
|
||||||
});
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,31 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Kiri\Task;
|
|
||||||
|
|
||||||
use Exception;
|
|
||||||
use ReflectionException;
|
|
||||||
|
|
||||||
trait TaskResolve
|
|
||||||
{
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @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->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