entityManager->getRepository(Trace::class) ->createQueryBuilder('t') ->select('t') ->where('t.status = :status') ->setParameter('status', TraceStatus::PENDING) ->andWhere('t.id IN ( SELECT MIN(t2.id) FROM App\Entity\Trace t2 WHERE t2.status = :status GROUP BY t2.client )') ->orderBy('t.id', 'ASC') ->getQuery() ->getResult(); $count = count($traces); $io->info("Found $count pending trace(s) - processing the latest one"); $processedCount = 0; $errorCount = 0; foreach ($traces as $trace) { try { $io->info("Processing trace {$trace->getUuid()} with command: {$trace->getCommand()}"); $result = $this->executeTrace($trace); if ($result) { $processedCount++; $io->success("Successfully processed trace {$trace->getUuid()}"); } else { $errorCount++; $io->error("Failed to process trace {$trace->getUuid()}"); } } catch (\Exception $e) { $errorCount++; $io->error("Error processing trace {$trace->getUuid()}: " . $e->getMessage()); $trace->setStatus(TraceStatus::FAILED); $trace->setOutput($e->getMessage()); $trace->setFinishedAt(new \DateTime()); $this->entityManager->persist($trace); } } $this->entityManager->flush(); $executionTime = microtime(true) - $startTime; $io->success("Processed $processedCount traces successfully, $errorCount failed"); $io->note("Execution time: " . round($executionTime, 3) . "s"); return Command::SUCCESS; } private function executeTrace(Trace $trace): bool { $command = $trace->getCommand(); $input = $trace->getInput() ?? []; $client = $trace->getClient(); if (!$client) { throw new \Exception("No client associated with trace"); } $trace->setExecutedAt(new \DateTime()); $this->entityManager->persist($trace); $this->entityManager->flush(); try { switch ($command) { case CommandTypes::CREATE_IMAGE: return $this->executeCreateImage($trace, $input); case CommandTypes::DEPLOY_IMAGE: return $this->executeDeployImage($trace, $input); case CommandTypes::PARTITION_AND_FORMAT: return $this->executePartitionAssistant($trace, $input); case CommandTypes::RUN_SCRIPT: return $this->executeRunScript($trace, $input); case CommandTypes::POWER_ON: return $this->executePowerOn($trace, $input); case CommandTypes::REBOOT: return $this->executeReboot($trace, $input); case CommandTypes::SHUTDOWN: return $this->executeShutdown($trace, $input); case CommandTypes::LOGIN: return $this->executeLogin($trace, $input); default: throw new \Exception("Unsupported command type: $command"); } } catch (\Exception $e) { $trace->setStatus(TraceStatus::FAILED); $trace->setOutput($e->getMessage()); $trace->setFinishedAt(new \DateTime()); $this->entityManager->persist($trace); $this->entityManager->flush(); throw $e; } } private function executeCreateImage(Trace $trace, array $input): bool { $client = $trace->getClient(); $image = null; if (isset($input['action']) && $input['action'] === 'create') { $image = new Image(); $image->setName($input['imageName'] ?? 'Image_' . uniqid()); $image->setType($input['type'] ?? 'monolithic'); $image->setRemotePc($input['remotePc'] ?? false); $image->setIsGlobal($input['isGlobal'] ?? false); $image->setClient($client); $this->entityManager->persist($image); } elseif (isset($input['action']) && $input['action'] === 'update') { if (!isset($input['imageUuid'])) { throw new \Exception("Image UUID not found in trace input for update operation"); } $image = $this->entityManager->getRepository(Image::class) ->findOneBy(['uuid' => $input['imageUuid']]); if (!$image) { throw new \Exception("Image not found with UUID: {$input['imageUuid']} for update operation"); } } else { throw new \Exception("Either 'create' or 'update' field must be set to true in trace input"); } if (isset($input['diskNumber']) && isset($input['partitionNumber'])) { $partitionInfo = [ 'numDisk' => $input['diskNumber'], 'numPartition' => $input['partitionNumber'], 'partitionCode' => $input['partitionCode'] ?? 'LINUX', 'filesystem' => $input['filesystem'] ?? 'EXT4' ]; $image->setPartitionInfo(json_encode($partitionInfo)); } $partition = null; if (isset($input['diskNumber']) && isset($input['partitionNumber'])) { $partition = $this->entityManager->getRepository(Partition::class) ->findOneBy([ 'client' => $client, 'diskNumber' => $input['diskNumber'], 'partitionNumber' => $input['partitionNumber'] ]); if (!$partition) { throw new \Exception("Partition not found for client {$client->getUuid()} with disk {$input['diskNumber']} and partition {$input['partitionNumber']}"); } } try { $response = $this->createImageAction->__invoke( queue: false, image: $image, partition: $partition, client: $client, gitRepositoryName: $input['gitRepositoryName'] ?? null, existingTrace: $trace ); if ($response->getStatusCode() === 200) { $trace->setStatus(TraceStatus::IN_PROGRESS); $trace->setFinishedAt(new \DateTime()); $this->entityManager->persist($trace); $this->entityManager->flush(); return true; } return false; } catch (\Exception $e) { return false; } } private function executeDeployImage(Trace $trace, array $input): bool { $client = $trace->getClient(); if (!isset($input['imageImageRepository'])) { throw new \Exception("ImageImageRepository UUID not found in trace input"); } $imageImageRepository = $this->entityManager->getRepository(ImageImageRepository::class) ->findOneBy(['uuid' => $input['imageImageRepository']]); if (!$imageImageRepository) { throw new \Exception("ImageImageRepository not found with UUID: {$input['imageImageRepository']}"); } $deployInput = new DeployImageInput(); $deployInput->method = $input['method'] ?? 'unicast'; $deployInput->type = $input['type'] ?? 'monolithic'; $deployInput->diskNumber = $input['diskNumber'] ?? 1; $deployInput->partitionNumber = $input['partitionNumber'] ?? 1; $deployInput->mcastMode = $input['mcastMode'] ?? 'duplex'; $deployInput->mcastSpeed = $input['mcastSpeed'] ?? 100; $deployInput->mcastPort = $input['mcastPort'] ?? 8000; $deployInput->mcastIp = $input['mcastIp'] ?? '224.0.0.1'; $deployInput->maxClients = $input['maxClients'] ?? 10; $deployInput->maxTime = $input['maxTime'] ?? 3600; $deployInput->p2pMode = $input['p2pMode'] ?? 'seed'; $deployInput->p2pTime = $input['p2pTime'] ?? 300; $jobId = $this->deployImageAction->__invoke( imageImageRepository: $imageImageRepository, input: $deployInput, client: $client ); $trace->setJobId($jobId); $this->entityManager->persist($trace); $this->entityManager->flush(); return true; } private function executePartitionAssistant(Trace $trace, array $input): bool { $client = $trace->getClient(); $partitionInput = new PartitionPostInput(); $partitionInput->clients = [new ClientOutput($client)]; $partitions = []; foreach ($input as $item) { $partitionInputObj = new PartitionInput(); $partitionInputObj->diskNumber = $item['diskNumber']; $partitionInputObj->partitionNumber = (int)$item['partitionNumber']; $partitionInputObj->partitionCode = $item['partitionCode'] ?? 'LINUX'; $partitionInputObj->size = (float)($item['size'] / 1024); $partitionInputObj->filesystem = $item['filesystem'] ?? 'EXT4'; $partitionInputObj->format = ($item['format'] ?? '0') === '1'; $partitions[] = $partitionInputObj; } $partitionInput->partitions = $partitions; $partitionInput->queue = false; try { $response = $this->partitionAssistantAction->__invoke($partitionInput, $trace); if ($response->getStatusCode() === 200) { $trace->setStatus(TraceStatus::SUCCESS); $trace->setFinishedAt(new \DateTime()); $this->entityManager->persist($trace); $this->entityManager->flush(); return true; } return false; } catch (\Exception $e) { return false; } } private function executeRunScript(Trace $trace, array $input): bool { $client = $trace->getClient(); if (!isset($input['script'])) { throw new \Exception("Script not found in trace input"); } $commandExecuteInput = new CommandExecuteInput(); $commandExecuteInput->clients = [new ClientOutput($client)]; $commandExecuteInput->script = $input['script']; $commandExecuteInput->queue = false; try { $response = $this->runScriptAction->__invoke($commandExecuteInput, $trace); if ($response->getStatusCode() === 200) { $trace->setStatus(TraceStatus::SUCCESS); $trace->setFinishedAt(new \DateTime()); $this->entityManager->persist($trace); $this->entityManager->flush(); return true; } return false; } catch (\Exception $e) { return false; } } private function executePowerOn(Trace $trace, array $input): bool { $trace->setStatus(TraceStatus::SUCCESS); $trace->setFinishedAt(new \DateTime()); $this->entityManager->persist($trace); $this->entityManager->flush(); return true; } private function executeReboot(Trace $trace, array $input): bool { $client = $trace->getClient(); $multipleClientsInput = new MultipleClientsInput(); $multipleClientsInput->clients = [new ClientOutput($client)]; $multipleClientsInput->queue = false; try { $response = $this->rebootAction->__invoke($multipleClientsInput); if ($response->getStatusCode() === 200) { $trace->setStatus(TraceStatus::SUCCESS); $trace->setFinishedAt(new \DateTime()); $this->entityManager->persist($trace); $this->entityManager->flush(); return true; } return false; } catch (\Exception $e) { return false; } } private function executeShutdown(Trace $trace, array $input): bool { $client = $trace->getClient(); $multipleClientsInput = new MultipleClientsInput(); $multipleClientsInput->clients = [new ClientOutput($client)]; $multipleClientsInput->queue = false; try { $response = $this->powerOffAction->__invoke($multipleClientsInput); if ($response->getStatusCode() === 200) { $trace->setStatus(TraceStatus::SUCCESS); $trace->setFinishedAt(new \DateTime()); $this->entityManager->persist($trace); $this->entityManager->flush(); return true; } return false; } catch (\Exception $e) { return false; } } private function executeLogin(Trace $trace, array $input): bool { $client = $trace->getClient(); if (!isset($input['partition'])) { throw new \Exception("Partition not found in trace input"); } $partition = $this->entityManager->getRepository(\App\Entity\Partition::class) ->findOneBy(['uuid' => $input['partition']]); if (!$partition) { throw new \Exception("Partition not found with UUID: {$input['partition']}"); } $bootClientsInput = new BootClientsInput(); $bootClientsInput->clients = [new ClientOutput($client)]; $bootClientsInput->partition = new PartitionOutput($partition); try { $response = $this->loginAction->__invoke($bootClientsInput); if ($response->getStatusCode() === 200) { $trace->setStatus(TraceStatus::SUCCESS); $trace->setFinishedAt(new \DateTime()); $this->entityManager->persist($trace); $this->entityManager->flush(); return true; } return false; } catch (\Exception $e) { return false; } } }