|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Opifer\CmsBundle\Command; |
| 4 | + |
| 5 | +use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
| 6 | +use Symfony\Component\Console\Input\InputInterface; |
| 7 | +use Symfony\Component\Console\Output\OutputInterface; |
| 8 | +use Symfony\Component\Process\Process; |
| 9 | +use Symfony\Component\Process\ProcessBuilder; |
| 10 | +use Opifer\CmsBundle\Entity\Cron; |
| 11 | + |
| 12 | +/** |
| 13 | + * This command is the main entry point for pending cronjobs |
| 14 | + * |
| 15 | + * To add cronjobs, simply create your own symfony command and add that command |
| 16 | + * to the cronjobs page in the cms. |
| 17 | + * |
| 18 | + * @see http://symfony.com/doc/current/cookbook/console/console_command.html |
| 19 | + */ |
| 20 | +class CronRunCommand extends ContainerAwareCommand |
| 21 | +{ |
| 22 | + /** @var string */ |
| 23 | + private $env; |
| 24 | + |
| 25 | + /** @var ManagerRegistry */ |
| 26 | + private $registry; |
| 27 | + |
| 28 | + /** @var OutputInterface */ |
| 29 | + private $output; |
| 30 | + |
| 31 | + /** |
| 32 | + * {@inheritDoc} |
| 33 | + */ |
| 34 | + protected function configure() |
| 35 | + { |
| 36 | + $this |
| 37 | + ->setName('opifer:cron:run') |
| 38 | + ->setDescription('Runs all due cronjobs from the queue.'); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Execute |
| 43 | + * |
| 44 | + * @param InputInterface $input |
| 45 | + * @param OutputInterface $output |
| 46 | + * |
| 47 | + * @return void |
| 48 | + */ |
| 49 | + protected function execute(InputInterface $input, OutputInterface $output) |
| 50 | + { |
| 51 | + $this->env = $input->getOption('env'); |
| 52 | + $this->registry = $this->getContainer()->get('doctrine'); |
| 53 | + $this->output = $output; |
| 54 | + |
| 55 | + $this->runCrons(); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Run jobs |
| 60 | + * |
| 61 | + * @return void |
| 62 | + */ |
| 63 | + private function runCrons() |
| 64 | + { |
| 65 | + $due = $this->getRepository()->findDue(); |
| 66 | + |
| 67 | + foreach ($due as $cron) { |
| 68 | + $this->startCron($cron); |
| 69 | + } |
| 70 | + |
| 71 | + $this->output->writeln('All done.'); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Start a cron |
| 76 | + * |
| 77 | + * @param Cron $cron |
| 78 | + * |
| 79 | + * @return void |
| 80 | + */ |
| 81 | + private function startCron(Cron $cron) |
| 82 | + { |
| 83 | + $this->output->writeln(sprintf('Started %s.', $cron)); |
| 84 | + $this->changeState($cron, Cron::STATE_RUNNING); |
| 85 | + |
| 86 | + $pb = $this->getCommandProcessBuilder(); |
| 87 | + $pb->add($cron->getCommand()); |
| 88 | + |
| 89 | + $process = $pb->getProcess(); |
| 90 | + $process->run(function ($type, $buffer) { // or ->start() to make processes run asynchronously |
| 91 | + if (Process::ERR === $type) { |
| 92 | + $this->output->writeln('ERR > '.$buffer); |
| 93 | + } else { |
| 94 | + $this->output->writeln(' > '.$buffer); |
| 95 | + } |
| 96 | + }); |
| 97 | + |
| 98 | + if (!$process->isSuccessful()) { |
| 99 | + $this->changeState($cron, Cron::STATE_FAILED); |
| 100 | + |
| 101 | + throw new \RuntimeException($process->getErrorOutput()); |
| 102 | + } |
| 103 | + |
| 104 | + $this->changeState($cron, Cron::STATE_FINISHED); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Change the state of the cron |
| 109 | + * |
| 110 | + * @param Cron $cron |
| 111 | + * @param string $state |
| 112 | + * |
| 113 | + * @return void |
| 114 | + */ |
| 115 | + private function changeState(Cron $cron, $state) |
| 116 | + { |
| 117 | + $cron->setState($state); |
| 118 | + |
| 119 | + $em = $this->getEntityManager(); |
| 120 | + $em->persist($cron); |
| 121 | + $em->flush($cron); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Get the process builder |
| 126 | + * |
| 127 | + * @return \Symfony\Component\Process\ProcessBuilder |
| 128 | + */ |
| 129 | + private function getCommandProcessBuilder() |
| 130 | + { |
| 131 | + $pb = new ProcessBuilder(); |
| 132 | + |
| 133 | + // PHP wraps the process in "sh -c" by default, but we need to control |
| 134 | + // the process directly. |
| 135 | + if (! defined('PHP_WINDOWS_VERSION_MAJOR')) { |
| 136 | + $pb->add('exec'); |
| 137 | + } |
| 138 | + |
| 139 | + $pb |
| 140 | + ->add('php') |
| 141 | + ->add($this->getContainer()->getParameter('kernel.root_dir').'/console') |
| 142 | + ->add('--env='.$this->env) |
| 143 | + ; |
| 144 | + |
| 145 | + return $pb; |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * Get the entity manager |
| 150 | + * |
| 151 | + * @return \Doctrine\ORM\EntityManager |
| 152 | + */ |
| 153 | + private function getEntityManager() |
| 154 | + { |
| 155 | + return $this->registry->getManagerForClass('OpiferCmsBundle:Cron'); |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * Get repository |
| 160 | + * |
| 161 | + * @return \Doctrine\ORM\EntityRepository |
| 162 | + */ |
| 163 | + private function getRepository() |
| 164 | + { |
| 165 | + return $this->getEntityManager()->getRepository('OpiferCmsBundle:Cron'); |
| 166 | + } |
| 167 | +} |
0 commit comments