Skip to content

Commit

Permalink
Add --no-output option to scheduler:execute (hide scheduler output only)
Browse files Browse the repository at this point in the history
  • Loading branch information
j-guyon committed May 26, 2015
1 parent e8a9009 commit 8af729e
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 18 deletions.
69 changes: 55 additions & 14 deletions Command/ExecuteCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Output\StreamOutput;
use JMose\CommandSchedulerBundle\Entity\ScheduledCommand;
Expand All @@ -25,6 +26,21 @@ class ExecuteCommand extends ContainerAwareCommand
*/
private $em;

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

/**
* @var boolean
*/
private $dumpMode;

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

/**
* @inheritdoc
*/
Expand All @@ -34,9 +50,34 @@ protected function configure()
->setName('scheduler:execute')
->setDescription('Execute scheduled commands')
->addOption('dump', null, InputOption::VALUE_NONE, 'Display next execution')
->addOption('no-output', null, InputOption::VALUE_NONE, 'Disable output message from scheduler')
->setHelp('This class is the entry point to execute all scheduled command');
}

/**
* Initialize parameters and services used in execute function
*
* @param InputInterface $input
* @param OutputInterface $output
*/
protected function initialize(InputInterface $input, OutputInterface $output)
{
$this->dumpMode = $input->getOption('dump');
$this->logPath = rtrim($this->getContainer()->getParameter('jmose_command_scheduler.log_path'), '/\\');
$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')
);
}

/**
* @param InputInterface $input
* @param OutputInterface $output
Expand All @@ -45,23 +86,21 @@ protected function configure()
protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln('<info>Start : ' . ($input->getOption('dump') ? 'Dump' : 'Execute') . ' all scheduled command</info>');
$output->writeln('<info>Current server datetime : ' . date('d/m/Y H:i:s') .'</info>');

// Before continue, we check that the output file is valid and writable (except for gaufrette)
if (strpos($this->getContainer()->getParameter('jmose_command_scheduler.log_path'), 'gaufrette:') !== 0 &&
false === is_writable($this->getContainer()->getParameter('jmose_command_scheduler.log_path'))
) {
$output->writeln('<error>' . $this->getContainer()->getParameter('jmose_command_scheduler.log_path') .
' not found or not writable. You should override `log_path` in your config.yml' . '</error>');
if (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>'
);

return;
}
$manager = ($this->getContainer()->hasParameter('jmose_command_scheduler.doctrine_manager')) ? $this->getContainer()->getParameter('jmose_command_scheduler.doctrine_manager') : 'default';
$this->em = $this->getContainer()->get('doctrine')->getManager($manager);
$scheduledCommand = $this->em->getRepository('JMoseCommandSchedulerBundle:ScheduledCommand')->findEnabledCommand();

$commands = $this->em->getRepository('JMoseCommandSchedulerBundle:ScheduledCommand')->findEnabledCommand();

$noneExecution = true;
foreach ($scheduledCommand as $command) {
foreach ($commands as $command) {

/** @var ScheduledCommand $command */
$cron = CronExpression::factory($command->getCronExpression());
Expand All @@ -80,8 +119,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
} elseif ($nextRunDate < $now) {
$noneExecution = false;
$output->writeln(
'Command <comment>' . $command->getCommand() . '</comment> should be executed - last execution : <comment>' .
$command->getLastExecution()->format('d/m/Y H:i:s') . '.</comment>'
'Command <comment>'.$command->getCommand().
'</comment> should be executed - last execution : <comment>'.
$command->getLastExecution()->format('d/m/Y H:i:s').'.</comment>'
);

if (!$input->getOption('dump')) {
Expand Down Expand Up @@ -127,11 +167,12 @@ private function executeCommand(ScheduledCommand $scheduledCommand, OutputInterf
$this->getContainer()->getParameter('jmose_command_scheduler.log_path') .
$scheduledCommand->getLogFile(), 'a', false
));
$logOutput->setVerbosity($output->getVerbosity());
$logOutput->setVerbosity($this->commandsVerbosity);

// Execute command and get return code
try {
$output->writeln('<info>Execute</info> : <comment>' . $scheduledCommand->getCommand() . ' ' .$scheduledCommand->getArguments() . '</comment>');
$output->writeln('<info>Execute</info> : <comment>' . $scheduledCommand->getCommand()
. ' ' .$scheduledCommand->getArguments() . '</comment>');
$result = $command->run($input, $logOutput);
} catch (\Exception $e) {
$logOutput->writeln($e->getMessage());
Expand Down
2 changes: 1 addition & 1 deletion Fixtures/ORM/LoadScheduledCommandData.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function load(ObjectManager $manager)
$this->createScheduledCommand('one', 'container:debug', '--help', '@daily', 'one.log', 100, $beforeYesterday);
$this->createScheduledCommand('two', 'container:debug', '', '@daily', 'two.log', 80, $beforeYesterday, true);
$this->createScheduledCommand('three', 'container:debug', '', '@daily', 'three.log',60, $today, false, true);
$this->createScheduledCommand('four', 'debug:router', '', '@daily', 'four.log', 40, $today, false, false, true);
$this->createScheduledCommand('four', 'router:debug', '', '@daily', 'four.log', 40, $today, false, false, true);
}

/**
Expand Down
31 changes: 28 additions & 3 deletions Tests/Command/ExecuteCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,33 @@ public function testExecute()
$this->assertStringStartsWith('Start : Execute all scheduled command', $output);
$this->assertRegExp('/container:debug should be executed/', $output);
$this->assertRegExp('/Execute : container:debug --help/', $output);
$this->assertRegExp('/Immediately execution asked for : debug:router/', $output);
$this->assertRegExp('/Execute : debug:router/', $output);
$this->assertRegExp('/Immediately execution asked for : router:debug/', $output);
$this->assertRegExp('/Execute : router:debug/', $output);

$output = $this->runCommand('scheduler:execute');
$this->assertRegExp('/Nothing to do/', $output);
}

/**
* Test scheduler:execute without option
*/
public function testExecuteWithNoOutput()
{
//DataFixtures create 4 records
$this->loadFixtures(
array(
'JMose\CommandSchedulerBundle\Fixtures\ORM\LoadScheduledCommandData'
)
);

$output = $this->runCommand(
'scheduler:execute',
array(
'--no-output' => true
)
);

$this->assertEquals('', $output);

$output = $this->runCommand('scheduler:execute');
$this->assertRegExp('/Nothing to do/', $output);
Expand All @@ -56,6 +81,6 @@ public function testExecuteWithDump()

$this->assertStringStartsWith('Start : Dump all scheduled command', $output);
$this->assertRegExp('/Command container:debug should be executed/', $output);
$this->assertRegExp('/Immediately execution asked for : debug:router/', $output);
$this->assertRegExp('/Immediately execution asked for : router:debug/', $output);
}
}

0 comments on commit 8af729e

Please sign in to comment.