aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/phpbb/console/command/cron
diff options
context:
space:
mode:
authorLEZY Thomas <thomas.gif.91@gmail.com>2014-05-29 16:37:45 +0200
committerLEZY Thomas <thomas.gif.91@gmail.com>2014-05-29 16:37:45 +0200
commite7fd259766ff78edf98ee08fff83cb70ac46b6f7 (patch)
tree77e46fc43236e8c88b9665d56b23a8953c1ba7cb /phpBB/phpbb/console/command/cron
parent532e4470ea08e6245878e49e77f1ca51354681e7 (diff)
downloadforums-e7fd259766ff78edf98ee08fff83cb70ac46b6f7.tar
forums-e7fd259766ff78edf98ee08fff83cb70ac46b6f7.tar.gz
forums-e7fd259766ff78edf98ee08fff83cb70ac46b6f7.tar.bz2
forums-e7fd259766ff78edf98ee08fff83cb70ac46b6f7.tar.xz
forums-e7fd259766ff78edf98ee08fff83cb70ac46b6f7.zip
[ticket/12597] Refactoring and test improving
Adding tests of return status Refactoring code Adding consistency in verbose mode PHPBB3-12597
Diffstat (limited to 'phpBB/phpbb/console/command/cron')
-rw-r--r--phpBB/phpbb/console/command/cron/run.php104
1 files changed, 75 insertions, 29 deletions
diff --git a/phpBB/phpbb/console/command/cron/run.php b/phpBB/phpbb/console/command/cron/run.php
index df69b05321..14cda84e7f 100644
--- a/phpBB/phpbb/console/command/cron/run.php
+++ b/phpBB/phpbb/console/command/cron/run.php
@@ -33,7 +33,7 @@ class run extends \phpbb\console\command\command
* Construct method
*
* @param \phpbb\cron\manager $cron_manager The cron manager containing
- * the cron tasks to be executed.
+ * the cron tasks to be executed.
* @param \phpbb\lock\db $lock_db The lock for accessing database.
* @param \phobb\user $user The user object (used to get language information)
*/
@@ -68,14 +68,15 @@ class run extends \phpbb\console\command\command
* 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 the exit status is set to 2. Verbose option does nothing in
- * this case.
+ * argument will be started. If verbose option is specified,
+ * an info message containing the name of the task is printed.
+ * If no task matches the argument given, an error message is printed
+ * and the exit status is set to 2.
*
- * @param InputInterface $input The input stream used to get the argument
+ * @param InputInterface $input The input stream used to get the argument and verboe option.
* @param OutputInterface $output The output stream, used for printing 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
+ * @return int 0 if all is ok, 1 if a lock error occured and 2 if no task matching the argument was found.
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
@@ -84,40 +85,85 @@ class run extends \phpbb\console\command\command
$task_name = $input->getArgument('name');
if ($task_name)
{
- $task = $this->cron_manager->find_task($task_name);
- if ($task)
- {
- $task->run();
- return 0;
- }
- else
- {
- $output->writeln('<error>' . $this->user->lang('CRON_NO_TASK') . '</error>');
- return 2;
- }
+ return $this->run_one($input, $output, $task_name);
}
else
{
- $run_tasks = $this->cron_manager->find_all_ready_tasks();
+ $this->run_all($input, $output);
+ return 0;
+ }
+ }
+ else
+ {
+ $output->writeln('<error>' . $this->user->lang('CRON_LOCK_ERROR') . '</error>');
+ return 1;
+ }
+ }
- foreach ($run_tasks as $task)
- {
- if ($input->getOption('verbose'))
- {
- $output->writeln($this->user->lang('RUNNING_TASK', $task->get_name()));
- }
+ /*
+ * Executes the command in the case when no argument was given.
+ *
+ * If verbose mode is set, an info message will be printed if their is no task to
+ * be run, or else for each starting task.
+ *
+ * @see execute
+ * @param InputInterface $input The input stream used to get the argument and verboe option.
+ * @param OutputInterface $output The output stream, used for printing verbose-mode and error information.
+ * @return null
+ */
+ private function run_all(InputInterface $input, OutputInterface $output)
+ {
+ $run_tasks = $this->cron_manager->find_all_ready_tasks();
- $task->run();
+ if ($run_tasks) {
+ foreach ($run_tasks as $task)
+ {
+ if ($input->getOption('verbose'))
+ {
+ $output->writeln('<info>' . $this->user->lang('RUNNING_TASK', $task->get_name()) . '</info>');
}
- $this->lock_db->release();
- return 0;
+ $task->run();
}
}
else
{
- $output->writeln('<error>' . $this->user->lang('CRON_LOCK_ERROR') . '</error>');
- return 1;
+ $output->writeln('<info>' . $this->user->lang('CRON_NO_TASK') . '</info>');
+ }
+ $this->lock_db->release();
+ }
+
+ /*
+ * Executes the command in the case where an argument is given.
+ *
+ * If their is a task whose name matches the argument, it is run and 0 is returned.
+ * and if verbose mode is set, print an info message with the name of the task.
+ * If there is no task matching $task_name, the function prints an error message
+ * and returns with status 2.
+ *
+ * @see execute
+ * @param string $task_name The name of the task that should be run.
+ * @param InputInterface $input The input stream used to get the argument and verboe option.
+ * @param OutputInterface $output The output stream, used for printing verbose-mode and error information.
+ * @return int 0 if all is well, 2 if no task matches $task_name.
+ */
+ private function run_one(InputInterface $input, OutputInterface $output, $task_name)
+ {
+ $task = $this->cron_manager->find_task($task_name);
+ if ($task)
+ {
+ if ($input->getOption('verbose'))
+ {
+ $output->writeln('<info>' . $this->user->lang('RUNNING_TASK', $task_nameœ) . '</info>');
+ }
+
+ $task->run();
+ return 0;
+ }
+ else
+ {
+ $output->writeln('<error>' . $this->user->lang('CRON_NO_SUCH_TASK') . '</error>');
+ return 2;
}
}
}