/** * @param ScheduledCommand $scheduledCommand * @param OutputInterface $output * @param InputInterface $input */ private function executeCommand(ScheduledCommand $scheduledCommand, OutputInterface $output, InputInterface $input) { $now = new \DateTime(); /** @var ScheduledCommand $scheduledCommand */ $scheduledCommand = $this->entityManager->merge($scheduledCommand); $scheduledCommand->setLastExecution($now); $scheduledCommand->setLocked(true); if ($scheduledCommand->logExecutions()) { $log = new Execution(); $log->setCommand($scheduledCommand); $log->setExecutionDate($now); $this->entityManager->persist($log); $scheduledCommand->addLog($log); } $this->entityManager->flush(); try { $command = $this->getApplication()->find($scheduledCommand->getCommand()); } catch (\InvalidArgumentException $e) { $scheduledCommand->setLastReturnCode(-1); $output->writeln('<error>Cannot find ' . $scheduledCommand->getCommand() . '</error>'); return; } $input = new ArrayInput(array_merge(array('command' => $scheduledCommand->getCommand(), '--env' => $input->getOption('env')), $scheduledCommand->getArguments(true))); // Use a StreamOutput or NullOutput to redirect write() and writeln() in a log file if (false === $this->logPath || "" == $scheduledCommand->getLogFile() || 'null' == $scheduledCommand->getLogFile() || false) { $logOutput = new NullOutput(); } else { $logOutput = new StreamOutput(fopen($this->logPath . $scheduledCommand->getLogFile(), 'a', false), $this->commandsVerbosity); } $commandOutput = new CommandOutput(); $commandOutput->setDefaultOutput($logOutput); // Execute command and get return code try { $output->writeln('<info>Execute</info> : <comment>' . $scheduledCommand->getCommand() . ' ' . $scheduledCommand->getArguments() . '</comment>'); $result = $command->run($input, $commandOutput); } catch (\Exception $e) { $logOutput->writeln($e->getMessage()); $logOutput->writeln($e->getTraceAsString()); $result = -1; } if (false === $this->entityManager->isOpen()) { $output->writeln('<comment>Entity manager closed by the last command.</comment>'); $this->entityManager = $this->entityManager->create($this->entityManager->getConnection(), $this->entityManager->getConfiguration()); } $scheduledCommand = $this->entityManager->merge($scheduledCommand); $scheduledCommand->setLastReturnCode($result); $scheduledCommand->setLocked(false); $scheduledCommand->setExecuteImmediately(false); if ($scheduledCommand->logExecutions()) { /** @var Execution $log */ $log = $scheduledCommand->getCurrentLog(); $log->setReturnCode($result); $log->setOutput($commandOutput->getBuffer('string')); // calculate runtime in seconds $now = new \DateTime(); $runtime = $now->getTimestamp() - $log->getExecutionDate()->getTimestamp(); $log->setRuntime($runtime); } $this->entityManager->flush(); /* * This clear() is necessary to avoid conflict between commands and to be sure that none entity are managed * before entering in a new command */ $this->entityManager->clear(); unset($command); gc_collect_cycles(); }
/** * create new Execution in database * * @param int $id * @param \DateTime $executionDate * @param int $runtime * @param int $returnCode * @param ScheduledCommand $command */ protected function createExecution($id, $executionDate, $runtime, $returnCode, $command) { $execution = new Execution(); $execution->setId($id)->setExecutionDate($executionDate)->setRuntime($runtime)->setReturnCode($returnCode)->setCommand($command)->setOutput("foo\nbar\n"); $this->manager->persist($execution); $this->manager->flush(); }