diff --git a/phpBB/config/console.yml b/phpBB/config/console.yml index d32befa15e..f268c2f05e 100644 --- a/phpBB/config/console.yml +++ b/phpBB/config/console.yml @@ -46,6 +46,15 @@ services: tags: - { name: console.command } + console.command.cron.execute_all: + class: phpbb\console\command\cron\execute_all + arguments: + - @cron.manager + - @cron.lock_db + - @user + tags: + - { name: console.command } + console.command.db.migrate: class: phpbb\console\command\db\migrate arguments: diff --git a/phpBB/cron.php b/phpBB/cron.php index f63642faa2..a8333aecaf 100644 --- a/phpBB/cron.php +++ b/phpBB/cron.php @@ -39,11 +39,6 @@ function do_cron($cron_lock, $run_tasks) foreach ($run_tasks as $task) { - if (defined('DEBUG') && $config['use_system_cron']) - { - echo "[phpBB cron] Running task '{$task->get_name()}'\n"; - } - $task->run(); } @@ -59,38 +54,28 @@ function do_cron($cron_lock, $run_tasks) // // If DEBUG is defined and cron lock cannot be obtained, a message will be printed. -if (!$config['use_system_cron']) -{ - $cron_type = request_var('cron_type', ''); +$cron_type = request_var('cron_type', ''); - // Comment this line out for debugging so the page does not return an image. - output_image(); -} +// Comment this line out for debugging so the page does not return an image. +output_image(); $cron_lock = $phpbb_container->get('cron.lock_db'); if ($cron_lock->acquire()) { $cron = $phpbb_container->get('cron.manager'); - if ($config['use_system_cron']) + // If invalid task is specified, empty $run_tasks is passed to do_cron which then does nothing + $run_tasks = array(); + $task = $cron->find_task($cron_type); + if ($task) { - $run_tasks = $cron->find_all_ready_tasks(); - } - else - { - // If invalid task is specified, empty $run_tasks is passed to do_cron which then does nothing - $run_tasks = array(); - $task = $cron->find_task($cron_type); - if ($task) + if ($task->is_parametrized()) { - if ($task->is_parametrized()) - { - $task->parse_parameters($request); - } - if ($task->is_ready()) - { - $run_tasks = array($task); - } + $task->parse_parameters($request); + } + if ($task->is_ready()) + { + $run_tasks = array($task); } } @@ -100,6 +85,6 @@ else { if (defined('DEBUG')) { - echo "Could not obtain cron lock.\n"; + echo $this->user->lang('CRON_LOCK_ERROR') . '\n'; } } diff --git a/phpBB/language/en/acp/common.php b/phpBB/language/en/acp/common.php index 6f6a5f901f..9e7f280fa6 100644 --- a/phpBB/language/en/acp/common.php +++ b/phpBB/language/en/acp/common.php @@ -221,8 +221,11 @@ $lang = array_merge($lang, array( 'BACK' => 'Back', + 'CLI_DESCR_CRON_EXECUTE_ALL' => 'Executes all available cron tasks.', + 'COLOUR_SWATCH' => 'Web-safe colour swatch', 'CONFIG_UPDATED' => 'Configuration updated successfully.', + 'CRON_LOCK_ERROR' => 'Could not obtain cron lock.', 'DEACTIVATE' => 'Deactivate', 'DIRECTORY_DOES_NOT_EXIST' => 'The entered path ā€œ%sā€ does not exist.', @@ -283,6 +286,7 @@ $lang = array_merge($lang, array( 'REMIND' => 'Remind', 'RESYNC' => 'Resynchronise', + 'RUNNING_TASK' => 'Running task: %s.', 'SELECT_ANONYMOUS' => 'Select anonymous user', 'SELECT_OPTION' => 'Select option', diff --git a/phpBB/phpbb/console/command/cron/execute_all.php b/phpBB/phpbb/console/command/cron/execute_all.php new file mode 100644 index 0000000000..4f0f225e91 --- /dev/null +++ b/phpBB/phpbb/console/command/cron/execute_all.php @@ -0,0 +1,65 @@ +cron_manager = $cron_manager; + $this->lock_db = $lock_db; + $this->user = $user; + parent::__construct(); + } + + protected function configure() + { + $this + ->setName('cron:execute-all') + ->setDescription($this->user->lang('CLI_DESCR_CRON_EXECUTE_ALL')) + ; + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + if ($this->lock_db->acquire()) + { + $run_tasks = $this->cron_manager->find_all_ready_tasks(); + + foreach ($run_tasks as $task) + { + if ($input->getOption('verbose')) + { + $output->writeln($this->user->lang('RUNNING_TASK', $task->get_name()) . "\n"); + } + + $task->run(); + } + $this->lock_db->release(); + } + else + { + $output->writeln('' . $this->user->lang('CRON_LOCK_ERROR') . ''); + } + } +} + diff --git a/tests/console/cron/execute_all_test.php b/tests/console/cron/execute_all_test.php new file mode 100644 index 0000000000..83cffd5be6 --- /dev/null +++ b/tests/console/cron/execute_all_test.php @@ -0,0 +1,85 @@ +createXMLDataSet(dirname(__FILE__) . '/fixtures/config.xml'); + } + + public function setUp() + { + global $db, $config, $phpbb_root_path, $pathEx; + + $db = $this->db = $this->new_dbal(); + $config = $this->config = new \phpbb\config\config(array('cron_lock' => '0')); + set_config(null, null, null, $this->config); + $this->lock = new \phpbb\lock\db('cron_lock', $this->config, $this->db); + + $this->user = $this->getMock('\phpbb\user'); + $this->user->method('lang')->will($this->returnArgument(0)); + + $tasks = array( + new phpbb_cron_task_core_simple_ready(), + ); + $this->cron_manager = new \phpbb\cron\manager($tasks, $phpbb_root_path, $pathEx); + + $this->assertEquals('0', $config['cron_lock']); + } + + public function test_normal_use() + { + $command_tester = $this->get_command_tester(); + $command_tester->execute(array('command' => $command_name)); + + $this->assertEquals('', $command_tester->getDisplay()); + } + + public function test_verbose_mode() + { + $command_tester = $this->get_command_tester(); + $command_tester->execute(array('command' => $command_name, '--verbose' => true)); + + $this->assertContains('RUNNING_TASK', $command_tester->getDisplay()); + } + + public function test_error_lock() + { + $this->lock->acquire(); + $command_tester = $this->get_command_tester(); + $command_tester->execute(array('command' => $command_name)); + + $this->assertContains('CRON_LOCK_ERROR', $command_tester->getDisplay()); + } + + public function get_command_tester() + { + $application = new Application(); + $application->add(new execute_all($this->cron_manager, $this->lock, $this->user)); + + $command = $application->find('cron:execute-all'); + $command_name = $command->getName(); + return new CommandTester($command); + } +} + diff --git a/tests/console/cron/fixtures/config.xml b/tests/console/cron/fixtures/config.xml new file mode 100644 index 0000000000..b49be1e05a --- /dev/null +++ b/tests/console/cron/fixtures/config.xml @@ -0,0 +1,9 @@ + + + + config_name + config_value + is_dynamic +
+
+ diff --git a/tests/console/cron/tasks/simple_ready.php b/tests/console/cron/tasks/simple_ready.php new file mode 100644 index 0000000000..245d9ba738 --- /dev/null +++ b/tests/console/cron/tasks/simple_ready.php @@ -0,0 +1,13 @@ +