[feature/system-cron] Reformatted cron, includes/cron and includes/cron_lock.

PHPBB3-9596
This commit is contained in:
Oleg Pudeyev 2010-04-15 10:11:40 -04:00
parent 3956e9f533
commit 61e0285da8
3 changed files with 99 additions and 57 deletions

View file

@ -21,7 +21,8 @@ include($phpbb_root_path . 'includes/cron_lock.' . $phpEx);
$user->session_begin(false); $user->session_begin(false);
$auth->acl($user->data); $auth->acl($user->data);
function output_image() { function output_image()
{
// Output transparent gif // Output transparent gif
header('Cache-Control: no-cache'); header('Cache-Control: no-cache');
header('Content-type: image/gif'); header('Content-type: image/gif');
@ -33,10 +34,12 @@ function output_image() {
// flush(); // flush();
} }
function do_cron($run_tasks) { function do_cron($run_tasks)
{
global $cron_lock; global $cron_lock;
foreach ($run_tasks as $cron_type) { foreach ($run_tasks as $cron_type)
{
$cron->run_task($cron_type); $cron->run_task($cron_type);
} }
@ -45,12 +48,16 @@ function do_cron($run_tasks) {
garbage_collection(); garbage_collection();
} }
if ($cron_lock->lock()) { if ($cron_lock->lock())
if ($config['use_system_cron']) { {
if ($config['use_system_cron'])
{
$use_shutdown_function = false; $use_shutdown_function = false;
$run_tasks = $cron->find_all_runnable_tasks(); $run_tasks = $cron->find_all_runnable_tasks();
} else { }
else
{
$cron_type = request_var('cron_type', ''); $cron_type = request_var('cron_type', '');
$use_shutdown_function = (@function_exists('register_shutdown_function')) ? true : false; $use_shutdown_function = (@function_exists('register_shutdown_function')) ? true : false;
@ -58,15 +65,19 @@ if ($cron_lock->lock()) {
if ($cron->is_valid_task($cron_type) && $cron->is_task_runnable($cron_type)) if ($cron->is_valid_task($cron_type) && $cron->is_task_runnable($cron_type))
{ {
if ($use_shutdown_function && !$cron->is_task_shutdown_function_compatible($cron_type)) { if ($use_shutdown_function && !$cron->is_task_shutdown_function_compatible($cron_type))
{
$use_shutdown_function = false; $use_shutdown_function = false;
} }
$run_tasks = array($cron_type); $run_tasks = array($cron_type);
} }
} }
if ($use_shutdown_function) { if ($use_shutdown_function)
{
register_shutdown_function('do_cron', $run_tasks); register_shutdown_function('do_cron', $run_tasks);
} else { }
else
{
do_cron($run_tasks); do_cron($run_tasks);
} }
} }

View file

@ -24,21 +24,25 @@ class cron
{ {
var $tasks = array(); var $tasks = array();
function cron() { function cron()
{
global $config, $phpbb_root_path, $phpEx; global $config, $phpbb_root_path, $phpEx;
$modules = $config['cron_modules']; $modules = $config['cron_modules'];
$modules = explode(',', $modules); $modules = explode(',', $modules);
foreach ($modules as $module) { foreach ($modules as $module)
{
// explode will return array("") when exploding an empty string; // explode will return array("") when exploding an empty string;
// users may also specify something like foo,,bar. // users may also specify something like foo,,bar.
// Account for module being possibly empty // Account for module being possibly empty
if (!empty($module)) { if (!empty($module))
{
// Misspelling or specifying nonexistent modules here may make the board // Misspelling or specifying nonexistent modules here may make the board
// unusable due to error messages screwing up header output // unusable due to error messages screwing up header output
include_once($phpbb_root_path . "includes/cron/$module.$phpEx"); include_once($phpbb_root_path . "includes/cron/$module.$phpEx");
$cron_class = "cron_tasks_$module"; $cron_class = "cron_tasks_$module";
$object = new $cron_class; $object = new $cron_class;
foreach ($object->tasks as $cron_type => $params) { foreach ($object->tasks as $cron_type => $params)
{
$params['object'] = $object; $params['object'] = $object;
$this->tasks[$cron_type] = $params; $this->tasks[$cron_type] = $params;
} }
@ -46,90 +50,115 @@ class cron
} }
} }
function is_valid_task($cron_type) { function is_valid_task($cron_type)
{
return isset($this->tasks[$cron_type]); return isset($this->tasks[$cron_type]);
} }
function is_task_runnable($cron_type, $args=null) { function is_task_runnable($cron_type, $args=null)
{
global $config; global $config;
$time_now = time(); $time_now = time();
$cron_params = $this->tasks[$cron_type]; $cron_params = $this->tasks[$cron_type];
if ($cron_params['enable_config'] && !$config[$cron_params['enable_config']]) { if ($cron_params['enable_config'] && !$config[$cron_params['enable_config']])
{
return false; return false;
} }
if ($cron_param['custom_condition']) { if ($cron_param['custom_condition'])
{
$callable = array($cron_params['object'], $cron_type . '_condition'); $callable = array($cron_params['object'], $cron_type . '_condition');
if ($args) { if ($args)
{
$answer = call_user_func_array($callable, $args); $answer = call_user_func_array($callable, $args);
} else { } else
{
$answer = call_user_func($callable); $answer = call_user_func($callable);
} }
if (!$answer) { if (!$answer)
{
return false; return false;
} }
} }
if ($time_now - $config[$cron_params['interval_config']] > $config[$cron_params['last_run_config']]) { if ($time_now - $config[$cron_params['interval_config']] > $config[$cron_params['last_run_config']])
{
return true; return true;
} }
return false; return false;
} }
function is_task_shutdown_function_compatible($cron_type) { function is_task_shutdown_function_compatible($cron_type)
{
$cron_params = $this->tasks[$cron_type]; $cron_params = $this->tasks[$cron_type];
if (isset($cron_params['shutdown_function_condition'])) { if (isset($cron_params['shutdown_function_condition']))
{
return call_user_func(array($cron_params->object, $cron_type . '_shutdown_function_condition')); return call_user_func(array($cron_params->object, $cron_type . '_shutdown_function_condition'));
} else { } else
{
return true; return true;
} }
} }
function determine_cron_mode_param() { function determine_cron_mode_param()
{
global $config; global $config;
if ($config['use_system_cron']) { if ($config['use_system_cron'])
{
$mode = 'run_from_system'; $mode = 'run_from_system';
} else { } else
{
$mode_param = 'run_from_phpbb'; $mode_param = 'run_from_phpbb';
} }
return $mode_param; return $mode_param;
} }
function find_one_runnable_task() { function find_one_runnable_task()
{
$mode_param = $this->determine_cron_mode_param(); $mode_param = $this->determine_cron_mode_param();
foreach ($this->tasks as $cron_type => $cron_params) { foreach ($this->tasks as $cron_type => $cron_params)
if ($cron_params[$mode_param] && $this->is_task_runnable($cron_type)) { {
if ($cron_params[$mode_param] && $this->is_task_runnable($cron_type))
{
return $cron_type; return $cron_type;
} }
} }
return null; return null;
} }
function find_all_runnable_tasks() { function find_all_runnable_tasks()
{
$mode_param = $this->determine_cron_mode_param(); $mode_param = $this->determine_cron_mode_param();
$tasks = array(); $tasks = array();
foreach ($this->tasks as $cron_type => $cron_params) { foreach ($this->tasks as $cron_type => $cron_params)
if ($cron_params[$mode_param] && $this->is_task_runnable($cron_type)) { {
if ($cron_params[$mode_param] && $this->is_task_runnable($cron_type))
{
$tasks[] = $cron_type; $tasks[] = $cron_type;
} }
} }
return $tasks; return $tasks;
} }
function generate_task_code($cron_type, $args=array()) { function generate_task_code($cron_type, $args=array())
{
$cron_params = $this->tasks[$cron_type]; $cron_params = $this->tasks[$cron_type];
if ($cron_params['custom_code']) { if ($cron_params['custom_code'])
{
$code = call_user_func_array(array($cron_params['object'], $cron_type . '_code'), $args); $code = call_user_func_array(array($cron_params['object'], $cron_type . '_code'), $args);
} else { } else
{
$code = $this->generate_generic_task_code($cron_type); $code = $this->generate_generic_task_code($cron_type);
} }
return $code; return $code;
} }
function generate_generic_task_code($cron_type) { function generate_generic_task_code($cron_type)
{
global $phpbb_root_path, $phpEx; global $phpbb_root_path, $phpEx;
return '<img src="' . append_sid($phpbb_root_path . 'cron.' . $phpEx, 'cron_type=' . $cron_type) . '" width="1" height="1" alt="cron" />'; return '<img src="' . append_sid($phpbb_root_path . 'cron.' . $phpEx, 'cron_type=' . $cron_type) . '" width="1" height="1" alt="cron" />';
} }
function run_task($cron_type) { function run_task($cron_type)
{
call_user_func(array($this->tasks[$cron_type]['object'], 'run_' . $cron_type)); call_user_func(array($this->tasks[$cron_type]['object'], 'run_' . $cron_type));
} }
} }

View file

@ -24,7 +24,8 @@ class cron_lock
{ {
private $cron_id; private $cron_id;
function lock() { function lock()
{
global $config, $db; global $config, $db;
if (!isset($config['cron_lock'])) if (!isset($config['cron_lock']))
@ -62,7 +63,8 @@ class cron_lock
return true; return true;
} }
function unlock() { function unlock()
{
global $db; global $db;
$sql = 'UPDATE ' . CONFIG_TABLE . " $sql = 'UPDATE ' . CONFIG_TABLE . "