Skip to content

Commit

Permalink
Merge 87dab5f into e4344a3
Browse files Browse the repository at this point in the history
  • Loading branch information
j-guyon committed Dec 6, 2018
2 parents e4344a3 + 87dab5f commit 8d97c60
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 48 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Expand Up @@ -14,6 +14,8 @@ matrix:
env: SYMFONY_VERSION=3.4.*
- php: 7.2
env: SYMFONY_VERSION=4.0.*
- php: 7.2
env: SYMFONY_VERSION=4.2.*
- php: nightly
env: SYMFONY_VERSION=4.0.*

Expand Down
37 changes: 24 additions & 13 deletions Command/ExecuteCommand.php
Expand Up @@ -3,7 +3,8 @@
namespace JMose\CommandSchedulerBundle\Command;

use Cron\CronExpression;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Bridge\Doctrine\ManagerRegistry;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand All @@ -18,7 +19,7 @@
* @author Julien Guyon <julienguyon@hotmail.com>
* @package JMose\CommandSchedulerBundle\Command
*/
class ExecuteCommand extends ContainerAwareCommand
class ExecuteCommand extends Command
{

/**
Expand All @@ -41,6 +42,25 @@ class ExecuteCommand extends ContainerAwareCommand
*/
private $commandsVerbosity;

/**
* ExecuteCommand constructor.
* @param ManagerRegistry $managerRegistry
* @param $managerName
* @param $logPath
*/
public function __construct(ManagerRegistry $managerRegistry, $managerName, $logPath)
{
$this->em = $managerRegistry->getManager($managerName);
$this->logPath = $logPath;

// If logpath is not set to false, append the directory separator to it
if (false !== $this->logPath) {
$this->logPath = rtrim($this->logPath, '/\\').DIRECTORY_SEPARATOR;
}

parent::__construct();
}

/**
* @inheritdoc
*/
Expand All @@ -63,23 +83,13 @@ protected function configure()
protected function initialize(InputInterface $input, OutputInterface $output)
{
$this->dumpMode = $input->getOption('dump');
$this->logPath = $this->getContainer()->getParameter('jmose_command_scheduler.log_path');

// If logpath is not set to false, append the directory separator to it
if (false !== $this->logPath) {
$this->logPath = rtrim($this->logPath, '/\\').DIRECTORY_SEPARATOR;
}

// Store the original verbosity before apply the quiet parameter
$this->commandsVerbosity = $output->getVerbosity();

if (true === $input->getOption('no-output')) {
$output->setVerbosity(OutputInterface::VERBOSITY_QUIET);
}

$this->em = $this->getContainer()->get('doctrine')->getManager(
$this->getContainer()->getParameter('jmose_command_scheduler.doctrine_manager')
);
}

/**
Expand All @@ -94,7 +104,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
// Before continue, we check that the output file is valid and writable (except for gaufrette)
if (false !== $this->logPath && strpos($this->logPath, 'gaufrette:') !== 0 && false === is_writable(
$this->logPath
)) {
)
) {
$output->writeln(
'<error>'.$this->logPath.
' not found or not writable. You should override `log_path` in your config.yml'.'</error>'
Expand Down
59 changes: 35 additions & 24 deletions Command/MonitorCommand.php
Expand Up @@ -2,7 +2,8 @@

namespace JMose\CommandSchedulerBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Bridge\Doctrine\ManagerRegistry;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
Expand All @@ -13,7 +14,7 @@
* @author Daniel Fischer <dfischer000@gmail.com>
* @package JMose\CommandSchedulerBundle\Command
*/
class MonitorCommand extends ContainerAwareCommand
class MonitorCommand extends Command
{

/**
Expand Down Expand Up @@ -46,6 +47,32 @@ class MonitorCommand extends ContainerAwareCommand
*/
private $sendMailIfNoError;

/**
* MonitorCommand constructor.
* @param ManagerRegistry $managerRegistry
* @param $managerName
* @param $lockTimeout
* @param $receiver
* @param $mailSubject
* @param $sendMailIfNoError
*/
public function __construct(
ManagerRegistry $managerRegistry,
$managerName,
$lockTimeout,
$receiver,
$mailSubject,
$sendMailIfNoError
) {
$this->em = $managerRegistry->getManager($managerName);
$this->lockTimeout = $lockTimeout;
$this->receiver = $receiver;
$this->mailSubject = $mailSubject;
$this->sendMailIfNoError = $sendMailIfNoError;

parent::__construct();
}

/**
* @inheritdoc
*/
Expand All @@ -58,25 +85,6 @@ protected function configure()
->setHelp('This class is for monitoring all active commands.');
}

/**
* Initialize parameters and services used in execute function
*
* @param InputInterface $input
* @param OutputInterface $output
*/
protected function initialize(InputInterface $input, OutputInterface $output)
{
$this->lockTimeout = $this->getContainer()->getParameter('jmose_command_scheduler.lock_timeout');
$this->dumpMode = $input->getOption('dump');
$this->receiver = $this->getContainer()->getParameter('jmose_command_scheduler.monitor_mail');
$this->mailSubject = $this->getContainer()->getParameter('jmose_command_scheduler.monitor_mail_subject');
$this->sendMailIfNoError = $this->getContainer()->getParameter('jmose_command_scheduler.send_ok');

$this->em = $this->getContainer()->get('doctrine')->getManager(
$this->getContainer()->getParameter('jmose_command_scheduler.doctrine_manager')
);
}

/**
* @param InputInterface $input
* @param OutputInterface $output
Expand All @@ -85,6 +93,7 @@ protected function initialize(InputInterface $input, OutputInterface $output)
protected function execute(InputInterface $input, OutputInterface $output)
{
// If not in dump mode and none receiver is set, exit.
$this->dumpMode = $input->getOption('dump');
if (!$this->dumpMode && count($this->receiver) === 0) {
$output->writeln('Please add receiver in configuration');

Expand All @@ -100,7 +109,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
$message = "";

foreach ($failedCommands as $command) {
$message .= sprintf("%s: returncode %s, locked: %s, last execution: %s\n",
$message .= sprintf(
"%s: returncode %s, locked: %s, last execution: %s\n",
$command->getName(),
$command->getLastReturnCode(),
$command->getLocked(),
Expand Down Expand Up @@ -134,8 +144,8 @@ private function sendMails($message)
// prepare email constants
$hostname = gethostname();
$subject = $this->getMailSubject();
$headers = 'From: cron-monitor@' . $hostname . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$headers = 'From: cron-monitor@'.$hostname."\r\n".
'X-Mailer: PHP/'.phpversion();

foreach ($this->receiver as $rcv) {
mail(trim($rcv), $subject, $message, $headers);
Expand All @@ -150,6 +160,7 @@ private function sendMails($message)
private function getMailSubject()
{
$hostname = gethostname();

return sprintf($this->mailSubject, $hostname, date('Y-m-d H:i:s'));
}

Expand Down
30 changes: 23 additions & 7 deletions Command/UnlockCommand.php
Expand Up @@ -3,7 +3,8 @@
namespace JMose\CommandSchedulerBundle\Command;

use JMose\CommandSchedulerBundle\Entity\ScheduledCommand;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Bridge\Doctrine\ManagerRegistry;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand All @@ -15,14 +16,19 @@
* @author Marcel Pfeiffer <m.pfeiffer@strucnamics.de>
* @package JMose\CommandSchedulerBundle\Command
*/
class UnlockCommand extends ContainerAwareCommand
class UnlockCommand extends Command
{

/**
* @var \Doctrine\ORM\EntityManager
*/
private $em;

/**
* @var integer
*/
private $defaultLockTimeout;

/**
* @var integer|boolean Number of seconds after a command is considered as timeout
*/
Expand All @@ -38,6 +44,20 @@ class UnlockCommand extends ContainerAwareCommand
*/
private $scheduledCommandName = [];

/**
* UnlockCommand constructor.
* @param ManagerRegistry $managerRegistry
* @param $managerName
* @param $lockTimeout
*/
public function __construct(ManagerRegistry $managerRegistry, $managerName, $lockTimeout)
{
$this->em = $managerRegistry->getManager($managerName);
$this->defaultLockTimeout = $lockTimeout;

parent::__construct();
}

/**
* @inheritdoc
*/
Expand Down Expand Up @@ -69,16 +89,12 @@ protected function initialize(InputInterface $input, OutputInterface $output)

$this->lockTimeout = $input->getOption('lock-timeout', null);
if ($this->lockTimeout === null) {
$this->lockTimeout = $this->getContainer()->getParameter('jmose_command_scheduler.lock_timeout');
$this->lockTimeout = $this->defaultLockTimeout;
} else {
if ($this->lockTimeout === 'false') {
$this->lockTimeout = false;
}
}

$this->em = $this->getContainer()->get('doctrine')->getManager(
$this->getContainer()->getParameter('jmose_command_scheduler.doctrine_manager')
);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions Controller/BaseController.php
Expand Up @@ -2,15 +2,15 @@

namespace JMose\CommandSchedulerBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

/**
* Class BaseController
*
* @author Julien Guyon <julienguyon@hotmail.com>
* @package JMose\CommandSchedulerBundle\Controller
*/
abstract class BaseController extends Controller
abstract class BaseController extends AbstractController
{
/**
* @return \Doctrine\Common\Persistence\ObjectManager|object
Expand Down
10 changes: 8 additions & 2 deletions DependencyInjection/Configuration.php
Expand Up @@ -17,8 +17,14 @@ class Configuration implements ConfigurationInterface
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('jmose_command_scheduler');
$treeBuilder = new TreeBuilder('jmose_command_scheduler');
if (method_exists($treeBuilder, 'getRootNode')) {
$rootNode = $treeBuilder->getRootNode();
} else {
// BC layer for symfony/config 4.1 and older
$rootNode = $treeBuilder->root('jmose_command_scheduler');
}

$rootNode
->children()
->scalarNode('doctrine_manager')->defaultValue('default')->end()
Expand Down
15 changes: 15 additions & 0 deletions Resources/config/services.yml
Expand Up @@ -10,10 +10,25 @@ services:
- { name: form.type, alias: command_choice }

JMose\CommandSchedulerBundle\Command\ExecuteCommand:
arguments:
- "@doctrine"
- "%jmose_command_scheduler.doctrine_manager%"
- "%jmose_command_scheduler.log_path%"
tags: [console.command]

JMose\CommandSchedulerBundle\Command\MonitorCommand:
arguments:
- "@doctrine"
- "%jmose_command_scheduler.doctrine_manager%"
- "%jmose_command_scheduler.lock_timeout%"
- "%jmose_command_scheduler.monitor_mail%"
- "%jmose_command_scheduler.monitor_mail_subject%"
- "%jmose_command_scheduler.send_ok%"
tags: [console.command]

JMose\CommandSchedulerBundle\Command\UnlockCommand:
arguments:
- "@doctrine"
- "%jmose_command_scheduler.doctrine_manager%"
- "%jmose_command_scheduler.lock_timeout%"
tags: [console.command]

0 comments on commit 8d97c60

Please sign in to comment.