refs #2591. new response. Added job_id

develop
Manuel Aranda Rosales 2025-08-04 10:50:11 +02:00
parent 82d5edb55b
commit bc80b3af7c
7 changed files with 101 additions and 27 deletions

View File

@ -45,20 +45,28 @@ class DeployGitImageAction extends AbstractController
{
$this->validator->validate($input);
$this->handleGitDeployment($input);
$clientJobs = $this->handleGitDeployment($input);
return new JsonResponse(data: [], status: Response::HTTP_OK);
return new JsonResponse(data: $clientJobs, status: Response::HTTP_OK);
}
private function handleGitDeployment(DeployGitImageInput $input): void
private function handleGitDeployment(DeployGitImageInput $input): array
{
$clientJobs = [];
foreach ($input->clients as $client) {
$inputData = $this->createInputData($input, $client->getEntity());
$this->processDeployment($client->getEntity(), $input, $inputData, DeployMethodTypes::GIT);
$jobId = $this->processDeployment($client->getEntity(), $input, $inputData, DeployMethodTypes::GIT);
if ($jobId) {
$clientJobs[(string) '/clients/' . $client->getEntity()->getUuid()] = $jobId;
}
}
return $clientJobs;
}
private function processDeployment($client, DeployGitImageInput $input, array $inputData, string $deployType): void
private function processDeployment($client, DeployGitImageInput $input, array $inputData, string $deployType): ?string
{
$agentJobId = $this->deployGitImageOgAgentAction->__invoke($input, $client);
@ -66,10 +74,12 @@ class DeployGitImageAction extends AbstractController
if ($input->queue) {
$this->createService->__invoke($client, CommandTypes::DEPLOY_IMAGE, TraceStatus::PENDING, null, $inputData);
}
return;
return null;
}
$this->createService->__invoke($client, CommandTypes::DEPLOY_IMAGE, TraceStatus::IN_PROGRESS, $agentJobId, $inputData);
return $agentJobId;
}
private function createInputData(DeployGitImageInput $input, $client): array

View File

