ref 3018. Cancel multiple task
ogcore-debian-package/pipeline/head This commit looks good Details
testing/ogcore-api/pipeline/head There was a failure building this commit Details

pull/125/head
Manuel Aranda Rosales 2026-07-19 17:35:15 +02:00
parent d2cbc51336
commit f2566bb9c9
3 changed files with 135 additions and 59 deletions

View File

@ -5,7 +5,9 @@ namespace App\Controller;
use App\Dto\Input\CancelMultipleTracesInput;
use App\Entity\Trace;
use App\Model\TraceStatus;
use App\Service\Trace\KillJobService;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
@ -14,22 +16,44 @@ class CancelMultipleTracesAction extends AbstractController
{
public function __construct(
private readonly EntityManagerInterface $entityManager,
private readonly KillJobService $killJobService,
private readonly LoggerInterface $logger,
)
{
}
public function __invoke(CancelMultipleTracesInput $input): JsonResponse
{
foreach ($input->traces as $trace) {
/** @var Trace $trace */
$trace = $trace->getEntity();
$cancelled = [];
$failed = [];
$trace->setStatus(TraceStatus::CANCELLED);
$this->entityManager->persist($trace);
foreach ($input->traces as $traceOutput) {
/** @var Trace $trace */
$trace = $traceOutput->getEntity();
try {
if ($trace->getStatus() === TraceStatus::IN_PROGRESS) {
$this->killJobService->kill($trace, (string) $trace->getJobId());
} elseif ($trace->getStatus() === TraceStatus::PENDING) {
$trace->setStatus(TraceStatus::CANCELLED);
$this->entityManager->persist($trace);
} else {
$failed[] = ['trace' => $trace->getUuid(), 'reason' => 'not cancellable'];
continue;
}
$cancelled[] = $trace->getUuid();
} catch (\Throwable $e) {
$this->logger->error(sprintf('Error cancelling trace %s: %s', $trace->getUuid(), $e->getMessage()));
$failed[] = ['trace' => $trace->getUuid(), 'reason' => $e->getMessage()];
}
}
$this->entityManager->flush();
return new JsonResponse(data: 'Traces cancelled successfully', status: Response::HTTP_OK);
return new JsonResponse(
data: ['cancelled' => $cancelled, 'failed' => $failed],
status: Response::HTTP_OK
);
}
}
}

View File

@ -6,66 +6,27 @@ namespace App\Controller\OgAgent;
use App\Dto\Input\KillJobInput;
use App\Entity\Trace;
use App\Model\ClientStatus;
use App\Model\CommandTypes;
use App\Model\TraceStatus;
use App\Service\Trace\CreateService;
use App\Service\Trace\KillJobService;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Component\HttpKernel\Attribute\AsController;
class KillJobAction extends AbstractOgAgentController
#[AsController]
class KillJobAction extends AbstractController
{
/**
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
* @throws RedirectionExceptionInterface
* @throws ClientExceptionInterface
*/
public function __construct(
private readonly EntityManagerInterface $entityManager,
private readonly KillJobService $killJobService,
) {
}
public function __invoke(Trace $trace, KillJobInput $input): JsonResponse
{
$client = $trace->getClient();
if (!$client->getIp()) {
throw new BadRequestHttpException('IP is required');
}
$data = [
'job_id' => $input->jobId
];
$response = $this->createRequest(
method: 'POST',
url: 'https://'.$client->getIp().':8000/opengnsys/KillJob',
params: [
'json' => $data,
],
token: $client->getToken(),
);
if (isset($response['error']) && $response['code'] === Response::HTTP_INTERNAL_SERVER_ERROR) {
throw new BadRequestHttpException('Error killing job: '.$response['error']);
}
$this->logger->info('Killing job', ['client' => $client->getId(), 'job_id' => $input->jobId]);
$trace->setStatus(TraceStatus::CANCELLED);
$this->entityManager->persist($trace);
$this->killJobService->kill($trace, $input->jobId);
$this->entityManager->flush();
$inputData = [
'job_id' => $input->jobId,
'client' => $client->getId(),
'trace' => $trace->getUuid(),
'command' => $trace->getCommand(),
];
//$this->createService->__invoke($client, CommandTypes::KILL_JOB, TraceStatus::CANCELLED, $input->jobId, $inputData);
return new JsonResponse(data: $trace, status: Response::HTTP_OK);
}
}

View File

@ -0,0 +1,91 @@
<?php
declare(strict_types=1);
namespace App\Service\Trace;
use App\Entity\Trace;
use App\Model\TraceStatus;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
class KillJobService
{
public function __construct(
private readonly EntityManagerInterface $entityManager,
private readonly HttpClientInterface $httpClient,
private readonly LoggerInterface $logger,
#[Autowire(env: 'SSL_ENABLED')]
private readonly string $sslEnabled,
) {
}
/**
* Sends the kill signal to the client's ogAgent and marks the trace as cancelled.
* Persists the trace but does not flush; the caller controls the flush boundary
* so this can be used both for a single kill and inside a batch loop.
*
* @throws BadRequestHttpException on missing client IP, a network/HTTP failure talking to the
* agent, or an agent-reported failure
*/
public function kill(Trace $trace, string $jobId): void
{
$client = $trace->getClient();
if (!$client || !$client->getIp()) {
throw new BadRequestHttpException('IP is required');
}
try {
$response = $this->httpClient->request(
'POST',
'https://'.$client->getIp().':8000/opengnsys/KillJob',
$this->buildRequestOptions(['json' => ['job_id' => $jobId]], $client->getToken())
);
$result = json_decode($response->getContent(), true);
} catch (ClientExceptionInterface|ServerExceptionInterface|TransportExceptionInterface $e) {
$this->logger->error(sprintf('Error killing job on client %s: %s', $client->getIp(), $e->getMessage()));
throw new BadRequestHttpException('Error killing job: '.$e->getMessage());
}
if (isset($result['error']) && ($result['code'] ?? null) === Response::HTTP_INTERNAL_SERVER_ERROR) {
throw new BadRequestHttpException('Error killing job: '.$result['error']);
}
$this->logger->info('Killing job', ['client' => $client->getId(), 'job_id' => $jobId]);
$trace->setStatus(TraceStatus::CANCELLED);
$this->entityManager->persist($trace);
}
private function buildRequestOptions(array $params, ?string $token): array
{
$options = array_merge($params, [
'headers' => [
'accept' => 'application/json',
'Content-Type' => 'application/json',
'Authorization' => $token,
],
'verify_peer' => false,
'verify_host' => false,
'timeout' => 10,
]);
if ($this->sslEnabled === 'true') {
$options['verify_peer'] = true;
$options['verify_host'] = false;
$options['cafile'] = '/opt/opengnsys/ogcore/etc/certificates/ca.crt';
$options['local_cert'] = '/opt/opengnsys/ogcore/etc/certificates/ogcore.crt';
$options['local_pk'] = '/opt/opengnsys/ogcore/etc/certificates/ogcore.key';
}
return $options;
}
}