[ticket/12597] Changes name of command cron:run-all to cron:run.

Also adds an optional argument to specify one precise cron task to
lauch, and modifies test file accordingly.

PHPBB3-12597
This commit is contained in:
LEZY Thomas 2014-05-28 19:51:59 +02:00
parent 61ad42790f
commit 9f942776ad
4 changed files with 74 additions and 23 deletions

View file

@ -46,8 +46,8 @@ services:
tags:
- { name: console.command }
console.command.cron.run_all:
class: phpbb\console\command\cron\run_all
console.command.cron.run:
class: phpbb\console\command\cron\run
arguments:
- @cron.manager
- @cron.lock_db

View file

@ -221,11 +221,13 @@ $lang = array_merge($lang, array(
'BACK' => 'Back',
'CLI_DESCR_CRON_RUN_ALL' => 'Runs all available cron tasks.',
'CLI_DESCR_CRON_RUN' => 'Runs all available cron tasks.',
'CLI_DESCR_CRON_ARG_RUN_1' => 'What task do you what to run?',
'COLOUR_SWATCH' => 'Web-safe colour swatch',
'CONFIG_UPDATED' => 'Configuration updated successfully.',
'CRON_LOCK_ERROR' => 'Could not obtain cron lock.',
'CRON_NO_TASK' => 'No such cron task',
'DEACTIVATE' => 'Deactivate',
'DIRECTORY_DOES_NOT_EXIST' => 'The entered path “%s” does not exist.',

View file

@ -14,10 +14,11 @@
namespace phpbb\console\command\cron;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class run_all extends \phpbb\console\command\command
class run extends \phpbb\console\command\command
{
/** @var \phpbb\cron\manager */
protected $cron_manager;
@ -52,28 +53,48 @@ class run_all extends \phpbb\console\command\command
protected function configure()
{
$this
->setName('cron:run-all')
->setDescription($this->user->lang('CLI_DESCR_CRON_RUN_ALL'))
->setName('cron:run')
->setDescription($this->user->lang('CLI_DESCR_CRON_RUN'))
->addArgument('name', InputArgument::OPTIONAL, $this->user->lang('CLI_DESCR_CRON_ARG_RUN_1'));
;
}
/**
* Executes the function.
*
* Tries to acquire the cron lock, then runs all ready cron tasks.
* Tries to acquire the cron lock, then if no argument has been given runs all ready cron tasks.
* If the cron lock can not be obtained, an error message is printed
* and the exit status is set to 1.
* If the verbose option is specified, each start of a task is printed.
* Otherwise there is no output.
* If an argument is given to the command, only the task whose name matches the
* argument will be started. If none exists, an error message is
* printed and theexit status is set to -1. Verbose option does nothing in
* this case.
*
* @param InputInterface $input The input stream, unused here
* @param OutputInterface $output The output stream, used for printig verbose-mode
* and error information.
* @return int 0 if all is ok, 1 if a lock error occured
* @param OutputInterface $output The output stream, used for printig verbose-mode and error information.
*
* @return int 0 if all is ok, 1 if a lock error occured and -1 if no task matching the argument was found
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
if ($this->lock_db->acquire())
{
if ($task_name = $input->getArgument('name'))
{
if ($task = $this->cron_manager->find_task($task_name))
{
$task->run();
return 0;
}
else
{
$output->writeln('<error>' . $this->user->lang('CRON_NO_TASK') . '</error>');
return -1;
}
}
else
{
$run_tasks = $this->cron_manager->find_all_ready_tasks();
@ -90,6 +111,7 @@ class run_all extends \phpbb\console\command\command
return 0;
}
}
else
{
$output->writeln('<error>' . $this->user->lang('CRON_LOCK_ERROR') . '</error>');

View file

@ -13,11 +13,11 @@
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;
use phpbb\console\command\cron\run_all;
use phpbb\console\command\cron\run;
require_once dirname(__FILE__) . '/tasks/simple.php';
class phpbb_console_command_cron_run_all_test extends phpbb_database_test_case
class phpbb_console_command_cron_run_test extends phpbb_database_test_case
{
protected $db;
protected $config;
@ -81,12 +81,39 @@ class phpbb_console_command_cron_run_all_test extends phpbb_database_test_case
$this->assertSame(false, $this->task->executed);
}
public function test_arg_valid()
{
$command_tester = $this->get_command_tester();
$command_tester->execute(array('command' => $this->command_name, 'name' => 'phpbb_cron_task_simple'));
$this->assertSame('', $command_tester->getDisplay());
$this->assertSame(true, $this->task->executed);
}
public function test_arg_invalid()
{
$command_tester = $this->get_command_tester();
$command_tester->execute(array('command' => $this->command_name, 'name' => 'foo'));
$this->assertContains('CRON_NO_TASK', $command_tester->getDisplay());
$this->assertSame(false, $this->task->executed);
}
public function test_arg_valid_verbose()
{
$command_tester = $this->get_command_tester();
$command_tester->execute(array('command' => $this->command_name, 'name' => 'phpbb_cron_task_simple', '--verbose' => true));
$this->assertSame('', $command_tester->getDisplay());
$this->assertSame(true, $this->task->executed);
}
public function get_command_tester()
{
$application = new Application();
$application->add(new run_all($this->cron_manager, $this->lock, $this->user));
$application->add(new run($this->cron_manager, $this->lock, $this->user));
$command = $application->find('cron:run-all');
$command = $application->find('cron:run');
$this->command_name = $command->getName();
return new CommandTester($command);
}