2010-04-17 03:13:30 -04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
*
|
2014-05-27 20:18:06 +02:00
|
|
|
* This file is part of the phpBB Forum Software package.
|
|
|
|
*
|
|
|
|
* @copyright (c) phpBB Limited <https://www.phpbb.com>
|
|
|
|
* @license GNU General Public License, version 2 (GPL-2.0)
|
|
|
|
*
|
|
|
|
* For full copyright and license information, please see
|
|
|
|
* the docs/CREDITS.txt file.
|
2010-04-17 03:13:30 -04:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2013-09-10 14:01:09 +02:00
|
|
|
namespace phpbb\cron;
|
|
|
|
|
2010-04-17 03:13:30 -04:00
|
|
|
/**
|
|
|
|
* Cron manager class.
|
|
|
|
*
|
|
|
|
* Finds installed cron tasks, stores task objects, provides task selection.
|
|
|
|
*/
|
2013-09-10 14:01:09 +02:00
|
|
|
class manager
|
2010-04-17 03:13:30 -04:00
|
|
|
{
|
2010-12-17 01:15:57 +01:00
|
|
|
/**
|
2013-09-10 14:01:09 +02:00
|
|
|
* Set of \phpbb\cron\task\wrapper objects.
|
2010-12-17 01:15:57 +01:00
|
|
|
* Array holding all tasks that have been found.
|
|
|
|
*
|
2011-01-12 23:02:00 +01:00
|
|
|
* @var array
|
2010-12-17 01:15:57 +01:00
|
|
|
*/
|
2011-01-12 23:02:00 +01:00
|
|
|
protected $tasks = array();
|
|
|
|
|
2012-08-25 16:51:19 +02:00
|
|
|
protected $phpbb_root_path;
|
|
|
|
protected $php_ext;
|
2012-04-09 14:34:35 +02:00
|
|
|
|
2010-12-17 01:15:57 +01:00
|
|
|
/**
|
|
|
|
* Constructor. Loads all available tasks.
|
2011-01-13 00:51:32 +01:00
|
|
|
*
|
2014-06-15 13:10:12 +02:00
|
|
|
* @param array|\Traversable $tasks Provides an iterable set of task names
|
2014-05-19 04:07:08 +03:00
|
|
|
* @param string $phpbb_root_path Relative path to phpBB root
|
2014-08-07 13:19:49 +03:00
|
|
|
* @param string $php_ext PHP file extension
|
2010-12-17 01:15:57 +01:00
|
|
|
*/
|
2012-07-26 16:15:56 +02:00
|
|
|
public function __construct($tasks, $phpbb_root_path, $php_ext)
|
2010-04-17 03:13:30 -04:00
|
|
|
{
|
2012-04-09 14:34:35 +02:00
|
|
|
$this->phpbb_root_path = $phpbb_root_path;
|
2012-07-26 16:15:56 +02:00
|
|
|
$this->php_ext = $php_ext;
|
2012-04-09 14:34:35 +02:00
|
|
|
|
2012-04-09 00:22:55 +02:00
|
|
|
$this->load_tasks($tasks);
|
2010-04-17 03:13:30 -04:00
|
|
|
}
|
|
|
|
|
2010-12-17 01:15:57 +01:00
|
|
|
/**
|
|
|
|
* Loads tasks given by name, wraps them
|
|
|
|
* and puts them into $this->tasks.
|
|
|
|
*
|
2014-06-15 13:10:12 +02:00
|
|
|
* @param array|\Traversable $tasks Array of instances of \phpbb\cron\task\task
|
2010-12-17 01:15:57 +01:00
|
|
|
*
|
2012-11-30 23:03:06 -05:00
|
|
|
* @return null
|
2010-12-17 01:15:57 +01:00
|
|
|
*/
|
2012-04-09 00:22:55 +02:00
|
|
|
public function load_tasks($tasks)
|
2010-04-17 03:13:30 -04:00
|
|
|
{
|
2012-04-09 00:22:55 +02:00
|
|
|
foreach ($tasks as $task)
|
2010-04-17 03:13:30 -04:00
|
|
|
{
|
2012-04-09 14:34:35 +02:00
|
|
|
$this->tasks[] = $this->wrap_task($task);
|
2010-04-17 03:13:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Finds a task that is ready to run.
|
2010-12-19 23:56:43 +01:00
|
|
|
*
|
2010-04-17 03:13:30 -04:00
|
|
|
* If several tasks are ready, any one of them could be returned.
|
2010-12-17 01:15:57 +01:00
|
|
|
*
|
2010-12-19 23:56:43 +01:00
|
|
|
* If no tasks are ready, null is returned.
|
|
|
|
*
|
2013-09-10 14:01:09 +02:00
|
|
|
* @return \phpbb\cron\task\wrapper|null
|
2010-04-17 03:13:30 -04:00
|
|
|
*/
|
2010-08-22 21:49:12 -04:00
|
|
|
public function find_one_ready_task()
|
2014-10-27 15:45:24 -07:00
|
|
|
{
|
|
|
|
shuffle($this->tasks);
|
2010-04-17 03:13:30 -04:00
|
|
|
foreach ($this->tasks as $task)
|
|
|
|
{
|
|
|
|
if ($task->is_ready())
|
|
|
|
{
|
|
|
|
return $task;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Finds all tasks that are ready to run.
|
2010-12-17 01:15:57 +01:00
|
|
|
*
|
2013-09-10 14:01:09 +02:00
|
|
|
* @return array List of tasks which are ready to run (wrapped in \phpbb\cron\task\wrapper).
|
2010-04-17 03:13:30 -04:00
|
|
|
*/
|
2010-08-22 21:49:12 -04:00
|
|
|
public function find_all_ready_tasks()
|
2010-04-17 03:13:30 -04:00
|
|
|
{
|
|
|
|
$tasks = array();
|
|
|
|
foreach ($this->tasks as $task)
|
|
|
|
{
|
|
|
|
if ($task->is_ready())
|
|
|
|
{
|
|
|
|
$tasks[] = $task;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $tasks;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Finds a task by name.
|
2010-12-19 23:56:43 +01:00
|
|
|
*
|
|
|
|
* If there is no task with the specified name, null is returned.
|
|
|
|
*
|
2010-04-17 03:13:30 -04:00
|
|
|
* Web runner uses this method to resolve names to tasks.
|
2010-12-17 01:15:57 +01:00
|
|
|
*
|
2010-12-19 23:56:43 +01:00
|
|
|
* @param string $name Name of the task to look up.
|
2013-09-10 14:01:09 +02:00
|
|
|
* @return \phpbb\cron\task\task A task corresponding to the given name, or null.
|
2010-04-17 03:13:30 -04:00
|
|
|
*/
|
2010-08-22 21:49:12 -04:00
|
|
|
public function find_task($name)
|
2010-04-17 03:13:30 -04:00
|
|
|
{
|
|
|
|
foreach ($this->tasks as $task)
|
|
|
|
{
|
|
|
|
if ($task->get_name() == $name)
|
|
|
|
{
|
|
|
|
return $task;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2012-04-09 14:34:35 +02:00
|
|
|
|
2014-06-02 16:12:47 +02:00
|
|
|
/**
|
|
|
|
* Find all tasks and return them.
|
|
|
|
*
|
|
|
|
* @return array List of all tasks.
|
|
|
|
*/
|
|
|
|
public function get_tasks()
|
|
|
|
{
|
2014-06-03 10:42:50 +02:00
|
|
|
return $this->tasks;
|
2014-06-02 16:12:47 +02:00
|
|
|
}
|
|
|
|
|
2012-08-25 16:54:30 +02:00
|
|
|
/**
|
2013-09-10 14:01:09 +02:00
|
|
|
* Wraps a task inside an instance of \phpbb\cron\task\wrapper.
|
2012-08-25 16:54:30 +02:00
|
|
|
*
|
2013-09-10 14:01:09 +02:00
|
|
|
* @param \phpbb\cron\task\task $task The task.
|
|
|
|
* @return \phpbb\cron\task\wrapper The wrapped task.
|
2012-08-25 16:54:30 +02:00
|
|
|
*/
|
2013-09-10 14:01:09 +02:00
|
|
|
public function wrap_task(\phpbb\cron\task\task $task)
|
2012-04-09 14:34:35 +02:00
|
|
|
{
|
2013-09-10 14:01:09 +02:00
|
|
|
return new \phpbb\cron\task\wrapper($task, $this->phpbb_root_path, $this->php_ext);
|
2012-04-09 14:34:35 +02:00
|
|
|
}
|
2010-04-17 03:13:30 -04:00
|
|
|
}
|