diff options
Diffstat (limited to 'phpBB/cron.php')
| -rw-r--r-- | phpBB/cron.php | 308 | 
1 files changed, 71 insertions, 237 deletions
| diff --git a/phpBB/cron.php b/phpBB/cron.php index 3993a149b5..6de493f0bf 100644 --- a/phpBB/cron.php +++ b/phpBB/cron.php @@ -20,268 +20,102 @@ include($phpbb_root_path . 'common.' . $phpEx);  $user->session_begin(false);  $auth->acl($user->data); -$cron_type = request_var('cron_type', ''); -$use_shutdown_function = (@function_exists('register_shutdown_function')) ? true : false; - -// Output transparent gif -header('Cache-Control: no-cache'); -header('Content-type: image/gif'); -header('Content-length: 43'); - -echo base64_decode('R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=='); - -// test without flush ;) -// flush(); - -// -if (!isset($config['cron_lock'])) -{ -	set_config('cron_lock', '0', true); -} - -// make sure cron doesn't run multiple times in parallel -if ($config['cron_lock']) +function output_image()  { -	// 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]; +	// Output transparent gif +	header('Cache-Control: no-cache'); +	header('Content-type: image/gif'); +	header('Content-length: 43'); -	if ($time + 3600 >= time()) -	{ -		exit; -	} -} - -define('CRON_ID', time() . ' ' . unique_id()); +	echo base64_decode('R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=='); -$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); - -// another cron process altered the table between script start and UPDATE query so exit -if ($db->sql_affectedrows() != 1) -{ -	exit; +	// Flush here to prevent browser from showing the page as loading while +	// running cron. +	flush();  } -/** -* Run cron-like action -* Real cron-based layer will be introduced in 3.2 -*/ -switch ($cron_type) +function do_cron($cron_lock, $run_tasks)  { -	case 'queue': - -		if (time() - $config['queue_interval'] <= $config['last_queue_run'] || !file_exists($phpbb_root_path . 'cache/queue.' . $phpEx)) -		{ -			break; -		} - -		// A user reported using the mail() function while using shutdown does not work. We do not want to risk that. -		if ($use_shutdown_function && !$config['smtp_delivery']) -		{ -			$use_shutdown_function = false; -		} - -		include_once($phpbb_root_path . 'includes/functions_messenger.' . $phpEx); -		$queue = new queue(); - -		if ($use_shutdown_function) -		{ -			register_shutdown_function(array(&$queue, 'process')); -		} -		else -		{ -			$queue->process(); -		} - -	break; - -	case 'tidy_cache': - -		if (time() - $config['cache_gc'] <= $config['cache_last_gc'] || !method_exists($cache, 'tidy')) -		{ -			break; -		} - -		if ($use_shutdown_function) -		{ -			register_shutdown_function(array(&$cache, 'tidy')); -		} -		else -		{ -			$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; -		} - -		if ($use_shutdown_function) -		{ -			register_shutdown_function(array(&$search, 'tidy')); -		} -		else -		{ -			$search->tidy(); -		} - -	break; - -	case 'tidy_warnings': - -		if (time() - $config['warnings_gc'] <= $config['warnings_last_gc']) -		{ -			break; -		} - -		include_once($phpbb_root_path . 'includes/functions_admin.' . $phpEx); - -		if ($use_shutdown_function) -		{ -			register_shutdown_function('tidy_warnings'); -		} -		else -		{ -			tidy_warnings(); -		} +	global $config; -	break; - -	case 'tidy_database': - -		if (time() - $config['database_gc'] <= $config['database_last_gc']) -		{ -			break; -		} - -		include_once($phpbb_root_path . 'includes/functions_admin.' . $phpEx); - -		if ($use_shutdown_function) -		{ -			register_shutdown_function('tidy_database'); -		} -		else -		{ -			tidy_database(); -		} - -	break; - -	case 'tidy_sessions': - -		if (time() - $config['session_gc'] <= $config['session_last_gc']) +	foreach ($run_tasks as $task) +	{ +		if (defined('DEBUG_EXTRA') && $config['use_system_cron'])  		{ -			break; +			echo "[phpBB cron] Running task '{$task->get_name()}'\n";  		} -		if ($use_shutdown_function) -		{ -			register_shutdown_function(array(&$user, 'session_gc')); -		} -		else -		{ -			$user->session_gc(); -		} +		$task->run(); +	} -	break; +	// Unloading cache and closing db after having done the dirty work. +	$cron_lock->release(); +	garbage_collection(); +} -	case 'prune_forum': +// 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. +// +// If DEBUG_EXTRA is defined and cron lock cannot be obtained, a message will be printed. -		$forum_id = request_var('f', 0); +if ($config['use_system_cron']) +{ +	$use_shutdown_function = false; -		$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); +	$cron = new phpbb_cron_manager($phpbb_root_path . 'includes/cron/task', $phpEx, $cache->get_driver()); +} +else +{ +	$cron_type = request_var('cron_type', ''); +	$use_shutdown_function = (@function_exists('register_shutdown_function')) ? true : false; -		if (!$row) -		{ -			break; -		} +	// Comment this line out for debugging so the page does not return an image. +	output_image(); +} -		// Do the forum Prune thang -		if ($row['prune_next'] < time() && $row['enable_prune']) +$cron_lock = new phpbb_lock_db('cron_lock', $config, $db); +if ($cron_lock->acquire()) +{ +	if ($config['use_system_cron']) +	{ +		$run_tasks = $cron->find_all_ready_tasks(); +	} +	else +	{ +		// If invalid task is specified, empty $run_tasks is passed to do_cron which then does nothing +		$run_tasks = array(); +		$task = $cron->find_task($cron_type); +		if ($task)  		{ -			include_once($phpbb_root_path . 'includes/functions_admin.' . $phpEx); - -			if ($row['prune_days']) +			if ($task->is_parametrized())  			{ -				if ($use_shutdown_function) -				{ -					register_shutdown_function('auto_prune', $row['forum_id'], 'posted', $row['forum_flags'], $row['prune_days'], $row['prune_freq']); -				} -				else -				{ -					auto_prune($row['forum_id'], 'posted', $row['forum_flags'], $row['prune_days'], $row['prune_freq']); -				} +				$task->parse_parameters($request);  			} - -			if ($row['prune_viewed']) +			if ($task->is_ready())  			{ -				if ($use_shutdown_function) +				if ($use_shutdown_function && !$task->is_shutdown_function_safe())  				{ -					register_shutdown_function('auto_prune', $row['forum_id'], 'viewed', $row['forum_flags'], $row['prune_viewed'], $row['prune_freq']); -				} -				else -				{ -					auto_prune($row['forum_id'], 'viewed', $row['forum_flags'], $row['prune_viewed'], $row['prune_freq']); +					$use_shutdown_function = false;  				} +				$run_tasks = array($task);  			}  		} - -	break; -} - -// Unloading cache and closing db after having done the dirty work. -if ($use_shutdown_function) -{ -	register_shutdown_function('unlock_cron'); -	register_shutdown_function('garbage_collection'); +	} +	if ($use_shutdown_function) +	{ +		register_shutdown_function('do_cron', $cron_lock, $run_tasks); +	} +	else +	{ +		do_cron($cron_lock, $run_tasks); +	}  }  else  { -	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); +	if (defined('DEBUG_EXTRA')) +	{ +		echo "Could not obtain cron lock.\n"; +	}  } - -?>
\ No newline at end of file | 
