public function deleteOldLogs(CronJob $job = null) { // Unfortunately, because we can't use DELETE k WHERE k.id > (SELECT MAX(k2.id) FROM k2) // we have to select the max IDs first $data = $this->getEntityManager()->createQuery("SELECT job.id, MAX(result.id) FROM ColourStreamCronBundle:CronJob job\n JOIN job.results result\n WHERE result.result = :code\n GROUP BY result.job")->setParameter('code', CronJobResult::SUCCEEDED)->getResult(); foreach ($data as $datum) { $jobId = $datum['id']; $minId = $datum[1]; if (!$job || $job->getId() == $jobId) { $this->getEntityManager()->createQuery("DELETE ColourStreamCronBundle:CronJobResult result\n WHERE result.id < :minId\n AND result.job = :jobId")->setParameter('minId', $minId)->setParameter('jobId', $jobId)->getResult(); } } }
protected function runJob(CronJob $job, OutputInterface $output, EntityManager $em, $date = null) { $date = $date == null ? new \DateTime() : $date; $output->setDecorated(true); $output->writeln("<comment>Running " . $job->getCommand() . ": </comment>"); try { $commandToRun = $this->getApplication()->get($job->getCommand()); } catch (InvalidArgumentException $ex) { $output->writeln(" skipped (command no longer exists)"); $this->recordJobResult($em, $job, 0, "Command no longer exists", CronJobResult::SKIPPED); // No need to reschedule non-existant commands return; } $emptyInput = new ArgvInput(); $jobOutput = new MemoryWriter(); $jobStart = microtime(true); try { $returnCode = $commandToRun->execute($emptyInput, $jobOutput); } catch (\Exception $ex) { $returnCode = CronJobResult::FAILED; $jobOutput->writeln(""); $jobOutput->writeln("Job execution failed with exception " . get_class($ex) . ":"); $jobOutput->writeln($ex->__toString()); } $jobEnd = microtime(true); // Clamp the result to accepted values if ($returnCode < CronJobResult::RESULT_MIN || $returnCode > CronJobResult::RESULT_MAX) { $returnCode = CronJobResult::FAILED; } // Output the result $statusStr = "unknown"; if ($returnCode == CronJobResult::SKIPPED) { $statusStr = "skipped"; } elseif ($returnCode == CronJobResult::SUCCEEDED) { $statusStr = "succeeded"; } elseif ($returnCode == CronJobResult::FAILED) { $statusStr = "failed"; } $output->writeln($jobOutput->getOutput()); $durationStr = sprintf("%0.2f", $jobEnd - $jobStart); $output->writeln("{$statusStr} in {$durationStr} seconds"); // Record the result $this->recordJobResult($em, $job, $jobEnd - $jobStart, $jobOutput->getOutput(), $returnCode); // And update the job with it's next scheduled time $newTime = clone $date; $output->writeln('New Time : ' . $newTime->format(DATE_ATOM)); $newTime = $newTime->add(new \DateInterval($job->getInterval())); $output->writeln('New interval Time : ' . $newTime->format(DATE_ATOM)); $job->setNextRun($newTime); }
protected function newJobFound(EntityManager $em, OutputInterface $output, Command $command, CronJobAnno $anno, $defaultDisabled = false) { $newJob = new CronJob(); $newJob->setCommand($command->getName()); $newJob->setDescription($command->getDescription()); $newJob->setInterval($anno->value); $newJob->setNextRun(new \DateTime()); $newJob->setEnabled(!$defaultDisabled); $output->writeln("Added the job " . $newJob->getCommand() . " with interval " . $newJob->getInterval()); $em->persist($newJob); }
protected function newJobFound(EntityManager $em, OutputInterface $output, Command $command, CronJobAnno $anno, $defaultDisabled = false) { $nextRun = $this->getNextRun($anno); $newJob = new CronJob(); $newJob->setCommand($command->getName()); $newJob->setDescription($command->getDescription()); $newJob->setInterval($anno->interval); $newJob->setNextRun($nextRun); $newJob->setEnabled(!$defaultDisabled); $output->writeln("Added the job " . $newJob->getCommand() . " with interval " . $newJob->getInterval() . " with the next run at " . $nextRun->format('Y-m-d H:i:s')); $em->persist($newJob); }