fix(trace): fallback to terminatescript and ensure cancellation persists even if agent is unreachable

devel
Juan Manuel Bardallo 2026-10-26 15:21:50 +01:00
parent d171ddb22b
commit 82b2a4e460
1 changed files with 43 additions and 18 deletions

View File

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