modify plugin name
This commit is contained in:
@@ -6,7 +6,9 @@ use Exception;
|
||||
use Kiri;
|
||||
use Kiri\Abstracts\Component;
|
||||
use Kiri\Server\SwooleServerInterface;
|
||||
use Swoole\Coroutine;
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
use Swoole\Process;
|
||||
|
||||
|
||||
/**
|
||||
@@ -19,10 +21,42 @@ class AsyncTaskExecute extends Component
|
||||
use TaskResolve;
|
||||
|
||||
|
||||
private int $total = 50;
|
||||
|
||||
/**
|
||||
* @param int $total
|
||||
*/
|
||||
public function setTotal(int $total): void
|
||||
{
|
||||
$this->total = $total;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return void
|
||||
* @throws Kiri\Exception\ConfigException
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
public function start()
|
||||
{
|
||||
$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');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param OnTaskInterface|string $handler
|
||||
* @param array $params
|
||||
* @param int $workerId
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
* @throws \ReflectionException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function execute(OnTaskInterface|string $handler, array $params = [], int $workerId = -1)
|
||||
@@ -30,12 +64,24 @@ class AsyncTaskExecute extends Component
|
||||
if (is_string($handler)) {
|
||||
$handler = $this->handle($handler, $params);
|
||||
}
|
||||
$container = $this->getContainer();
|
||||
if ($container->has(SwooleServerInterface::class)) {
|
||||
$server = $container->get(SwooleServerInterface::class);
|
||||
if ($workerId < 0 || $workerId > $server->setting['task_worker_num']) {
|
||||
$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);
|
||||
}
|
||||
|
||||
$server = Kiri::getDi()->get(SwooleServerInterface::class);
|
||||
if ($workerId < 0 || $workerId > $server->setting['task_worker_num']) {
|
||||
$workerId = random_int(0, $server->setting['task_worker_num'] - 1);
|
||||
$processManager = $container->get(Kiri\Server\ProcessManager::class);
|
||||
|
||||
/** @var Process $process */
|
||||
$process = $processManager->get('task.' . $workerId, 'tasker');
|
||||
$process->write(serialize($handler));
|
||||
}
|
||||
$server->task(serialize($handler), $workerId);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -27,10 +27,19 @@ class CoroutineTaskExecute extends Component
|
||||
private int $total = 50;
|
||||
|
||||
|
||||
/**
|
||||
* @param int $total
|
||||
*/
|
||||
public function setTotal(int $total): void
|
||||
{
|
||||
$this->total = $total;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function init()
|
||||
public function start()
|
||||
{
|
||||
$this->hashMap = new HashMap();
|
||||
|
||||
@@ -40,7 +49,7 @@ class CoroutineTaskExecute extends Component
|
||||
|
||||
Coroutine::create(function () {
|
||||
$barrier = Coroutine\Barrier::make();
|
||||
for ($i = 0; $i < 50; $i++) {
|
||||
for ($i = 0; $i < $this->total; $i++) {
|
||||
Coroutine::create(function () {
|
||||
$this->handler();
|
||||
});
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
<?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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user