Skip to content

Commit

Permalink
Merge branch 'task_events'
Browse files Browse the repository at this point in the history
  • Loading branch information
agentsib committed May 23, 2016
2 parents 43380d5 + 532ddb1 commit 38254a2
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 16 deletions.
32 changes: 19 additions & 13 deletions Command/CronjobsCronCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -24,6 +25,9 @@ class CronjobsCronCommand extends Command implements ContainerAwareInterface
/** @var ContainerInterface */
private $container;

/**
* {@inheritdoc}
*/
protected function configure ()
{
$this->setName('agentsib:crontab:cron')
Expand All @@ -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');
Expand All @@ -51,20 +58,19 @@ protected function execute (InputInterface $input, OutputInterface $output)

foreach ($manager->getDatabaseCronjobs() as $cronjob) {
/** @var AbstractCronjob $cronjob */

$newRunDate = new \DateTime();
$newDate = new \DateTime();

if (!$cronjob->isExecuteImmediately()) {
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(
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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()) {
Expand All @@ -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();
Expand All @@ -225,15 +235,11 @@ private function executeCommand(AbstractCronjob $cronjob, InputInterface $input,

}


/**
* {@inheritdoc}
*/
public function setContainer (ContainerInterface $container = null)
{
$this->container = $container;
}



}
5 changes: 3 additions & 2 deletions Doctrine/CrontabManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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);
}


Expand Down
73 changes: 73 additions & 0 deletions Event/CronjobsCommandEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php


namespace AgentSIB\CrontabBundle\Event;


use AgentSIB\CrontabBundle\Model\AbstractCronjob;
use Symfony\Component\EventDispatcher\Event;

/**
* Class CronjobsCommandEvent
*
* @package AgentSIB\CrontabBundle\Event
*/
class CronjobsCommandEvent extends Event
{
const ON_EXECUTE_ERROR = 'agentsib.crontab.error';

const ON_EXECUTE_SUCCESS = 'agentsib.crontab.success';

/** @var AbstractCronjob */
private $cronjob;

/** @var string */
private $message;

/** @var string */
private $output;

/**
* @param AbstractCronjob $cronjob
* @param string|null $message
* @param string|null $output
*/
public function __construct(AbstractCronjob $cronjob, $message = null, $output = null)
{
$this->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;
}
}
20 changes: 19 additions & 1 deletion Model/AbstractCrontabManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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();
Expand Down Expand Up @@ -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);
}
}
1 change: 1 addition & 0 deletions Resources/config/services.orm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 38254a2

Please sign in to comment.