From 82b2a4e460c24b3af536fb5b05fe248a198ab040 Mon Sep 17 00:00:00 2001 From: jmbardallo Date: Mon, 26 Oct 2026 15:21:50 +0100 Subject: [PATCH] fix(trace): fallback to terminatescript and ensure cancellation persists even if agent is unreachable --- src/Service/Trace/KillJobService.php | 61 ++++++++++++++++++++-------- 1 file changed, 43 insertions(+), 18 deletions(-) diff --git a/src/Service/Trace/KillJobService.php b/src/Service/Trace/KillJobService.php index 54328d1..1f017b2 100644 --- a/src/Service/Trace/KillJobService.php +++ b/src/Service/Trace/KillJobService.php @@ -39,29 +39,54 @@ class KillJobService { $client = $trace->getClient(); - if (!$client || !$client->getIp()) { - throw new BadRequestHttpException('IP is required'); - } + if ($client && $client->getIp()) { + $clientIp = $client->getIp(); + $killed = false; - 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()); - } + // 1. Intentar /opengnsys/KillJob (ogLive / nuevo ogagent) + try { + $response = $this->httpClient->request( + 'POST', + 'https://'.$clientIp.':8000/opengnsys/KillJob', + $this->buildRequestOptions(['json' => ['job_id' => $jobId]], $client->getToken()) + ); + $statusCode = $response->getStatusCode(); + if ($statusCode >= 200 && $statusCode < 300) { + $killed = true; + } + } catch (\Throwable $e) { + $this->logger->warning(sprintf('KillJob endpoint failed on client %s: %s', $clientIp, $e->getMessage())); + } - if (isset($result['error']) && ($result['code'] ?? null) === Response::HTTP_INTERNAL_SERVER_ERROR) { - throw new BadRequestHttpException('Error killing job: '.$result['error']); - } + // 2. Si KillJob devolvió 404 o falló, intentar fallback a /opengnsys/terminatescript (ogagent en Windows / Linux desktop) + if (!$killed) { + try { + $response = $this->httpClient->request( + 'POST', + 'https://'.$clientIp.':8000/opengnsys/terminatescript', + $this->buildRequestOptions(['json' => ['job_id' => $jobId]], $client->getToken()) + ); + $statusCode = $response->getStatusCode(); + if ($statusCode >= 200 && $statusCode < 300) { + $killed = true; + } + } catch (\Throwable $e) { + $this->logger->warning(sprintf('terminatescript endpoint also failed on client %s: %s', $clientIp, $e->getMessage())); + } + } - $this->logger->info('Killing job', ['client' => $client->getId(), 'job_id' => $jobId]); + if ($killed) { + $this->logger->info('Job successfully killed on client', ['client' => $client->getId(), 'job_id' => $jobId]); + } else { + $this->logger->warning('Could not signal agent to kill job (client may be offline or rebooting), cancelling trace locally', [ + 'client' => $client->getId(), + 'job_id' => $jobId, + ]); + } + } $trace->setStatus(TraceStatus::CANCELLED); + $trace->setFinishedAt(new \DateTime()); $this->entityManager->persist($trace); }