ref 3009. Execute task right now
parent
b4907bc5e6
commit
22947fd44d
|
|
@ -24,6 +24,13 @@ resources:
|
|||
ApiPlatform\Metadata\Post: ~
|
||||
ApiPlatform\Metadata\Delete: ~
|
||||
|
||||
command_task_execute:
|
||||
class: ApiPlatform\Metadata\Post
|
||||
method: POST
|
||||
input: App\Dto\Input\CommandTaskExecuteInput
|
||||
uriTemplate: /command-tasks/{uuid}/execute
|
||||
controller: App\Controller\CommandTaskExecuteAction
|
||||
|
||||
properties:
|
||||
App\Entity\CommandTask:
|
||||
id:
|
||||
|
|
|
|||
|
|
@ -17,12 +17,10 @@ use App\Entity\CommandTask;
|
|||
use App\Entity\Image;
|
||||
use App\Entity\ImageImageRepository;
|
||||
use App\Entity\Partition;
|
||||
use App\Entity\Trace;
|
||||
use App\Model\ClientStatus;
|
||||
use App\Model\CommandTypes;
|
||||
use App\Model\TraceStatus;
|
||||
use App\Repository\ClientRepository;
|
||||
use App\Repository\CommandTaskRepository;
|
||||
use App\Service\CommandTaskTraceCreator;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
|
|
@ -41,6 +39,7 @@ class RunScheduledCommandTasksCommand extends Command
|
|||
private readonly DeployImageAction $deployImageAction,
|
||||
private readonly CreateImageAction $createImageAction,
|
||||
private readonly PartitionAssistantAction $partitionAssistantAction,
|
||||
private readonly CommandTaskTraceCreator $commandTaskTraceCreator,
|
||||
)
|
||||
{
|
||||
parent::__construct();
|
||||
|
|
@ -78,15 +77,15 @@ class RunScheduledCommandTasksCommand extends Command
|
|||
if ($isDue && $notYetExecuted) {
|
||||
$output->writeln("Ejecutando tarea: " . $task->getName());
|
||||
|
||||
$scripts = $task->getCommandTaskScripts()->toArray();
|
||||
usort($scripts, fn($a, $b) => $a->getExecutionOrder() <=> $b->getExecutionOrder());
|
||||
|
||||
foreach ($scripts as $script) {
|
||||
$output->writeln(" - Creando traza para script de tipo {$script->getType()} con orden {$script->getExecutionOrder()}");
|
||||
|
||||
$this->createTraceForScript($script, $task, $output);
|
||||
if ($task->getScope() !== 'clients') {
|
||||
$ou = $task->getOrganizationalUnit();
|
||||
$clients = $ou ? $this->clientRepository->findClientsByOrganizationalUnitAndDescendants($ou->getId()) : [];
|
||||
} else {
|
||||
$clients = $task->getClients();
|
||||
}
|
||||
|
||||
$this->commandTaskTraceCreator->createPendingTraces($task, $clients);
|
||||
|
||||
$task->setLastExecution(new \DateTimeImmutable('now', new \DateTimeZone('UTC')));
|
||||
$task->setNextExecution($task->calculateNextExecutionDate());
|
||||
|
||||
|
|
@ -98,127 +97,6 @@ class RunScheduledCommandTasksCommand extends Command
|
|||
return Command::SUCCESS;
|
||||
}
|
||||
|
||||
private function createTraceForScript($script, CommandTask $task, OutputInterface $output): void
|
||||
{
|
||||
$scriptParameters = $script->getParameters();
|
||||
|
||||
if ($task->getScope() !== 'clients') {
|
||||
$ou = $task->getOrganizationalUnit();
|
||||
$clients = $ou ? $this->clientRepository->findClientsByOrganizationalUnitAndDescendants($ou->getId()) : [];
|
||||
} else {
|
||||
$clients = $task->getClients();
|
||||
}
|
||||
|
||||
foreach ($clients as $client) {
|
||||
$trace = new Trace();
|
||||
$trace->setClient($client);
|
||||
$trace->setStatus(TraceStatus::PENDING);
|
||||
$trace->setExecutedAt(new \DateTime());
|
||||
|
||||
switch ($script->getType()) {
|
||||
case 'run-script':
|
||||
$trace->setCommand(CommandTypes::RUN_SCRIPT);
|
||||
$trace->setInput([
|
||||
'script' => $script->getContent()
|
||||
]);
|
||||
break;
|
||||
|
||||
case 'deploy-image':
|
||||
$trace->setCommand(CommandTypes::DEPLOY_IMAGE);
|
||||
|
||||
$input = [
|
||||
'imageImageRepository' => $scriptParameters['imageUuid']
|
||||
];
|
||||
|
||||
if (isset($scriptParameters['method'])) {
|
||||
$input['method'] = $scriptParameters['method'];
|
||||
}
|
||||
if (isset($scriptParameters['type'])) {
|
||||
$input['type'] = $scriptParameters['type'];
|
||||
}
|
||||
if (isset($scriptParameters['diskNumber'])) {
|
||||
$input['diskNumber'] = $scriptParameters['diskNumber'];
|
||||
}
|
||||
if (isset($scriptParameters['partitionNumber'])) {
|
||||
$input['partitionNumber'] = $scriptParameters['partitionNumber'];
|
||||
}
|
||||
if (isset($scriptParameters['mcastMode'])) {
|
||||
$input['mcastMode'] = $scriptParameters['mcastMode'];
|
||||
}
|
||||
if (isset($scriptParameters['mcastSpeed'])) {
|
||||
$input['mcastSpeed'] = $scriptParameters['mcastSpeed'];
|
||||
}
|
||||
if (isset($scriptParameters['mcastPort'])) {
|
||||
$input['mcastPort'] = $scriptParameters['mcastPort'];
|
||||
}
|
||||
if (isset($scriptParameters['mcastIp'])) {
|
||||
$input['mcastIp'] = $scriptParameters['mcastIp'];
|
||||
}
|
||||
if (isset($scriptParameters['maxClients'])) {
|
||||
$input['maxClients'] = $scriptParameters['maxClients'];
|
||||
}
|
||||
if (isset($scriptParameters['maxTime'])) {
|
||||
$input['maxTime'] = $scriptParameters['maxTime'];
|
||||
}
|
||||
if (isset($scriptParameters['p2pMode'])) {
|
||||
$input['p2pMode'] = $scriptParameters['p2pMode'];
|
||||
}
|
||||
if (isset($scriptParameters['p2pTime'])) {
|
||||
$input['p2pTime'] = $scriptParameters['p2pTime'];
|
||||
}
|
||||
|
||||
$trace->setInput($input);
|
||||
break;
|
||||
|
||||
case 'create-image':
|
||||
$trace->setCommand(CommandTypes::CREATE_IMAGE);
|
||||
$trace->setInput([
|
||||
'type' => $scriptParameters['type'],
|
||||
'imageName' => $scriptParameters['imageName'],
|
||||
'imageUuid' => $scriptParameters['imageUuid'] ?? null,
|
||||
'diskNumber' => $scriptParameters['diskNumber'] ?? null,
|
||||
'partitionNumber' => $scriptParameters['partitionNumber'] ?? null,
|
||||
'gitRepositoryName' => $scriptParameters['gitRepositoryName'] ?? null,
|
||||
'action' => $scriptParameters['action'] ?? 'create'
|
||||
]);
|
||||
break;
|
||||
|
||||
case 'partition-assistant':
|
||||
$trace->setCommand(CommandTypes::PARTITION_AND_FORMAT);
|
||||
$trace->setInput($scriptParameters);
|
||||
break;
|
||||
|
||||
case 'power-on':
|
||||
$trace->setCommand(CommandTypes::POWER_ON);
|
||||
$trace->setInput($scriptParameters ?? []);
|
||||
break;
|
||||
|
||||
case 'reboot':
|
||||
$trace->setCommand(CommandTypes::REBOOT);
|
||||
$trace->setInput($scriptParameters ?? []);
|
||||
break;
|
||||
|
||||
case 'shutdown':
|
||||
$trace->setCommand(CommandTypes::SHUTDOWN);
|
||||
$trace->setInput($scriptParameters ?? []);
|
||||
break;
|
||||
|
||||
case 'login':
|
||||
$trace->setCommand(CommandTypes::LOGIN);
|
||||
$trace->setInput($scriptParameters ?? []);
|
||||
break;
|
||||
|
||||
default:
|
||||
$output->writeln(" - Tipo de script no soportado: {$script->getType()}");
|
||||
continue 2;
|
||||
}
|
||||
|
||||
$this->entityManager->persist($trace);
|
||||
$output->writeln(" - Traza creada para cliente {$client->getUuid()} con comando {$trace->getCommand()}");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private function executePartitionAssistant($script, CommandTask $task, OutputInterface $output): void
|
||||
{
|
||||
$scriptParameters = $script->getParameters();
|
||||
|
|
|
|||
|
|
@ -0,0 +1,32 @@
|
|||
<?php
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Dto\Input\CommandTaskExecuteInput;
|
||||
use App\Entity\CommandTask;
|
||||
use App\Service\CommandTaskTraceCreator;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class CommandTaskExecuteAction extends AbstractController
|
||||
{
|
||||
public function __construct(
|
||||
private readonly EntityManagerInterface $entityManager,
|
||||
private readonly CommandTaskTraceCreator $commandTaskTraceCreator,
|
||||
)
|
||||
{
|
||||
}
|
||||
|
||||
public function __invoke(CommandTaskExecuteInput $input, CommandTask $commandTask): JsonResponse
|
||||
{
|
||||
$clients = array_map(fn($client) => $client->getEntity(), $input->clients);
|
||||
|
||||
$this->commandTaskTraceCreator->createPendingTraces($commandTask, $clients);
|
||||
|
||||
$this->entityManager->flush();
|
||||
|
||||
return new JsonResponse(data: 'Command task executed successfully', status: Response::HTTP_OK);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
<?php
|
||||
|
||||
namespace App\Dto\Input;
|
||||
|
||||
use ApiPlatform\Metadata\ApiProperty;
|
||||
use App\Dto\Output\ClientOutput;
|
||||
use Symfony\Component\Serializer\Annotation\Groups;
|
||||
use Symfony\Component\Validator\Constraints as Assert;
|
||||
|
||||
final class CommandTaskExecuteInput
|
||||
{
|
||||
/**
|
||||
* @var ClientOutput[]
|
||||
*/
|
||||
#[Assert\NotNull]
|
||||
#[Groups(['command-task:write'])]
|
||||
#[ApiProperty(
|
||||
description: 'Los clientes donde ejecutar la tarea',
|
||||
example: []
|
||||
)]
|
||||
public array $clients = [];
|
||||
}
|
||||
|
|
@ -0,0 +1,148 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use App\Entity\CommandTask;
|
||||
use App\Entity\Trace;
|
||||
use App\Model\CommandTypes;
|
||||
use App\Model\TraceStatus;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
|
||||
final readonly class CommandTaskTraceCreator
|
||||
{
|
||||
public function __construct(
|
||||
private EntityManagerInterface $entityManager,
|
||||
)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Crea una Trace en estado PENDING por cada script de la tarea y cliente indicado,
|
||||
* sin tener en cuenta el scope/organizationalUnit/clients propios de la CommandTask.
|
||||
* No hace flush(): lo decide quien la invoque.
|
||||
*
|
||||
* @param array $clients
|
||||
*/
|
||||
public function createPendingTraces(CommandTask $task, array $clients): void
|
||||
{
|
||||
$scripts = $task->getCommandTaskScripts()->toArray();
|
||||
usort($scripts, fn($a, $b) => $a->getExecutionOrder() <=> $b->getExecutionOrder());
|
||||
|
||||
foreach ($scripts as $script) {
|
||||
foreach ($clients as $client) {
|
||||
$this->createTraceForScript($script, $client);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function createTraceForScript($script, $client): void
|
||||
{
|
||||
$scriptParameters = $script->getParameters();
|
||||
|
||||
$trace = new Trace();
|
||||
$trace->setClient($client);
|
||||
$trace->setStatus(TraceStatus::PENDING);
|
||||
$trace->setExecutedAt(new \DateTime());
|
||||
|
||||
switch ($script->getType()) {
|
||||
case 'run-script':
|
||||
$trace->setCommand(CommandTypes::RUN_SCRIPT);
|
||||
$trace->setInput([
|
||||
'script' => $script->getContent()
|
||||
]);
|
||||
break;
|
||||
|
||||
case 'deploy-image':
|
||||
$trace->setCommand(CommandTypes::DEPLOY_IMAGE);
|
||||
|
||||
$input = [
|
||||
'imageImageRepository' => $scriptParameters['imageUuid']
|
||||
];
|
||||
|
||||
if (isset($scriptParameters['method'])) {
|
||||
$input['method'] = $scriptParameters['method'];
|
||||
}
|
||||
if (isset($scriptParameters['type'])) {
|
||||
$input['type'] = $scriptParameters['type'];
|
||||
}
|
||||
if (isset($scriptParameters['diskNumber'])) {
|
||||
$input['diskNumber'] = $scriptParameters['diskNumber'];
|
||||
}
|
||||
if (isset($scriptParameters['partitionNumber'])) {
|
||||
$input['partitionNumber'] = $scriptParameters['partitionNumber'];
|
||||
}
|
||||
if (isset($scriptParameters['mcastMode'])) {
|
||||
$input['mcastMode'] = $scriptParameters['mcastMode'];
|
||||
}
|
||||
if (isset($scriptParameters['mcastSpeed'])) {
|
||||
$input['mcastSpeed'] = $scriptParameters['mcastSpeed'];
|
||||
}
|
||||
if (isset($scriptParameters['mcastPort'])) {
|
||||
$input['mcastPort'] = $scriptParameters['mcastPort'];
|
||||
}
|
||||
if (isset($scriptParameters['mcastIp'])) {
|
||||
$input['mcastIp'] = $scriptParameters['mcastIp'];
|
||||
}
|
||||
if (isset($scriptParameters['maxClients'])) {
|
||||
$input['maxClients'] = $scriptParameters['maxClients'];
|
||||
}
|
||||
if (isset($scriptParameters['maxTime'])) {
|
||||
$input['maxTime'] = $scriptParameters['maxTime'];
|
||||
}
|
||||
if (isset($scriptParameters['p2pMode'])) {
|
||||
$input['p2pMode'] = $scriptParameters['p2pMode'];
|
||||
}
|
||||
if (isset($scriptParameters['p2pTime'])) {
|
||||
$input['p2pTime'] = $scriptParameters['p2pTime'];
|
||||
}
|
||||
|
||||
$trace->setInput($input);
|
||||
break;
|
||||
|
||||
case 'create-image':
|
||||
$trace->setCommand(CommandTypes::CREATE_IMAGE);
|
||||
$trace->setInput([
|
||||
'type' => $scriptParameters['type'],
|
||||
'imageName' => $scriptParameters['imageName'],
|
||||
'imageUuid' => $scriptParameters['imageUuid'] ?? null,
|
||||
'diskNumber' => $scriptParameters['diskNumber'] ?? null,
|
||||
'partitionNumber' => $scriptParameters['partitionNumber'] ?? null,
|
||||
'gitRepositoryName' => $scriptParameters['gitRepositoryName'] ?? null,
|
||||
'action' => $scriptParameters['action'] ?? 'create'
|
||||
]);
|
||||
break;
|
||||
|
||||
case 'partition-assistant':
|
||||
$trace->setCommand(CommandTypes::PARTITION_AND_FORMAT);
|
||||
$trace->setInput($scriptParameters);
|
||||
break;
|
||||
|
||||
case 'power-on':
|
||||
$trace->setCommand(CommandTypes::POWER_ON);
|
||||
$trace->setInput($scriptParameters ?? []);
|
||||
break;
|
||||
|
||||
case 'reboot':
|
||||
$trace->setCommand(CommandTypes::REBOOT);
|
||||
$trace->setInput($scriptParameters ?? []);
|
||||
break;
|
||||
|
||||
case 'shutdown':
|
||||
$trace->setCommand(CommandTypes::SHUTDOWN);
|
||||
$trace->setInput($scriptParameters ?? []);
|
||||
break;
|
||||
|
||||
case 'login':
|
||||
$trace->setCommand(CommandTypes::LOGIN);
|
||||
$trace->setInput($scriptParameters ?? []);
|
||||
break;
|
||||
|
||||
default:
|
||||
return;
|
||||
}
|
||||
|
||||
$this->entityManager->persist($trace);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue