diff --git a/src/Command/RunScheduledCommandTasksCommand.php b/src/Command/RunScheduledCommandTasksCommand.php new file mode 100644 index 0000000..06e9971 --- /dev/null +++ b/src/Command/RunScheduledCommandTasksCommand.php @@ -0,0 +1,90 @@ +setTimezone(new \DateTimeZone('Europe/Madrid')); + $nowMinute = $now->format('Y-m-d H:i'); + + $tasks = $this->commandTaskRepository->findAll(); + + foreach ($tasks as $task) { + /** @var CommandTask $task */ + $nextExecution = $task->getNextExecution(); + if (!$nextExecution) { + continue; + } + + $taskMinute = $nextExecution->format('Y-m-d H:i'); + + if ($taskMinute === $nowMinute) { + $output->writeln("Ejecutando tarea: " . $task->getName()); + + $scripts = $task->getCommandTaskScripts()->toArray(); + usort($scripts, fn($a, $b) => $a->getExecutionOrder() <=> $b->getExecutionOrder()); + + foreach ($scripts as $script) { + try { + $output->writeln(" - Ejecutando script de tipo {$script->getType()} con orden {$script->getExecutionOrder()}"); + + if ($script->getType() === 'run-script') { + $input = new CommandExecuteInput(); + + foreach ($task->getOrganizationalUnit()?->getClients() as $client) { + $input->clients[] = new ClientOutput($client); + } + $input->script = $script->getContent(); + + $this->runScriptAction->__invoke($input); + } + } catch (TransportExceptionInterface $e) { + $output->writeln("Error ejecutando script: " . $e->getMessage()); + continue; + } + } + + $task->setLastExecution(new \DateTime()); + $task->setNextExecution($task->calculateNextExecutionDate()); + $this->entityManager->persist($task); + } + } + + $this->entityManager->flush(); + return Command::SUCCESS; + } +}