From 7b74c9ab70510711fbbd8f02fe77b3c1e9e4ffae Mon Sep 17 00:00:00 2001 From: Manuel Aranda Date: Thu, 26 Jun 2025 17:08:40 +0200 Subject: [PATCH] refs #2327. KillJob integration --- config/api_platform/Trace.yaml | 18 ++++++ src/Controller/OgAgent/KillJobAction.php | 71 ++++++++++++++++++++++++ src/Dto/Input/KillJobInput.php | 15 +++++ src/EventListener/ClientMacListener.php | 6 +- src/Model/CommandTypes.php | 4 ++ 5 files changed, 112 insertions(+), 2 deletions(-) create mode 100644 src/Controller/OgAgent/KillJobAction.php create mode 100644 src/Dto/Input/KillJobInput.php diff --git a/config/api_platform/Trace.yaml b/config/api_platform/Trace.yaml index 3d64fa4..86ce3a6 100644 --- a/config/api_platform/Trace.yaml +++ b/config/api_platform/Trace.yaml @@ -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 diff --git a/src/Controller/OgAgent/KillJobAction.php b/src/Controller/OgAgent/KillJobAction.php new file mode 100644 index 0000000..2877cce --- /dev/null +++ b/src/Controller/OgAgent/KillJobAction.php @@ -0,0 +1,71 @@ +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); + } +} diff --git a/src/Dto/Input/KillJobInput.php b/src/Dto/Input/KillJobInput.php new file mode 100644 index 0000000..4ac88f3 --- /dev/null +++ b/src/Dto/Input/KillJobInput.php @@ -0,0 +1,15 @@ +putHostAction->__invoke($oldMac, $client); - $this->deleteAction->__invoke($oldMac); + if ($client->getSubnet()) { + $this->putHostAction->__invoke($oldMac, $client); + $this->deleteAction->__invoke($oldMac); + } } } \ No newline at end of file diff --git a/src/Model/CommandTypes.php b/src/Model/CommandTypes.php index 63c48eb..f309dcc 100644 --- a/src/Model/CommandTypes.php +++ b/src/Model/CommandTypes.php @@ -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