diff options
author | Oleg Pudeyev <oleg@bsdpower.com> | 2010-04-15 10:11:40 -0400 |
---|---|---|
committer | Oleg Pudeyev <oleg@bsdpower.com> | 2011-02-12 22:05:49 -0500 |
commit | 61e0285da86ffaf825cfcd486b5372b9566e51d7 (patch) | |
tree | 352d2e8086f34997aa6e910a69fd15976b262cc4 | |
parent | 3956e9f53363171db1ccbafe4971564ff91404e9 (diff) | |
download | forums-61e0285da86ffaf825cfcd486b5372b9566e51d7.tar forums-61e0285da86ffaf825cfcd486b5372b9566e51d7.tar.gz forums-61e0285da86ffaf825cfcd486b5372b9566e51d7.tar.bz2 forums-61e0285da86ffaf825cfcd486b5372b9566e51d7.tar.xz forums-61e0285da86ffaf825cfcd486b5372b9566e51d7.zip |
[feature/system-cron] Reformatted cron, includes/cron and includes/cron_lock.
PHPBB3-9596
-rw-r--r-- | phpBB/cron.php | 37 | ||||
-rw-r--r-- | phpBB/includes/cron.php | 107 | ||||
-rw-r--r-- | phpBB/includes/cron_lock.php | 12 |
3 files changed, 99 insertions, 57 deletions
diff --git a/phpBB/cron.php b/phpBB/cron.php index 1dbe1768c1..5f21e105d3 100644 --- a/phpBB/cron.php +++ b/phpBB/cron.php @@ -21,7 +21,8 @@ include($phpbb_root_path . 'includes/cron_lock.' . $phpEx); $user->session_begin(false); $auth->acl($user->data); -function output_image() { +function output_image() +{ // Output transparent gif header('Cache-Control: no-cache'); header('Content-type: image/gif'); @@ -33,40 +34,50 @@ function output_image() { // flush(); } -function do_cron($run_tasks) { +function do_cron($run_tasks) +{ global $cron_lock; - - foreach ($run_tasks as $cron_type) { + + foreach ($run_tasks as $cron_type) + { $cron->run_task($cron_type); } - + // Unloading cache and closing db after having done the dirty work. $cron_lock->unlock(); garbage_collection(); } -if ($cron_lock->lock()) { - if ($config['use_system_cron']) { +if ($cron_lock->lock()) +{ + if ($config['use_system_cron']) + { $use_shutdown_function = false; - + $run_tasks = $cron->find_all_runnable_tasks(); - } else { + } + else + { $cron_type = request_var('cron_type', ''); $use_shutdown_function = (@function_exists('register_shutdown_function')) ? true : false; - + output_image(); 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; } $run_tasks = array($cron_type); } } - if ($use_shutdown_function) { + if ($use_shutdown_function) + { register_shutdown_function('do_cron', $run_tasks); - } else { + } + else + { do_cron($run_tasks); } } diff --git a/phpBB/includes/cron.php b/phpBB/includes/cron.php index 2aa22858b7..b9a1bb778a 100644 --- a/phpBB/includes/cron.php +++ b/phpBB/includes/cron.php @@ -23,113 +23,142 @@ if (!defined('IN_PHPBB')) class cron { var $tasks = array(); - - function cron() { + + function cron() + { global $config, $phpbb_root_path, $phpEx; $modules = $config['cron_modules']; $modules = explode(',', $modules); - foreach ($modules as $module) { + foreach ($modules as $module) + { // explode will return array("") when exploding an empty string; // users may also specify something like foo,,bar. // Account for module being possibly empty - if (!empty($module)) { + if (!empty($module)) + { // Misspelling or specifying nonexistent modules here may make the board // unusable due to error messages screwing up header output include_once($phpbb_root_path . "includes/cron/$module.$phpEx"); $cron_class = "cron_tasks_$module"; $object = new $cron_class; - foreach ($object->tasks as $cron_type => $params) { + foreach ($object->tasks as $cron_type => $params) + { $params['object'] = $object; $this->tasks[$cron_type] = $params; } } } } - - function is_valid_task($cron_type) { + + function is_valid_task($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; $time_now = time(); $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; } - if ($cron_param['custom_condition']) { + if ($cron_param['custom_condition']) + { $callable = array($cron_params['object'], $cron_type . '_condition'); - if ($args) { + if ($args) + { $answer = call_user_func_array($callable, $args); - } else { + } else + { $answer = call_user_func($callable); } - if (!$answer) { + if (!$answer) + { 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 false; } - - function is_task_shutdown_function_compatible($cron_type) { + + function is_task_shutdown_function_compatible($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')); - } else { + } else + { return true; } } - - function determine_cron_mode_param() { + + function determine_cron_mode_param() + { global $config; - if ($config['use_system_cron']) { + if ($config['use_system_cron']) + { $mode = 'run_from_system'; - } else { + } else + { $mode_param = 'run_from_phpbb'; } return $mode_param; } - - function find_one_runnable_task() { + + function find_one_runnable_task() + { $mode_param = $this->determine_cron_mode_param(); - foreach ($this->tasks as $cron_type => $cron_params) { - if ($cron_params[$mode_param] && $this->is_task_runnable($cron_type)) { + foreach ($this->tasks as $cron_type => $cron_params) + { + if ($cron_params[$mode_param] && $this->is_task_runnable($cron_type)) + { return $cron_type; } } return null; } - - function find_all_runnable_tasks() { + + function find_all_runnable_tasks() + { $mode_param = $this->determine_cron_mode_param(); $tasks = array(); - foreach ($this->tasks as $cron_type => $cron_params) { - if ($cron_params[$mode_param] && $this->is_task_runnable($cron_type)) { + foreach ($this->tasks as $cron_type => $cron_params) + { + if ($cron_params[$mode_param] && $this->is_task_runnable($cron_type)) + { $tasks[] = $cron_type; } } return $tasks; } - - function generate_task_code($cron_type, $args=array()) { + + function generate_task_code($cron_type, $args=array()) + { $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); - } else { + } else + { $code = $this->generate_generic_task_code($cron_type); } return $code; } - - function generate_generic_task_code($cron_type) { + + function generate_generic_task_code($cron_type) + { 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" />'; } - - function run_task($cron_type) { + + function run_task($cron_type) + { call_user_func(array($this->tasks[$cron_type]['object'], 'run_' . $cron_type)); } } diff --git a/phpBB/includes/cron_lock.php b/phpBB/includes/cron_lock.php index 22c052f5ca..1046d62da4 100644 --- a/phpBB/includes/cron_lock.php +++ b/phpBB/includes/cron_lock.php @@ -24,9 +24,10 @@ class cron_lock { private $cron_id; - function lock() { + function lock() + { global $config, $db; - + if (!isset($config['cron_lock'])) { set_config('cron_lock', '0', true); @@ -58,11 +59,12 @@ class cron_lock { return false; } - + return true; } - - function unlock() { + + function unlock() + { global $db; $sql = 'UPDATE ' . CONFIG_TABLE . " |