/** * @param CronJob $job * * @return CronJobResult * @throws NonUniqueResultException */ public function findMostRecent(CronJob $job = null) { $qb = $this->createQueryBuilder('p'); $expr = $qb->expr(); if ($job) { $qb->andWhere($expr->eq('p.cronJob', ':cronJob')); $qb->setParameter('cronJob', $job->getId()); } $qb->orderBy('p.createdAt', 'DESC'); $qb->setMaxResults(1); return $qb->getQuery()->getOneOrNullResult(); }
/** * @param CronJob $job * @param OutputInterface $output */ protected function runJob(CronJob $job, OutputInterface $output) { $command = $job->getCommand(); $watch = 'job-' . $command; $output->write("Running " . $job->getCommand() . ": "); try { $commandToRun = $this->getApplication()->get($job->getCommand()); } catch (\InvalidArgumentException $ex) { $output->writeln(' skipped (command no longer exists)'); $this->recordJobResult($job, 0, 'Command no longer exists', CronJobResult::SKIPPED); // No need to reschedule non-existant commands return; } $emptyInput = new ArrayInput(['command' => $job->getCommand()]); $jobOutput = new BufferedOutput(); $this->getStopWatch()->start($watch); try { $statusCode = $commandToRun->run($emptyInput, $jobOutput); } catch (\Exception $ex) { $statusCode = CronJobResult::FAILED; $jobOutput->writeln(''); $jobOutput->writeln('Job execution failed with exception ' . get_class($ex) . ':'); $jobOutput->writeln($ex->__toString()); } $this->getStopWatch()->stop($watch); if (is_null($statusCode)) { $statusCode = 0; } $statusStr = CronJobResult::FAILED; switch ($statusCode) { case 0: $statusStr = CronJobResult::SUCCEEDED; break; case 2: $statusStr = CronJobResult::SKIPPED; break; } $bufferedOutput = $jobOutput->fetch(); $output->write($bufferedOutput); $duration = $this->getStopWatch()->getEvent($watch)->getDuration(); $output->writeln($statusStr . ' in ' . number_format($duration / 1000, 2) . ' seconds'); // Record the result $this->recordJobResult($job, $duration, $bufferedOutput, $statusCode); }
/** * @param OutputInterface $output * @param Command $command * @param CronJobAnnotation $annotation * @param bool $defaultDisabled */ protected function newJobFound(OutputInterface $output, Command $command, CronJobAnnotation $annotation, $defaultDisabled = false) { $newJob = new CronJob(); $newJob->setCommand($command->getName()); $newJob->setDescription($command->getDescription()); $newJob->setPeriod($annotation->value); $newJob->setEnable(!$defaultDisabled); $newJob->calculateNextRun(); $output->writeln("Added the job " . $newJob->getCommand() . " with period " . $newJob->getPeriod()); $this->getEntityManager()->persist($newJob); }