diff --git a/CHANGELOG.md b/CHANGELOG.md index 3d6aa8c..39e1ca2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Changelog +## [1.12.0] - 2026-10-20 + +### Added +- **Cola y Despacho Asíncrono de Comandos (`AbstractOgAgentController`, `RunScriptAction`, `RebootAction`, `PowerOffAction`, `PartitionAssistantAction`, `RefreshAction`, `DeployImageAction`)**: + - Implementada detección temprana de clientes offline (`isClientOffline`): cuando un equipo está apagado o no disponible, sus acciones o scripts se encolan directamente en estado `PENDING` en la base de datos sin realizar peticiones de red bloqueantes, eliminando los cuelgues del servidor por timeouts prolongados al enviar comandos masivos en aulas. + - Implementado envío HTTP asíncrono y en paralelo (`sendAsyncRequest` / Guzzle Promises) para clientes encendidos en todas las acciones principales de ogAgent, optimizando la concurrencia y reduciendo drásticamente el tiempo de respuesta de la API. + ## [1.11.3] - 2026-10-14 ### Fixed diff --git a/src/Controller/DeployImageAction.php b/src/Controller/DeployImageAction.php index d1cfe13..2139cde 100644 --- a/src/Controller/DeployImageAction.php +++ b/src/Controller/DeployImageAction.php @@ -133,6 +133,16 @@ class DeployImageAction extends AbstractController $repositoryError = $repositoryErrors[$this->getRepositoryKey($client)] ?? null; + if ($this->deployImageOgAgentAction->isClientOffline($client)) { + $this->logger->info(sprintf('Client is offline, queueing deployment directly (%s)', $methodLabel), [ + 'client_uuid' => $client->getUuid() + ]); + + $this->queueDeployment($client, $input, $inputData); + + continue; + } + if ($repositoryError !== null) { $this->logger->warning(sprintf('Error with OgRepository for client (%s)', $methodLabel), [ 'client_uuid' => $client->getUuid(), diff --git a/src/Controller/OgAgent/AbstractOgAgentController.php b/src/Controller/OgAgent/AbstractOgAgentController.php index 166b708..e982d6c 100644 --- a/src/Controller/OgAgent/AbstractOgAgentController.php +++ b/src/Controller/OgAgent/AbstractOgAgentController.php @@ -4,6 +4,8 @@ namespace App\Controller\OgAgent; use App\Controller\OgRepository\Git\CreateRepositoryAction; use App\Controller\OgRepository\Git\SshKeyAction; +use App\Entity\Client; +use App\Model\ClientStatus; use App\Service\CreatePartitionService; use App\Service\Trace\CreateService; use Doctrine\ORM\EntityManagerInterface; @@ -36,18 +38,41 @@ abstract class AbstractOgAgentController extends AbstractController { } + public function isClientOffline(?Client $client): bool + { + if ($client === null) { + return true; + } + + return in_array($client->getStatus(), [ + ClientStatus::OFF, + ClientStatus::DISCONNECTED, + ClientStatus::TURNING_OFF, + ], true); + } + protected function buildRequestOptions(array $params = [], ?string $token = null): array { - $options = array_merge($params, [ - 'headers' => [ - 'accept' => 'application/json', - 'Content-Type' => 'application/json', - 'Authorization' => $token, - ], + $defaultHeaders = [ + 'accept' => 'application/json', + 'Content-Type' => 'application/json', + ]; + + if ($token !== null) { + $defaultHeaders['Authorization'] = $token; + } + + $defaultOptions = [ + 'headers' => $defaultHeaders, 'verify_peer' => false, 'verify_host' => false, 'timeout' => 10, - ]); + ]; + + $options = array_merge($defaultOptions, $params); + if (isset($params['headers'])) { + $options['headers'] = array_merge($defaultHeaders, $params['headers']); + } if ($this->sslEnabled === 'true') { $options['verify_peer'] = true; @@ -60,6 +85,13 @@ abstract class AbstractOgAgentController extends AbstractController return $options; } + public function sendAsyncRequest(string $method, string $url, array $params = [], ?string $token = null): ResponseInterface + { + $options = $this->buildRequestOptions($params, $token); + + return $this->httpClient->request($method, $url, $options); + } + /** * @throws TransportExceptionInterface * @throws ServerExceptionInterface diff --git a/src/Controller/OgAgent/PartitionAssistantAction.php b/src/Controller/OgAgent/PartitionAssistantAction.php index 119a534..e47a5ec 100644 --- a/src/Controller/OgAgent/PartitionAssistantAction.php +++ b/src/Controller/OgAgent/PartitionAssistantAction.php @@ -79,6 +79,24 @@ class PartitionAssistantAction extends AbstractOgAgentController ]; } + // Detección temprana: si el equipo está offline y se solicita encolado, registrar traza PENDING sin esperar red + if ($this->isClientOffline($client)) { + if ($input->queue) { + foreach ($disks as $diskNumber => $diskInfo) { + $data = []; + if (!empty($diskInfo['diskData'])) { + $data[] = $diskInfo['diskData']; + } + $data = array_merge($data, $diskInfo['partitionData']); + + $this->createService->__invoke($client, CommandTypes::PARTITION_AND_FORMAT, TraceStatus::PENDING, null, $data); + } + $this->logger->info('Partition assistant queued (PENDING) for offline client', ['client' => $client->getId()]); + } + continue; + } + + $pending = []; foreach ($disks as $diskNumber => $diskInfo) { $data = []; if (!empty($diskInfo['diskData'])) { @@ -92,28 +110,36 @@ class PartitionAssistantAction extends AbstractOgAgentController "cfg" => $data, "ids" => "0" ]; + $url = 'https://' . $client->getIp() . ':8000/opengnsys/Configurar'; - $response = $this->createRequest( - method: 'POST', - url: 'https://'.$client->getIp().':8000/opengnsys/Configurar', - params: [ - 'json' => $result, - ], - token: $client->getToken(), - ); + try { + $pending[] = [ + 'client' => $client, + 'data' => $data, + 'url' => $url, + 'response' => $this->sendAsyncRequest('POST', $url, ['json' => $result], $client->getToken()), + ]; + } catch (\Exception $e) { + if ($input->queue) { + $this->createService->__invoke($client, CommandTypes::PARTITION_AND_FORMAT, TraceStatus::PENDING, null, $data); + } + } + } + + foreach ($pending as $item) { + $response = $this->readResponse($item['response'], $item['url']); + $data = $item['data']; $this->logger->info('Partition assistant', ['client' => $client->getId()]); if (isset($response['error']) && $response['code'] === Response::HTTP_INTERNAL_SERVER_ERROR) { if ($input->queue) { $this->createService->__invoke($client, CommandTypes::PARTITION_AND_FORMAT, TraceStatus::PENDING, null, $data); - continue; } - continue; } - $jobId = $response['job_id']; + $jobId = $response['job_id'] ?? null; $client->setStatus(ClientStatus::BUSY); $this->entityManager->persist($client); @@ -128,7 +154,9 @@ class PartitionAssistantAction extends AbstractOgAgentController $this->createService->__invoke($client, CommandTypes::PARTITION_AND_FORMAT, TraceStatus::IN_PROGRESS, $jobId, $data); } - $clientJobs['/clients/' . $client->getUuid()] = $jobId; + if ($jobId) { + $clientJobs['/clients/' . $client->getUuid()] = $jobId; + } } } diff --git a/src/Controller/OgAgent/PowerOffAction.php b/src/Controller/OgAgent/PowerOffAction.php index 9441979..36f777c 100644 --- a/src/Controller/OgAgent/PowerOffAction.php +++ b/src/Controller/OgAgent/PowerOffAction.php @@ -39,6 +39,8 @@ class PowerOffAction extends AbstractOgAgentController */ public function __invoke(MultipleClientsInput $input): JsonResponse { + $pending = []; + foreach ($input->clients as $clientEntity) { /** @var Client $client */ $client = $clientEntity->getEntity(); @@ -47,7 +49,10 @@ class PowerOffAction extends AbstractOgAgentController throw new BadRequestHttpException('IP is required'); } - if ($client->getStatus() === ClientStatus::OFF || $client->getStatus() === ClientStatus::TURNING_OFF || $client->getStatus() === ClientStatus::DISCONNECTED) { + if ($this->isClientOffline($client)) { + if ($input->queue) { + $this->createService->__invoke($client, CommandTypes::SHUTDOWN, TraceStatus::PENDING, null, []); + } continue; } @@ -55,15 +60,24 @@ class PowerOffAction extends AbstractOgAgentController 'nfn' => 'Apagar', 'ids' => '0' ]; + $url = 'https://' . $client->getIp() . ':8000/opengnsys/Apagar'; - $response = $this->createRequest( - method: 'POST', - url: 'https://'.$client->getIp().':8000/opengnsys/Apagar', - params: [ - 'json' => $data, - ], - token: $client->getToken(), - ); + try { + $pending[] = [ + 'client' => $client, + 'url' => $url, + 'response' => $this->sendAsyncRequest('POST', $url, ['json' => $data], $client->getToken()), + ]; + } catch (\Exception $e) { + if ($input->queue) { + $this->createService->__invoke($client, CommandTypes::SHUTDOWN, TraceStatus::PENDING, null, []); + } + } + } + + foreach ($pending as $item) { + $client = $item['client']; + $response = $this->readResponse($item['response'], $item['url']); $isBusy = isset($response['code']) && $response['code'] === Response::HTTP_CONFLICT; $isError = isset($response['error']) && $response['code'] === Response::HTTP_INTERNAL_SERVER_ERROR; @@ -80,7 +94,7 @@ class PowerOffAction extends AbstractOgAgentController $this->logger->info('Powering off client', ['client' => $client->getId()]); - $jobId = $response['job_id']; + $jobId = $response['job_id'] ?? null; $client->setStatus(ClientStatus::TURNING_OFF); $this->entityManager->persist($client); diff --git a/src/Controller/OgAgent/RebootAction.php b/src/Controller/OgAgent/RebootAction.php index 26f1228..18c1d7a 100644 --- a/src/Controller/OgAgent/RebootAction.php +++ b/src/Controller/OgAgent/RebootAction.php @@ -27,28 +27,47 @@ class RebootAction extends AbstractOgAgentController */ public function __invoke(MultipleClientsInput $input): JsonResponse { + $pending = []; + foreach ($input->clients as $clientEntity) { /** @var Client $client */ $client = $clientEntity->getEntity(); - if (!$client->getIp()) { throw new BadRequestHttpException('IP is required'); } + // Detección temprana: equipos apagados o desconectados + if ($this->isClientOffline($client)) { + if ($input->queue) { + $this->createService->__invoke($client, CommandTypes::REBOOT, TraceStatus::PENDING, null, []); + $this->logger->info('Reboot queued (PENDING) for offline client', ['client' => $client->getId()]); + } + continue; + } + $data = [ 'nfn' => 'Reiniciar', 'ids' => '0' ]; + $url = 'https://' . $client->getIp() . ':8000/opengnsys/Reiniciar'; - $response = $this->createRequest( - method: 'POST', - url: 'https://'.$client->getIp().':8000/opengnsys/Reiniciar', - params: [ - 'json' => $data, - ], - token: $client->getToken(), - ); + try { + $pending[] = [ + 'client' => $client, + 'url' => $url, + 'response' => $this->sendAsyncRequest('POST', $url, ['json' => $data], $client->getToken()), + ]; + } catch (\Exception $e) { + if ($input->queue) { + $this->createService->__invoke($client, CommandTypes::REBOOT, TraceStatus::PENDING, null, []); + } + } + } + + foreach ($pending as $item) { + $client = $item['client']; + $response = $this->readResponse($item['response'], $item['url']); $isBusy = isset($response['code']) && $response['code'] === Response::HTTP_CONFLICT; $isError = isset($response['error']) && $response['code'] === Response::HTTP_INTERNAL_SERVER_ERROR; @@ -62,7 +81,7 @@ class RebootAction extends AbstractOgAgentController $this->logger->info('Rebooting client', ['client' => $client->getId()]); - $jobId = $response['job_id']; + $jobId = $response['job_id'] ?? null; $client->setStatus(ClientStatus::INITIALIZING); $this->entityManager->persist($client); @@ -71,6 +90,6 @@ class RebootAction extends AbstractOgAgentController $this->createService->__invoke($client, CommandTypes::REBOOT, TraceStatus::SUCCESS, $jobId, []); } - return new JsonResponse(data: $client, status: Response::HTTP_OK); + return new JsonResponse(data: [], status: Response::HTTP_OK); } } diff --git a/src/Controller/OgAgent/RefreshAction.php b/src/Controller/OgAgent/RefreshAction.php index 2107a2d..8bbf606 100644 --- a/src/Controller/OgAgent/RefreshAction.php +++ b/src/Controller/OgAgent/RefreshAction.php @@ -39,6 +39,8 @@ class RefreshAction extends AbstractOgAgentController */ public function __invoke(MultipleClientsInput $input): JsonResponse { + $pending = []; + foreach ($input->clients as $clientEntity) { /** @var Client $client */ $client = $clientEntity->getEntity(); @@ -57,24 +59,31 @@ class RefreshAction extends AbstractOgAgentController 'nfn' => 'Actualizar', 'ids' => '0' ]; + $url = 'https://' . $client->getIp() . ':8000/ogAdmClient/Actualizar'; - $response = $this->createRequest( - method: 'POST', - url: 'https://'.$client->getIp().':8000/ogAdmClient/Actualizar', - params: [ - 'json' => $data, - ], - token: $client->getToken(), - ); + try { + $pending[] = [ + 'client' => $client, + 'url' => $url, + 'response' => $this->sendAsyncRequest('POST', $url, ['json' => $data], $client->getToken()), + ]; + } catch (\Exception $e) { + if ($input->queue) { + $this->createService->__invoke($client, CommandTypes::REFRESH, TraceStatus::PENDING, null, []); + } + } + } + + foreach ($pending as $item) { + $client = $item['client']; + $response = $this->readResponse($item['response'], $item['url']); if (isset($response['error']) && $response['code'] === Response::HTTP_INTERNAL_SERVER_ERROR) { $this->logger->error('Error refreshing client', ['client' => $client->getId(), 'error' => $response['error']]); if ($input->queue) { $this->createService->__invoke($client, CommandTypes::REFRESH, TraceStatus::PENDING, null, []); - continue; } - continue; } @@ -82,9 +91,6 @@ class RefreshAction extends AbstractOgAgentController $jobId = $response['job_id'] ?? null; - // We do not change the status here, it remains OG_LIVE - // $client->setStatus(ClientStatus::OG_LIVE); - $this->createService->__invoke($client, CommandTypes::REFRESH, TraceStatus::SUCCESS, $jobId, []); } diff --git a/src/Controller/OgAgent/RunScriptAction.php b/src/Controller/OgAgent/RunScriptAction.php index cad400a..1cf1279 100644 --- a/src/Controller/OgAgent/RunScriptAction.php +++ b/src/Controller/OgAgent/RunScriptAction.php @@ -27,6 +27,7 @@ class RunScriptAction extends AbstractOgAgentController public function __invoke(CommandExecuteInput $input, ?Trace $existingTrace = null): JsonResponse { $clientJobs = []; + $pending = []; /** @var Client $clientEntity */ foreach ($input->clients as $clientEntity) { @@ -37,10 +38,76 @@ class RunScriptAction extends AbstractOgAgentController throw new BadRequestHttpException('IP is required'); } + // Detección temprana: si el equipo está apagado o desconectado, no esperar timeout de red + if ($this->isClientOffline($client)) { + if ($input->queue) { + $this->createService->__invoke( + $client, + CommandTypes::RUN_SCRIPT, + TraceStatus::PENDING, + null, + ['script' => $input->script] + ); + $this->logger->info('Script queued (PENDING) for offline client', [ + 'client_ip' => $client->getIp() + ]); + } else { + $this->logger->info('Script skipped for offline client (queue disabled)', [ + 'client_ip' => $client->getIp() + ]); + } + continue; + } + + // Para equipos encendidos, lanzar la petición asíncrona concurrente $data = $this->buildRequestData($client, $input->script, $input->runAsAdmin); - $response = $this->executeScript($client, $data, $input); + $url = 'https://' . $client->getIp() . ':8000/opengnsys/EjecutarScript'; + + try { + $pending[] = [ + 'client' => $client, + 'url' => $url, + 'response' => $this->sendAsyncRequest('POST', $url, ['json' => $data], $client->getToken()), + ]; + } catch (\Exception $e) { + $this->handleError($client, $input, [ + 'code' => Response::HTTP_INTERNAL_SERVER_ERROR, + 'error' => $e->getMessage() + ]); + } + } + + // Fase 2: Recolectar las respuestas de los equipos que estaban en línea + foreach ($pending as $item) { + $client = $item['client']; + $url = $item['url']; + $response = $this->readResponse($item['response'], $url); if (isset($response['code']) && $response['code'] === 409) { + if ($input->queue) { + $this->createService->__invoke( + $client, + CommandTypes::RUN_SCRIPT, + TraceStatus::PENDING, + null, + ['script' => $input->script] + ); + $this->logger->info('Trace queued (PENDING) due to 409 busy response', [ + 'client_ip' => $client->getIp() + ]); + } else { + $this->createService->__invoke( + $client, + CommandTypes::RUN_SCRIPT, + TraceStatus::BUSY, + null, + ['script' => $input->script], + 'Some job is already running, refusing to launch another one' + ); + $this->logger->info('Trace created as busy due to 409 response', [ + 'client_ip' => $client->getIp() + ]); + } continue; } @@ -49,8 +116,10 @@ class RunScriptAction extends AbstractOgAgentController continue; } - $jobId = $response['job_id']; - $clientJobs[(string) '/clients/' . $client->getUuid()] = $jobId; + $jobId = $response['job_id'] ?? null; + if ($jobId) { + $clientJobs[(string) '/clients/' . $client->getUuid()] = $jobId; + } $this->handleSuccess($client, $input, $response, $existingTrace); } @@ -86,49 +155,6 @@ class RunScriptAction extends AbstractOgAgentController return $client->getStatus() === ClientStatus::LINUX_SESSION || $client->getStatus() === ClientStatus::WINDOWS_SESSION; } - private function executeScript(Client $client, array $data, CommandExecuteInput $input): array - { - $response = $this->createRequest( - method: 'POST', - url: 'https://'.$client->getIp().':8000/opengnsys/EjecutarScript', - params: [ - 'json' => $data, - ], - token: $client->getToken(), - ); - - if (isset($response['code']) && $response['code'] === 409) { - if ($input->queue) { - $this->createService->__invoke( - $client, - CommandTypes::RUN_SCRIPT, - TraceStatus::PENDING, - null, - ['script' => $input->script] - ); - - $this->logger->info('Trace queued (PENDING) due to 409 busy response', [ - 'client_ip' => $client->getIp() - ]); - } else { - $this->createService->__invoke( - $client, - CommandTypes::RUN_SCRIPT, - TraceStatus::BUSY, - null, - ['script' => $input->script], - 'Some job is already running, refusing to launch another one' - ); - - $this->logger->info('Trace created as busy due to 409 response', [ - 'client_ip' => $client->getIp() - ]); - } - } - - return $response; - } - private function isErrorResponse(array $response): bool { return isset($response['error']) && $response['code'] === Response::HTTP_INTERNAL_SERVER_ERROR;