ref 3005. Fixed command scheldule bug.

pull/125/head
Manuel Aranda Rosales 2026-07-13 10:32:15 +02:00
parent 04068ce748
commit ec48baebd3
2 changed files with 118 additions and 14 deletions

View File

@ -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;

View File

@ -0,0 +1,90 @@
<?php
namespace Functional;
use App\Controller\OgAgent\CreateImageAction;
use App\Controller\OgAgent\RunScriptAction;
use App\Controller\OgRepository\Git\CreateRepositoryAction;
use App\Controller\OgRepository\Git\SshKeyAction;
use App\Dto\Input\CommandExecuteInput;
use App\Dto\Output\ClientOutput;
use App\Entity\Client;
use App\Entity\Trace;
use App\Factory\ClientFactory;
use App\Model\CommandTypes;
use App\Model\TraceStatus;
use App\Service\CreatePartitionService;
use App\Service\Trace\CreateService;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\Response\MockResponse;
/**
* Reproduce el bug reportado: si el cliente está ocupado (el agente responde
* 409) y el usuario marcó "encolar tarea", ejecutar un script debía crear
* una traza PENDING para que opengnsys:execute-pending-traces la reintentase
* al terminar la tarea anterior. Antes del fix, esta rama ignoraba el flag
* "queue" por completo y siempre creaba una traza BUSY (que el comando de
* reintento nunca recoge, porque solo busca traces en PENDING).
*/
class RunScriptQueueTest extends AbstractTest
{
private function buildRunScriptAction(MockHttpClient $httpClient): RunScriptAction
{
$container = static::getContainer();
return new RunScriptAction(
$container->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)');
}
}