@ -47,42 +47,58 @@ class DeployImageAction extends AbstractController
{
$this->validator->validate($input);
$clientJobs = [];
if ($input->type === 'monolithic') {
$this->handleMonolithicDeployment($input, $image);
$clientJobs = $this->handleMonolithicDeployment($input, $image);
}
return new JsonResponse(data: [], status: Response::HTTP_OK);
return new JsonResponse(data: $clientJobs, status: Response::HTTP_OK);
}
private function handleMonolithicDeployment(DeployImageInput $input, ImageImageRepository $image): void
private function handleMonolithicDeployment(DeployImageInput $input, ImageImageRepository $image): array
{
$clientJobs = [];
switch ($input->method) {
case DeployMethodTypes::UNICAST:
case DeployMethodTypes::UNICAST_DIRECT:
$this->handleUnicastDeployment($input, $image);
$clientJobs = $this->handleUnicastDeployment($input, $image);
break;
case DeployMethodTypes::MULTICAST_UFTP:
case DeployMethodTypes::MULTICAST_UFTP_DIRECT:
case DeployMethodTypes::MULTICAST_UDPCAST:
case DeployMethodTypes::MULTICAST_UDPCAST_DIRECT:
$this->handleMulticastDeployment($input, $image);
$clientJobs = $this->handleMulticastDeployment($input, $image);
break;
case DeployMethodTypes::TORRENT:
$this->handleTorrentDeployment($input, $image);
$clientJobs = $this->handleTorrentDeployment($input, $image);
break;
}
return $clientJobs;
}
private function handleUnicastDeployment(DeployImageInput $input, ImageImageRepository $image): void
private function handleUnicastDeployment(DeployImageInput $input, ImageImageRepository $image): array
{
$clientJobs = [];
foreach ($input->clients as $client) {
$inputData = $this->createInputData($input, $image, $client->getEntity());
$this->processDeployment($client->getEntity(), $input, $image, $inputData, DeployMethodTypes::UNICAST);
$jobId = $this->processDeployment($client->getEntity(), $input, $image, $inputData, DeployMethodTypes::UNICAST);
if ($jobId) {
$clientJobs[(string) '/clients/' . $client->getEntity()->getUuid()] = $jobId;
}
}
return $clientJobs;
}
private function handleMulticastDeployment(DeployImageInput $input, ImageImageRepository $image): void
private function handleMulticastDeployment(DeployImageInput $input, ImageImageRepository $image): array
{
$clientJobs = [];
foreach ($input->clients as $client) {
$inputData = $this->createMulticastInputData($input, $image, $client->getEntity());
@ -92,12 +108,20 @@ class DeployImageAction extends AbstractController
continue;
}
$this->processDeployment($client->getEntity(), $input, $image, $inputData, DeployMethodTypes::MULTICAST);
$jobId = $this->processDeployment($client->getEntity(), $input, $image, $inputData, DeployMethodTypes::MULTICAST);
if ($jobId) {
$clientJobs[(string) '/clients/' . $client->getEntity()->getUuid()] = $jobId;
}
}
return $clientJobs;
}
private function handleTorrentDeployment(DeployImageInput $input, ImageImageRepository $image): void
private function handleTorrentDeployment(DeployImageInput $input, ImageImageRepository $image): array
{
$clientJobs = [];
foreach ($input->clients as $client) {
$inputData = $this->createTorrentInputData($input, $image, $client->getEntity());
@ -112,11 +136,17 @@ class DeployImageAction extends AbstractController
throw $e;
}
$this->processDeployment($client->getEntity(), $input, $image, $inputData, DeployMethodTypes::TORRENT);
$jobId = $this->processDeployment($client->getEntity(), $input, $image, $inputData, DeployMethodTypes::TORRENT);
if ($jobId) {
$clientJobs[(string) '/clients/' . $client->getEntity()->getUuid()] = $jobId;
}
}
return $clientJobs;
}
private function processDeployment($client, DeployImageInput $input, ImageImageRepository $image, array $inputData, string $deployType): void
private function processDeployment($client, DeployImageInput $input, ImageImageRepository $image, array $inputData, string $deployType): ?string
{
$agentJobId = $this->deployImageOgAgentAction->__invoke($image, $input, $client, $deployType);
@ -124,10 +154,12 @@ class DeployImageAction extends AbstractController
if ($input->queue) {
$this->createService->__invoke($client, CommandTypes::DEPLOY_IMAGE, TraceStatus::PENDING, null, $inputData);
}
return;
return null;
}
$this->createService->__invoke($client, CommandTypes::DEPLOY_IMAGE, TraceStatus::IN_PROGRESS, $agentJobId, $inputData);
return $agentJobId;
}
private function createInputData(DeployImageInput $input, ImageImageRepository $image, $client): array

View File

