diff --git a/src/Controller/OgAgent/RunScriptAction.php b/src/Controller/OgAgent/RunScriptAction.php index b48917f..d38f5f3 100644 --- a/src/Controller/OgAgent/RunScriptAction.php +++ b/src/Controller/OgAgent/RunScriptAction.php @@ -38,7 +38,7 @@ class RunScriptAction extends AbstractOgAgentController } $data = $this->buildRequestData($client, $input->script, $input->runAsAdmin); - $response = $this->executeScript($client, $data); + $response = $this->executeScript($client, $data, $input); if (isset($response['code']) && $response['code'] === 409) { continue; @@ -86,7 +86,7 @@ class RunScriptAction extends AbstractOgAgentController return $client->getStatus() === ClientStatus::LINUX_SESSION || $client->getStatus() === ClientStatus::WINDOWS_SESSION; } - private function executeScript(Client $client, array $data): array + private function executeScript(Client $client, array $data, CommandExecuteInput $input): array { $response = $this->createRequest( method: 'POST', @@ -98,18 +98,32 @@ class RunScriptAction extends AbstractOgAgentController ); if (isset($response['code']) && $response['code'] === 409) { - $this->createService->__invoke( - $client, - CommandTypes::RUN_SCRIPT, - TraceStatus::BUSY, - null, - ['script' => $data['script'] ?? $data['scp'] ?? ''], - '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() - ]); + 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; diff --git a/tests/Functional/RunScriptQueueTest.php b/tests/Functional/RunScriptQueueTest.php new file mode 100644 index 0000000..747f77b --- /dev/null +++ b/tests/Functional/RunScriptQueueTest.php @@ -0,0 +1,90 @@ +get(EntityManagerInterface::class), + $httpClient, + $container->get(CreatePartitionService::class), + $container->get(LoggerInterface::class), + $container->get(CreateService::class), + $container->get(CreateRepositoryAction::class), + $container->get(SshKeyAction::class), + 'false', + ); + } + + public function testBusyClientWithQueueCreatesPendingTraceNotBusy(): void + { + /** @var Client $client */ + $client = ClientFactory::createOne(['ip' => '127.0.0.1', 'token' => 'fake-token'])->_real(); + + $mockHttpClient = new MockHttpClient(fn() => new MockResponse('{"message":"busy"}', ['http_code' => 409])); + + $input = new CommandExecuteInput(); + $input->clients = [new ClientOutput($client)]; + $input->script = 'echo hello'; + $input->queue = true; + + $this->buildRunScriptAction($mockHttpClient)->__invoke($input); + + $trace = static::getContainer()->get('doctrine')->getRepository(Trace::class) + ->findOneBy(['client' => $client, 'command' => CommandTypes::RUN_SCRIPT]); + + $this->assertNotNull($trace, 'Debe haberse creado una traza para el script'); + $this->assertSame(TraceStatus::PENDING, $trace->getStatus(), 'Con "encolar" marcado, la traza debe quedar PENDING (para que el reintento la recoja), no BUSY'); + } + + public function testBusyClientWithoutQueueCreatesBusyTrace(): void + { + /** @var Client $client */ + $client = ClientFactory::createOne(['ip' => '127.0.0.1', 'token' => 'fake-token'])->_real(); + + $mockHttpClient = new MockHttpClient(fn() => new MockResponse('{"message":"busy"}', ['http_code' => 409])); + + $input = new CommandExecuteInput(); + $input->clients = [new ClientOutput($client)]; + $input->script = 'echo hello'; + $input->queue = false; + + $this->buildRunScriptAction($mockHttpClient)->__invoke($input); + + $trace = static::getContainer()->get('doctrine')->getRepository(Trace::class) + ->findOneBy(['client' => $client, 'command' => CommandTypes::RUN_SCRIPT]); + + $this->assertNotNull($trace); + $this->assertSame(TraceStatus::BUSY, $trace->getStatus(), 'Sin "encolar", debe mantenerse el comportamiento previo (traza BUSY, sin reintento)'); + } +}