diff --git a/Command/CronjobsCronCommand.php b/Command/CronjobsCronCommand.php index 954c18f..8569443 100644 --- a/Command/CronjobsCronCommand.php +++ b/Command/CronjobsCronCommand.php @@ -4,6 +4,7 @@ namespace AgentSIB\CrontabBundle\Command; +use AgentSIB\CrontabBundle\Event\CronjobsCommandEvent; use AgentSIB\CrontabBundle\Model\AbstractCronjob; use AgentSIB\CrontabBundle\Model\AbstractCrontabManager; use Cron\CronExpression; @@ -24,6 +25,9 @@ class CronjobsCronCommand extends Command implements ContainerAwareInterface /** @var ContainerInterface */ private $container; + /** + * {@inheritdoc} + */ protected function configure () { $this->setName('agentsib:crontab:cron') @@ -32,6 +36,9 @@ protected function configure () $this->addOption('dry-run', 'r', InputOption::VALUE_NONE, 'Just show commands for execute'); } + /** + * {@inheritdoc} + */ protected function execute (InputInterface $input, OutputInterface $output) { $manager = $this->container->get('agentsib_crontab.manager'); @@ -51,7 +58,6 @@ protected function execute (InputInterface $input, OutputInterface $output) foreach ($manager->getDatabaseCronjobs() as $cronjob) { /** @var AbstractCronjob $cronjob */ - $newRunDate = new \DateTime(); $newDate = new \DateTime(); @@ -59,12 +65,12 @@ protected function execute (InputInterface $input, OutputInterface $output) if ($cronjob->isDisabled() || $cronjob->isLocked() || !$cronjob->getCronExpression()) { continue; } + $cron = CronExpression::factory($cronjob->getCronExpression()); $newRunDate = $cron->getNextRunDate($cronjob->getLastExecution()); } if ($cronjob->isExecuteImmediately()) { - $noneExecution = false; $manager->appendToLog( @@ -120,10 +126,12 @@ protected function execute (InputInterface $input, OutputInterface $output) $manager->stopCronjob($cronjob, -10); + $errStr = sprintf('Cronjob %s killed by timeout', $cronjob->getId()); + $manager->triggerEvent($cronjob, CronjobsCommandEvent::ON_EXECUTE_ERROR, $errStr); $manager->appendToLog( $cronjob, AbstractCrontabManager::CHANNEL_ERROR, - sprintf('Cronjob %s killed by timeout', $cronjob->getId()) + $errStr ); if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) { @@ -137,11 +145,12 @@ protected function execute (InputInterface $input, OutputInterface $output) if (!$process->isRunning()) { if ($process->getExitCode() == 0) { - + $successStr = sprintf('Cronjob %s success completed', $cronjob->getId()); + $manager->triggerEvent($cronjob, CronjobsCommandEvent::ON_EXECUTE_SUCCESS, $successStr, trim($process->getOutput())); $manager->appendToLog( $cronjob, AbstractCrontabManager::CHANNEL_INFO, - sprintf('Cronjob %s success completed', $cronjob->getId()) + $successStr ); if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) { @@ -150,10 +159,12 @@ protected function execute (InputInterface $input, OutputInterface $output) } else { $cronResponseCode = 1; + $errStr = sprintf('Cronjob %s completed. Exit code: %s', $cronjob->getId(), $process->getExitCode()); + $manager->triggerEvent($cronjob, CronjobsCommandEvent::ON_EXECUTE_ERROR, $errStr); $manager->appendToLog( $cronjob, AbstractCrontabManager::CHANNEL_ERROR, - sprintf('Cronjob %s completed. Exit code: %s', $cronjob->getId(), $process->getExitCode()) + sprintf($errStr) ); if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) { @@ -192,14 +203,12 @@ protected function execute (InputInterface $input, OutputInterface $output) } } } + return $cronResponseCode; } - private function executeCommand(AbstractCronjob $cronjob, InputInterface $input, OutputInterface $output) { - $manager = $this->container->get('agentsib_crontab.manager'); - $executableFinder = new PhpExecutableFinder(); if (false === $php = $executableFinder->find()) { @@ -213,6 +222,7 @@ private function executeCommand(AbstractCronjob $cronjob, InputInterface $input, 'agentsib:crontab:execute', $cronjob->getId() )); + $builder->setWorkingDirectory(realpath($this->container->getParameter('kernel.root_dir').'/../')); $process = $builder->getProcess(); @@ -225,7 +235,6 @@ private function executeCommand(AbstractCronjob $cronjob, InputInterface $input, } - /** * {@inheritdoc} */ @@ -233,7 +242,4 @@ public function setContainer (ContainerInterface $container = null) { $this->container = $container; } - - - } \ No newline at end of file diff --git a/Doctrine/CrontabManager.php b/Doctrine/CrontabManager.php index 127ecad..d43688b 100644 --- a/Doctrine/CrontabManager.php +++ b/Doctrine/CrontabManager.php @@ -9,6 +9,7 @@ use Doctrine\Common\Persistence\ObjectManager; use Doctrine\ORM\EntityManager; use Doctrine\ORM\EntityRepository; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; class CrontabManager extends AbstractCrontabManager { @@ -18,12 +19,12 @@ class CrontabManager extends AbstractCrontabManager /** @var EntityRepository */ protected $repository; - public function __construct(ConsoleCommandsParser $commandsParser, ObjectManager $om, $class, $logPath) + public function __construct(ConsoleCommandsParser $commandsParser, ObjectManager $om, EventDispatcherInterface $dispatcher, $class, $logPath) { if (!$om instanceof EntityManager) { throw new \Exception('Wrong $om'); } - parent::__construct($commandsParser, $om, $class, $logPath); + parent::__construct($commandsParser, $om, $dispatcher, $class, $logPath); } diff --git a/Event/CronjobsCommandEvent.php b/Event/CronjobsCommandEvent.php new file mode 100644 index 0000000..975fedd --- /dev/null +++ b/Event/CronjobsCommandEvent.php @@ -0,0 +1,73 @@ +cronjob = $cronjob; + $this->message = $message; + $this->output = $output; + } + + /** + * @return AbstractCronjob + */ + public function getCronjob() + { + return $this->cronjob; + } + + /** + * @return string + */ + public function getCommand() + { + return $this->cronjob->getCommand(); + } + + /** + * @return string + */ + public function getMessage() + { + return $this->message; + } + + /** + * @return string + */ + public function getOutput() + { + return $this->output; + } +} \ No newline at end of file diff --git a/Model/AbstractCrontabManager.php b/Model/AbstractCrontabManager.php index 1070319..a7919e9 100644 --- a/Model/AbstractCrontabManager.php +++ b/Model/AbstractCrontabManager.php @@ -4,10 +4,12 @@ namespace AgentSIB\CrontabBundle\Model; +use AgentSIB\CrontabBundle\Event\CronjobsCommandEvent; use AgentSIB\CrontabBundle\Service\ConsoleCommandsParser; use Cron\CronExpression; use Doctrine\Common\Persistence\ObjectManager; use Doctrine\Common\Persistence\ObjectRepository; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; abstract class AbstractCrontabManager { @@ -30,12 +32,16 @@ abstract class AbstractCrontabManager protected $logPath; - public function __construct(ConsoleCommandsParser $commandsParser, ObjectManager $om, $class, $logPath) + /** @var EventDispatcherInterface */ + protected $dispatcher; + + public function __construct(ConsoleCommandsParser $commandsParser, ObjectManager $om, EventDispatcherInterface $dispatcher, $class, $logPath) { $this->om = $om; $this->repository = $om->getRepository($class); $this->commandsParser = $commandsParser; $this->logPath = $logPath; + $this->dispatcher = $dispatcher; $metadata = $om->getClassMetadata($class); $this->class = $metadata->getName(); @@ -182,4 +188,16 @@ public function appendToLog(AbstractCronjob $cronjob, $channel, $str) } } + /** + * Dispatch crontab event + * + * @param AbstractCronjob $cronjob + * @param string $eventType + * @param string $str + */ + public function triggerEvent(AbstractCronjob $cronjob, $eventType, $str) + { + $event = new CronjobsCommandEvent($cronjob, $str); + $this->dispatcher->dispatch($eventType, $event); + } } \ No newline at end of file diff --git a/Resources/config/services.orm.yml b/Resources/config/services.orm.yml index 1b82bdb..da11bb0 100644 --- a/Resources/config/services.orm.yml +++ b/Resources/config/services.orm.yml @@ -4,6 +4,7 @@ services: arguments: - '@agentsib_crontab.command_parser' - '@agentsib_crontab.entity_manager' + - '@event_dispatcher' - '%agentsib_crontab.cronjob_class%' - '%agentsib_crontab.logs_path%' public: false