Files
kiri-http-server/Task/TaskExecute.php
T

77 lines
2.0 KiB
PHP
Raw Permalink Normal View History

2023-08-17 17:04:55 +08:00
<?php
namespace Kiri\Server\Task;
2025-07-11 14:44:06 +08:00
use Exception;
2023-08-17 17:04:55 +08:00
use Kiri\Server\ServerInterface;
class TaskExecute implements TaskInterface
{
2025-07-11 11:50:54 +08:00
2023-08-17 17:04:55 +08:00
/**
2025-07-11 14:44:06 +08:00
* @param string $handler
2025-07-11 11:50:54 +08:00
* @param mixed $data
* @param int $dstWorkerId
* @param callable|null $finishFinishCallback
* @return void
2025-07-11 14:44:06 +08:00
* @throws
2025-07-11 11:50:54 +08:00
*/
2025-07-11 14:44:06 +08:00
public function task(string $handler, mixed $data, int $dstWorkerId = -1, ?callable $finishFinishCallback = null): void
2025-07-11 11:50:54 +08:00
{
2025-07-11 14:51:53 +08:00
$array = class_implements($handler, true);
2025-07-11 14:44:06 +08:00
if (!in_array(OnTaskInterface::class, $array, true)) {
throw new Exception('Task is not implement OnTaskInterface');
}
2025-07-11 11:50:54 +08:00
$server = \Kiri::getDi()->get(ServerInterface::class);
$server->task([$handler, $data], $dstWorkerId, $finishFinishCallback);
}
/**
2025-07-11 14:44:06 +08:00
* @param string $handler
2023-08-17 17:04:55 +08:00
* @param mixed $data
* @param float $timeout
* @param int $dstWorkerId
* @return mixed
2025-07-11 14:44:06 +08:00
* @throws
2023-08-17 17:04:55 +08:00
*/
2025-07-11 14:44:06 +08:00
public function taskWait(string $handler, mixed $data, float $timeout = 0.5, int $dstWorkerId = -1): mixed
2023-08-17 17:04:55 +08:00
{
2025-07-11 14:51:53 +08:00
$array = class_implements($handler, true);
2025-07-11 14:44:06 +08:00
if (!in_array(OnTaskInterface::class, $array, true)) {
throw new Exception('Task is not implement OnTaskInterface');
}
2024-08-29 17:01:07 +08:00
$server = \Kiri::getDi()->get(ServerInterface::class);
2025-07-11 11:50:54 +08:00
return $server->taskwait([$handler, $data], $timeout, $dstWorkerId);
2023-08-17 17:04:55 +08:00
}
/**
* @param array $tasks
* @param float $timeout
* @return false|array
*/
public function taskCo(array $tasks, float $timeout = 0.5): false|array
{
2024-08-29 17:01:07 +08:00
$server = \Kiri::getDi()->get(ServerInterface::class);
return $server->taskCo($tasks, $timeout);
2023-08-17 17:04:55 +08:00
}
/**
* @param array $tasks
* @param float $timeout
* @return false|array
*/
public function taskWaitMulti(array $tasks, float $timeout = 0.5): false|array
{
2024-08-29 17:01:07 +08:00
$server = \Kiri::getDi()->get(ServerInterface::class);
return $server->taskWaitMulti($tasks, $timeout);
2023-08-17 17:04:55 +08:00
}
2025-07-11 11:50:54 +08:00
}