aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/cron.php
diff options
context:
space:
mode:
Diffstat (limited to 'phpBB/cron.php')
-rw-r--r--phpBB/cron.php215
1 files changed, 35 insertions, 180 deletions
diff --git a/phpBB/cron.php b/phpBB/cron.php
index 8000066c92..3f022b1db8 100644
--- a/phpBB/cron.php
+++ b/phpBB/cron.php
@@ -1,10 +1,13 @@
<?php
/**
*
-* @package phpBB3
-* @version $Id$
-* @copyright (c) 2005 phpBB Group
-* @license http://opensource.org/licenses/gpl-license.php GNU Public License
+* 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.
*
*/
@@ -20,196 +23,48 @@ include($phpbb_root_path . 'common.' . $phpEx);
$user->session_begin(false);
$auth->acl($user->data);
-$cron_type = request_var('cron_type', '');
-
-// Output transparent gif
-header('Cache-Control: no-cache');
-header('Content-type: image/gif');
-header('Content-length: 43');
-
-echo base64_decode('R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==');
-
-// Flush here to prevent browser from showing the page as loading while running cron.
-flush();
-
-if (!isset($config['cron_lock']))
+function output_image()
{
- set_config('cron_lock', '0', true);
-}
+ // Output transparent gif
+ header('Cache-Control: no-cache');
+ header('Content-type: image/gif');
+ header('Content-length: 43');
-// make sure cron doesn't run multiple times in parallel
-if ($config['cron_lock'])
-{
- // if the other process is running more than an hour already we have to assume it
- // aborted without cleaning the lock
- $time = explode(' ', $config['cron_lock']);
- $time = $time[0];
+ echo base64_decode('R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==');
- if ($time + 3600 >= time())
- {
- exit;
- }
+ // Flush here to prevent browser from showing the page as loading while
+ // running cron.
+ flush();
}
-define('CRON_ID', time() . ' ' . unique_id());
+// Thanks to various fatal errors and lack of try/finally, it is quite easy to leave
+// the cron lock locked, especially when working on cron-related code.
+//
+// Attempt to alleviate the problem by doing setup outside of the lock as much as possible.
-$sql = 'UPDATE ' . CONFIG_TABLE . "
- SET config_value = '" . $db->sql_escape(CRON_ID) . "'
- WHERE config_name = 'cron_lock' AND config_value = '" . $db->sql_escape($config['cron_lock']) . "'";
-$db->sql_query($sql);
+$cron_type = request_var('cron_type', '');
-// another cron process altered the table between script start and UPDATE query so exit
-if ($db->sql_affectedrows() != 1)
-{
- exit;
-}
+// Comment this line out for debugging so the page does not return an image.
+output_image();
-/**
-* Run cron-like action
-* Real cron-based layer will be introduced in 3.2
-*/
-switch ($cron_type)
+$cron_lock = $phpbb_container->get('cron.lock_db');
+if ($cron_lock->acquire())
{
- case 'queue':
-
- if (time() - $config['queue_interval'] <= $config['last_queue_run'] || !file_exists($phpbb_root_path . 'cache/queue.' . $phpEx))
- {
- break;
- }
-
- include_once($phpbb_root_path . 'includes/functions_messenger.' . $phpEx);
- $queue = new queue();
-
- $queue->process();
-
- break;
-
- case 'tidy_cache':
-
- if (time() - $config['cache_gc'] <= $config['cache_last_gc'] || !method_exists($cache, 'tidy'))
- {
- break;
- }
-
- $cache->tidy();
-
- break;
-
- case 'tidy_search':
-
- // Select the search method
- $search_type = basename($config['search_type']);
-
- if (time() - $config['search_gc'] <= $config['search_last_gc'] || !file_exists($phpbb_root_path . 'includes/search/' . $search_type . '.' . $phpEx))
- {
- break;
- }
-
- include_once("{$phpbb_root_path}includes/search/$search_type.$phpEx");
-
- // We do some additional checks in the module to ensure it can actually be utilised
- $error = false;
- $search = new $search_type($error);
-
- if ($error)
- {
- break;
- }
-
- $search->tidy();
-
- break;
+ $cron = $phpbb_container->get('cron.manager');
- case 'tidy_warnings':
-
- if (time() - $config['warnings_gc'] <= $config['warnings_last_gc'])
- {
- break;
- }
-
- include_once($phpbb_root_path . 'includes/functions_admin.' . $phpEx);
-
- tidy_warnings();
-
- break;
-
- case 'tidy_database':
-
- if (time() - $config['database_gc'] <= $config['database_last_gc'])
- {
- break;
- }
-
- include_once($phpbb_root_path . 'includes/functions_admin.' . $phpEx);
-
- tidy_database();
-
- break;
-
- case 'tidy_sessions':
-
- if (time() - $config['session_gc'] <= $config['session_last_gc'])
- {
- break;
- }
-
- $user->session_gc();
-
- break;
-
- case 'prune_forum':
-
- $forum_id = request_var('f', 0);
-
- $sql = 'SELECT forum_id, prune_next, enable_prune, prune_days, prune_viewed, forum_flags, prune_freq
- FROM ' . FORUMS_TABLE . "
- WHERE forum_id = $forum_id";
- $result = $db->sql_query($sql);
- $row = $db->sql_fetchrow($result);
- $db->sql_freeresult($result);
-
- if (!$row)
+ $task = $cron->find_task($cron_type);
+ if ($task)
+ {
+ if ($task->is_parametrized())
{
- break;
+ $task->parse_parameters($request);
}
-
- // Do the forum Prune thang
- if ($row['prune_next'] < time() && $row['enable_prune'])
+ if ($task->is_ready())
{
- include_once($phpbb_root_path . 'includes/functions_admin.' . $phpEx);
-
- if ($row['prune_days'])
- {
- auto_prune($row['forum_id'], 'posted', $row['forum_flags'], $row['prune_days'], $row['prune_freq']);
- }
-
- if ($row['prune_viewed'])
- {
- auto_prune($row['forum_id'], 'viewed', $row['forum_flags'], $row['prune_viewed'], $row['prune_freq']);
- }
+ $task->run();
}
-
- break;
+ }
+ $cron_lock->release();
}
-// Unloading cache and closing db after having done the dirty work.
-unlock_cron();
garbage_collection();
-
-exit;
-
-
-/**
-* Unlock cron script
-*/
-function unlock_cron()
-{
- global $db;
-
- $sql = 'UPDATE ' . CONFIG_TABLE . "
- SET config_value = '0'
- WHERE config_name = 'cron_lock' AND config_value = '" . $db->sql_escape(CRON_ID) . "'";
- $db->sql_query($sql);
-}
-
-?> \ No newline at end of file