modify plugin name

This commit is contained in:
2022-06-16 17:38:23 +08:00
parent 10de6b5246
commit 4daad7d111
22 changed files with 1206 additions and 70 deletions
+4 -4
View File
@@ -4,7 +4,7 @@ namespace Kiri\Task\Annotation;
use Kiri\Annotation\AbstractAttribute;
use Kiri\Task\TaskManager;
use Kiri\Task\TaskContainer;
#[\Attribute(\Attribute::TARGET_CLASS)] class AsynchronousTask extends AbstractAttribute
{
@@ -25,9 +25,9 @@ use Kiri\Task\TaskManager;
*/
public function execute(mixed $class, mixed $method = ''): mixed
{
$AsyncTaskExecute = \Kiri::getDi()->get(TaskManager::class);
$AsyncTaskExecute->add($this->name, $class::class);
return parent::execute($class, $method); // TODO: Change the autogenerated stub
// TODO: Change the autogenerated stub
di(TaskContainer::class)->add($this->name, $class::class);
return parent::execute($class, $method);
}
}
+3 -3
View File
@@ -30,7 +30,7 @@ class OnServerTask
* @param mixed $data
* @throws ConfigException
*/
public function onTask(Server $server, int $task_id, int $src_worker_id, mixed $data)
public function onTask(Server $server, int $task_id, int $src_worker_id, mixed $data): void
{
try {
$data = $this->resolve($data);
@@ -49,7 +49,7 @@ class OnServerTask
* @param Server\Task $task
* @throws ConfigException
*/
public function onCoroutineTask(?Server $server, Server\Task $task)
public function onCoroutineTask(?Server $server, Server\Task $task): void
{
try {
$data = $this->resolve($task->data);
@@ -82,7 +82,7 @@ class OnServerTask
* @param int $task_id
* @param mixed $data
*/
public function onFinish(Server $server, int $task_id, mixed $data)
public function onFinish(Server $server, int $task_id, mixed $data): void
{
if (!($data instanceof OnTaskInterface)) {
return;
+1 -1
View File
@@ -12,6 +12,6 @@ interface OnTaskInterface
public function execute();
public function finish(Server $server, int $task_id);
public function finish(?Server $server, int $task_id);
}
@@ -7,11 +7,11 @@ use JetBrains\PhpStorm\Pure;
use Kiri\Abstracts\Component;
use Kiri\Core\HashMap;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use Kiri\Di\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
use Swoole\Server;
class TaskManager extends Component
class TaskContainer extends Component
{
@@ -30,30 +30,6 @@ class TaskManager extends Component
}
/**
* @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->container->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
@@ -5,26 +5,28 @@ namespace Kiri\Task;
use Exception;
use Kiri;
use Kiri\Abstracts\Component;
use Kiri\Server\SwooleServerInterface;
use Kiri\Server\ServerInterface;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use Kiri\Di\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
use Swoole\Coroutine;
use Swoole\Server;
/**
*
*/
class AsyncTaskExecute extends Component
class TaskExecute extends Component
{
/**
* @param TaskManager $hashMap
* @param TaskContainer $hashMap
* @param ContainerInterface $container
* @param array $config
* @throws Exception
*/
public function __construct(public TaskManager $hashMap, public ContainerInterface $container, array $config = [])
public function __construct(public TaskContainer $hashMap, public ContainerInterface $container, array $config = [])
{
parent::__construct($config);
}
@@ -43,16 +45,45 @@ class AsyncTaskExecute extends Component
if (is_string($handler)) {
$handler = $this->handle($handler, $params);
}
if ($this->container->has(SwooleServerInterface::class)) {
$server = $this->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);
if ($this->container->has(ServerInterface::class)) {
$this->onAsyncExec($handler, $workerId);
} else {
Coroutine::create(fn() => $this->onCoronExec($handler));
}
}
/**
* @param OnTaskInterface|string $handler
* @param int $workerId
* @return bool
* @throws Exception
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
protected function onAsyncExec(OnTaskInterface|string $handler, int $workerId = -1): bool
{
/** @var Server $server */
$server = $this->container->get(ServerInterface::class);
if ($workerId < 0 || $workerId > $server->setting['task_worker_num']) {
$workerId = random_int(0, $server->setting['task_worker_num'] - 1);
}
return (bool)$server->task(serialize($handler), $workerId);
}
/**
* @param OnTaskInterface|string $handler
* @return bool
*/
protected function onCoronExec(OnTaskInterface|string $handler): bool
{
$handler->execute();
$handler->finish(null, Coroutine::getCid());
return true;
}
/**
* @param $handler
* @param $params