refs #2327. KillJob integration
testing/ogcore-api/pipeline/head There was a failure building this commit Details

pull/37/head
Manuel Aranda Rosales 2025-06-26 17:08:40 +02:00
parent e325578e2b
commit 7b74c9ab70
5 changed files with 112 additions and 2 deletions

View File

@ -23,6 +23,24 @@ resources:
uriTemplate: /traces/server/{uuid}/cancel
controller: App\Controller\OgRepository\Image\CancelTransmissionAction
cancel_multiple_traces:
shortName: Trace Server
description: Cancel Multiple Traces in OgRepository
controller: App\Controller\CancelMultipleTracesAction
class: ApiPlatform\Metadata\Post
method: POST
input: App\Dto\Input\CancelMultipleTracesInput
uriTemplate: /traces/cancel-multiple
kill_job:
shortName: Kill Job
description: Kill Job in OgAgent
controller: App\Controller\OgAgent\KillJobAction
class: ApiPlatform\Metadata\Post
method: POST
input: App\Dto\Input\KillJobInput
uriTemplate: /traces/{uuid}/kill-job
order:
createdAt: DESC

View File

@ -0,0 +1,71 @@
<?php
declare(strict_types=1);
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 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;
class KillJobAction extends AbstractOgAgentController
{
/**
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
* @throws RedirectionExceptionInterface
* @throws ClientExceptionInterface
*/
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->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,15 @@
<?php
namespace App\Dto\Input;
use ApiPlatform\Metadata\ApiProperty;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
final class KillJobInput
{
#[Assert\NotNull()]
#[Groups(['trace:write'])]
#[ApiProperty(description: 'The job id to kill', example: "1234567890")]
public string $jobId;
}

View File

@ -47,7 +47,9 @@ readonly class ClientMacListener
return;
}
$this->putHostAction->__invoke($oldMac, $client);
$this->deleteAction->__invoke($oldMac);
if ($client->getSubnet()) {
$this->putHostAction->__invoke($oldMac, $client);
$this->deleteAction->__invoke($oldMac);
}
}
}

View File

@ -7,6 +7,7 @@ final class CommandTypes
public const string DEPLOY_IMAGE = 'deploy-image';
public const string RESTORE_IMAGE = 'restore-image';
public const string CREATE_IMAGE = 'create-image';
public const string CREATE_IMAGE_GIT = 'create-image-git';
public const string CONVERT_IMAGE = 'convert-image';
public const string CREATE_IMAGE_AUX_FILE = 'create-image-aux-file';
public const string BACKUP_IMAGE = 'backup-image';
@ -26,11 +27,13 @@ final class CommandTypes
public const string REMOVE_CACHE_IMAGE = 'remove-cache-image';
public const string HARDWARE_INVENTORY = 'hardware-inventory';
public const string SOFTWARE_INVENTORY = 'software-inventory';
public const string KILL_JOB = 'kill-job';
private const array COMMAND_TYPES = [
self::DEPLOY_IMAGE => 'Deploy Image',
self::RESTORE_IMAGE => 'Update Cache',
self::CREATE_IMAGE => 'Create Image',
self::CREATE_IMAGE_GIT => 'Create Image Git',
self::CONVERT_IMAGE => 'Convert Image',
self::CONVERT_IMAGE_TO_VIRTUAL => 'Convert Image to Virtual',
self::CREATE_IMAGE_AUX_FILE => 'Create Image Aux File',
@ -50,6 +53,7 @@ final class CommandTypes
self::REMOVE_CACHE_IMAGE => 'Remove Cache Image',
self::HARDWARE_INVENTORY => 'Hardware Inventory',
self::SOFTWARE_INVENTORY => 'Software Inventory',
self::KILL_JOB => 'Kill Job',
];
public static function getCommandTypes(): array