[feature/system-cron] Cache cron's task names.

Instead of using a path relative to phpbb_root_path the path to the task
directory is directly passed to the cron manager. Dummy tasks are now
in the tests directory directly.

PHPBB3-9596
This commit is contained in:
Nils Adermann 2011-01-13 00:51:32 +01:00 committed by Oleg Pudeyev
parent 7a8233020b
commit 09b136272b
6 changed files with 106 additions and 21 deletions

View file

@ -242,5 +242,5 @@ foreach ($cache->obtain_hooks() as $hook)
if (!$config['use_system_cron']) if (!$config['use_system_cron'])
{ {
$cron = new phpbb_cron_manager($phpbb_root_path, $phpEx); $cron = new phpbb_cron_manager($phpbb_root_path . 'includes/cron/task', $phpEx, $cache->get_driver());
} }

View file

@ -63,7 +63,7 @@ if ($config['use_system_cron'])
{ {
$use_shutdown_function = false; $use_shutdown_function = false;
$cron = new phpbb_cron_manager($phpbb_root_path, $phpEx); $cron = new phpbb_cron_manager($phpbb_root_path . 'includes/cron/task', $phpEx, $cache->get_driver());
} }
else else
{ {

View file

@ -33,10 +33,10 @@ class phpbb_cron_manager
protected $tasks = array(); protected $tasks = array();
/** /**
* phpBB's root directory. * Directory containing cron tasks
* @var string * @var string
*/ */
protected $phpbb_root_path; protected $task_path;
/** /**
* PHP file extension * PHP file extension
@ -45,12 +45,23 @@ class phpbb_cron_manager
protected $phpEx; protected $phpEx;
/** /**
* Constructor. Loads all available tasks. * Cache driver
* @var phpbb_cache_driver_interface
*/ */
public function __construct($phpbb_root_path, $phpEx) protected $cache;
/**
* Constructor. Loads all available tasks.
*
* @param string $task_path Directory containing cron tasks
* @param string $phpEx PHP file extension
* @param phpbb_cache_driver_interface $cache Cache for task names (optional)
*/
public function __construct($task_path, $phpEx, phpbb_cache_driver_interface $cache = null)
{ {
$this->phpbb_root_path = $phpbb_root_path; $this->task_path = $task_path;
$this->phpEx = $phpEx; $this->phpEx = $phpEx;
$this->cache = $cache;
$task_names = $this->find_cron_task_names(); $task_names = $this->find_cron_task_names();
$this->load_tasks($task_names); $this->load_tasks($task_names);
@ -72,17 +83,25 @@ class phpbb_cron_manager
*/ */
public function find_cron_task_names() public function find_cron_task_names()
{ {
$task_root_path = $this->phpbb_root_path . 'includes/cron/task/'; if ($this->cache)
{
$task_names = $this->cache->get('_cron_tasks');
if ($task_names !== false)
{
return $task_names;
}
}
$task_names = array(); $task_names = array();
$ext = '.' . $this->phpEx; $ext = '.' . $this->phpEx;
$ext_length = strlen($ext); $ext_length = strlen($ext);
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($task_root_path)); $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($this->task_path));
foreach ($iterator as $fileinfo) foreach ($iterator as $fileinfo)
{ {
$file = preg_replace("#^$task_root_path#", '', $fileinfo->getPathname()); $file = preg_replace('#^' . preg_quote($this->task_path, '#') . '#', '', $fileinfo->getPathname());
// skip directories and files direclty in the task root path // skip directories and files direclty in the task root path
if ($fileinfo->isFile() && strpos($file, '/') !== false) if ($fileinfo->isFile() && strpos($file, '/') !== false)
@ -90,11 +109,16 @@ class phpbb_cron_manager
$task_name = str_replace('/', '_', substr($file, 0, -$ext_length)); $task_name = str_replace('/', '_', substr($file, 0, -$ext_length));
if (substr($file, -$ext_length) == $ext && $this->is_valid_name($task_name)) if (substr($file, -$ext_length) == $ext && $this->is_valid_name($task_name))
{ {
$task_names[] = $task_name; $task_names[] = 'phpbb_cron_task_' . $task_name;
} }
} }
} }
if ($this->cache)
{
$this->cache->put('_cron_tasks', $task_names, $ttl = 3600);
}
return $task_names; return $task_names;
} }
@ -123,8 +147,7 @@ class phpbb_cron_manager
{ {
foreach ($task_names as $task_name) foreach ($task_names as $task_name)
{ {
$class = "phpbb_cron_task_$task_name"; $task = new $task_name();
$task = new $class();
$wrapper = new phpbb_cron_task_wrapper($task); $wrapper = new phpbb_cron_task_wrapper($task);
$this->tasks[] = $wrapper; $this->tasks[] = $wrapper;
} }

View file

@ -7,40 +7,56 @@
* *
*/ */
require_once __DIR__ . '/../mock/cache.php';
require_once __DIR__ . '/task/testmod/dummy_task.php';
require_once __DIR__ . '/task/testmod/second_dummy_task.php';
class phpbb_cron_manager_test extends PHPUnit_Framework_TestCase class phpbb_cron_manager_test extends PHPUnit_Framework_TestCase
{ {
public function setUp() public function setUp()
{ {
$this->manager = new phpbb_cron_manager(__DIR__ . '/../../phpBB/', 'php'); $this->manager = new phpbb_cron_manager(__DIR__ . '/task/', 'php');
$this->task_name = 'phpbb_cron_task_testmod_dummy_task';
} }
public function test_manager_finds_shipped_tasks() public function test_manager_finds_shipped_tasks()
{ {
$tasks = $this->manager->find_cron_task_names(); $tasks = $this->manager->find_cron_task_names();
$this->assertGreaterThan(1, count($tasks)); $this->assertEquals(2, sizeof($tasks));
} }
public function test_manager_finds_shipped_task_by_name() public function test_manager_finds_shipped_task_by_name()
{ {
$task = $this->manager->find_task('phpbb_cron_task_core_queue'); $task = $this->manager->find_task($this->task_name);
$this->assertNotNull($task); $this->assertInstanceOf('phpbb_cron_task_wrapper', $task);
$this->assertEquals($this->task_name, $task->get_name());
} }
public function test_manager_instantiates_task_by_name() public function test_manager_instantiates_task_by_name()
{ {
$task = $this->manager->instantiate_task('phpbb_cron_task_core_queue', array()); $task = $this->manager->instantiate_task($this->task_name, array());
$this->assertNotNull($task); $this->assertInstanceOf('phpbb_cron_task_wrapper', $task);
$this->assertEquals($this->task_name, $task->get_name());
} }
public function test_manager_finds_all_ready_tasks() public function test_manager_finds_all_ready_tasks()
{ {
$tasks = $this->manager->find_all_ready_tasks(); $tasks = $this->manager->find_all_ready_tasks();
$this->assertGreaterThan(0, count($tasks)); $this->assertEquals(2, sizeof($tasks));
} }
public function test_manager_finds_one_ready_task() public function test_manager_finds_one_ready_task()
{ {
$task = $this->manager->find_one_ready_task(); $task = $this->manager->find_one_ready_task();
$this->assertNotNull($task); $this->assertInstanceOf('phpbb_cron_task_wrapper', $task);
}
public function test_manager_finds_all_ready_tasks_cached()
{
$cache = new phpbb_mock_cache(array('_cron_tasks' => array($this->task_name)));
$manager = new phpbb_cron_manager(__DIR__ . '/../../phpBB/', 'php', $cache);
$tasks = $manager->find_all_ready_tasks();
$this->assertEquals(1, sizeof($tasks));
} }
} }

View file

@ -0,0 +1,23 @@
<?php
/**
*
* @package testing
* @copyright (c) 2010 phpBB Group
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/
class phpbb_cron_task_testmod_dummy_task extends phpbb_cron_task_base
{
public static $was_run = 0;
public function run()
{
self::$was_run++;
}
public function should_run()
{
return true;
}
}

View file

@ -0,0 +1,23 @@
<?php
/**
*
* @package testing
* @copyright (c) 2010 phpBB Group
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/
class phpbb_cron_task_testmod_second_dummy_task extends phpbb_cron_task_base
{
public static $was_run = 0;
public function run()
{
self::$was_run++;
}
public function should_run()
{
return true;
}
}