@ -202,7 +202,7 @@ class CreateImageAction extends AbstractOgAgentController
$inputData
);
return new JsonResponse(data: $image, status: Response::HTTP_OK);
return new JsonResponse(data: ['/clients/' . $client->getUuid() => $jobId], status: Response::HTTP_OK);
} catch (Exception $e) {
$client->setStatus(ClientStatus::OG_LIVE);
$this->entityManager->persist($client);
@ -327,7 +327,7 @@ class CreateImageAction extends AbstractOgAgentController
$inputData
);
return new JsonResponse(data: $image, status: Response::HTTP_OK);
return new JsonResponse(data: ['/clients/' . $client->getUuid() => $jobId], status: Response::HTTP_OK);
} catch (Exception $e) {
$client->setStatus(ClientStatus::OG_LIVE);
$this->entityManager->persist($client);

View File

@ -45,6 +45,8 @@ class PartitionAssistantAction extends AbstractOgAgentController
throw new BadRequestHttpException('Partitions is required');
}
$clientJobs = [];
foreach ($input->clients as $clientInput) {
/** @var Client $client */
$client = $clientInput->getEntity();
@ -125,10 +127,24 @@ class PartitionAssistantAction extends AbstractOgAgentController
$this->entityManager->persist($client);
$this->entityManager->flush();
<<<<<<< Updated upstream
$this->createService->__invoke($client, CommandTypes::PARTITION_AND_FORMAT, TraceStatus::IN_PROGRESS, $jobId, $data);
=======
if ($existingTrace) {
$existingTrace->setStatus(TraceStatus::IN_PROGRESS);
$existingTrace->setJobId($jobId);
$existingTrace->setInput($data);
$this->entityManager->persist($existingTrace);
$this->entityManager->flush();
} else {
$this->createService->__invoke($client, CommandTypes::PARTITION_AND_FORMAT, TraceStatus::IN_PROGRESS, $jobId, $data);
}
$clientJobs['/clients/' . $client->getUuid()] = $jobId;
>>>>>>> Stashed changes
}
}
return new JsonResponse(data: [], status: Response::HTTP_OK);
return new JsonResponse(data: $clientJobs, status: Response::HTTP_OK);
}
}

View File

@ -73,6 +73,10 @@ class RunScriptAction extends AbstractOgAgentController
$this->logger->info('Powering off client', ['client' => $client->getId()]);
$jobId = $response['job_id'];
<<<<<<< Updated upstream
=======
$clientJobs[(string) '/clients/' . $client->getUuid()] = $jobId;
>>>>>>> Stashed changes
$this->entityManager->persist($client);
$this->entityManager->flush();

View File

@ -22,6 +22,7 @@ use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\Response;
readonly class ImageProcessor implements ProcessorInterface
{
@ -39,7 +40,7 @@ readonly class ImageProcessor implements ProcessorInterface
* @throws \Exception
* @throws TransportExceptionInterface
*/
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): ImageOutput|null
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): ImageOutput|null|JsonResponse
{
switch ($operation){
case $operation instanceof Post:
@ -55,7 +56,7 @@ readonly class ImageProcessor implements ProcessorInterface
* @throws \Exception
* @throws TransportExceptionInterface
*/
private function processCreateOrUpdate($data, Operation $operation, array $uriVariables = [], array $context = []): ImageOutput
private function processCreateOrUpdate($data, Operation $operation, array $uriVariables = [], array $context = []): JsonResponse
{
if (!($data instanceof ImageInput)) {
throw new \Exception(sprintf('data is not instance of %s', ImageInput::class));
@ -87,7 +88,12 @@ readonly class ImageProcessor implements ProcessorInterface
throw new \Exception($content['error'] ?? 'Error creating image');
}
return new ImageOutput($data->selectedImage?->getEntity() ?? $image);
if ($response instanceof JsonResponse && $response->getStatusCode() === 200) {
$jobContent = json_decode($response->getContent(), true);
return new JsonResponse(data: $jobContent, status: Response::HTTP_OK);
}
return new JsonResponse(data: ['/clients/' . $image->getClient()->getUuid() => ['headers' => []]], status: Response::HTTP_OK);
} catch (\Exception $e) {
$this->logger->error('Error processing image creation/update', [
'error' => $e->getMessage(),

View File

@ -74,7 +74,13 @@ readonly class PartitionProcessor implements ProcessorInterface
//$this->partitionRepository->save($entity);
}
$this->partitionAssistantAction->__invoke($data);
$response = $this->partitionAssistantAction->__invoke($data);
// Si hay una respuesta exitosa, devolvemos el contenido del job ID
if ($response instanceof JsonResponse && $response->getStatusCode() === 200) {
$jobContent = json_decode($response->getContent(), true);
return new JsonResponse(data: $jobContent, status: Response::HTTP_OK);
}
return new JsonResponse('OK', Response::HTTP_NO_CONTENT);
}