From 05c21784f6b16778b466323fef634a9d485ff945 Mon Sep 17 00:00:00 2001 From: Marcel Werk Date: Wed, 30 May 2012 18:02:14 +0200 Subject: [PATCH] Small session system / cronjob optimization --- com.woltlab.wcf/cronjob.xml | 26 ++++++++--------- .../acp/session/ACPSessionEditor.class.php | 28 +++++++++++++++++++ .../system/cronjob/AbstractCronjob.class.php | 23 +++++++++++++++ ...lass.php => DailyCleanUpCronjob.class.php} | 22 +++++++++++---- ...ass.php => HourlyCleanUpCronjob.class.php} | 17 +++++------ .../cronjob/SessionCleanUpCronjob.class.php | 28 +++++++++++++++++++ .../session/ACPSessionFactory.class.php | 4 +-- .../system/session/SessionHandler.class.php | 9 +++++- 8 files changed, 125 insertions(+), 32 deletions(-) create mode 100644 wcfsetup/install/files/lib/system/cronjob/AbstractCronjob.class.php rename wcfsetup/install/files/lib/system/cronjob/{CleanUpSessionLogCronjob.class.php => DailyCleanUpCronjob.class.php} (64%) rename wcfsetup/install/files/lib/system/cronjob/{CleanUpCronjobLogCronjob.class.php => HourlyCleanUpCronjob.class.php} (50%) create mode 100644 wcfsetup/install/files/lib/system/cronjob/SessionCleanUpCronjob.class.php diff --git a/com.woltlab.wcf/cronjob.xml b/com.woltlab.wcf/cronjob.xml index 549c13d7da..c94b823ff0 100644 --- a/com.woltlab.wcf/cronjob.xml +++ b/com.woltlab.wcf/cronjob.xml @@ -28,10 +28,10 @@ - wcf\system\cronjob\CleanUpCronjobLogCronjob - Deletes old entries from cronjob log + wcf\system\cronjob\DailyCleanUpCronjob + Daily Cleanup 0 - 12 + 1 * * * @@ -41,29 +41,29 @@ - wcf\system\cronjob\CleanUpSessionLogCronjob - Deletes old entries from session log + wcf\system\cronjob\HourlyCleanUpCronjob + Hourly Cleanup 0 - 10 + * * * * 1 - 0 - 0 + 1 + 1 - wcf\system\cronjob\CleanupListenerCronjob - Executes the cleanup system - 5,20,35,50 + wcf\system\cronjob\SessionCleanUpCronjob + Deletes expired sessions + */30 * * * * 1 - 0 - 0 + 1 + 1 \ No newline at end of file diff --git a/wcfsetup/install/files/lib/data/acp/session/ACPSessionEditor.class.php b/wcfsetup/install/files/lib/data/acp/session/ACPSessionEditor.class.php index 9c90afb88d..87a305d37e 100644 --- a/wcfsetup/install/files/lib/data/acp/session/ACPSessionEditor.class.php +++ b/wcfsetup/install/files/lib/data/acp/session/ACPSessionEditor.class.php @@ -1,6 +1,7 @@ $userIDs + */ + public static function deleteUserSessions(array $userIDs = array()) { + $conditionBuilder = new PreparedStatementConditionBuilder(); + if (count($userIDs)) $conditionBuilder->add('userID IN (?)', array($userIDs)); + + $sql = "DELETE FROM ".call_user_func(array(static::$baseClass, 'getDatabaseTableName'))." + ".$conditionBuilder; + $statement = WCF::getDB()->prepareStatement($sql); + $statement->execute($conditionBuilder->getParameters()); + } + + /** + * Deletes the expired sessions. + * + * @param integer $timestamp + */ + public static function deleteExpiredSessions($timestamp) { + $sql = "DELETE FROM ".call_user_func(array(static::$baseClass, 'getDatabaseTableName'))." + WHERE lastActivityTime < ?"; + $statement = WCF::getDB()->prepareStatement($sql); + $statement->execute(array($timestamp)); + } } diff --git a/wcfsetup/install/files/lib/system/cronjob/AbstractCronjob.class.php b/wcfsetup/install/files/lib/system/cronjob/AbstractCronjob.class.php new file mode 100644 index 0000000000..a0d183b465 --- /dev/null +++ b/wcfsetup/install/files/lib/system/cronjob/AbstractCronjob.class.php @@ -0,0 +1,23 @@ + + * @package com.woltlab.wcf + * @subpackage system.cronjob + * @category Community Framework + */ +abstract class AbstractCronjob implements ICronjob { + /** + * @see wcf\system\cronjob\ICronjob::execute() + */ + public function execute(Cronjob $cronjob) { + EventHandler::getInstance()->fireAction($this, 'execute'); + } +} diff --git a/wcfsetup/install/files/lib/system/cronjob/CleanUpSessionLogCronjob.class.php b/wcfsetup/install/files/lib/system/cronjob/DailyCleanUpCronjob.class.php similarity index 64% rename from wcfsetup/install/files/lib/system/cronjob/CleanUpSessionLogCronjob.class.php rename to wcfsetup/install/files/lib/system/cronjob/DailyCleanUpCronjob.class.php index 13a0d844de..6b87d66308 100644 --- a/wcfsetup/install/files/lib/system/cronjob/CleanUpSessionLogCronjob.class.php +++ b/wcfsetup/install/files/lib/system/cronjob/DailyCleanUpCronjob.class.php @@ -4,21 +4,31 @@ use wcf\data\cronjob\Cronjob; use wcf\system\WCF; /** - * Deletes old entries from session log. + * Cronjob for a daily system cleanup. * * @author Marcel Werk - * @copyright 2001-2011 WoltLab GmbH + * @copyright 2001-2012 WoltLab GmbH * @license GNU Lesser General Public License * @package com.woltlab.wcf * @subpackage system.cronjob * @category Community Framework */ -class CleanUpSessionLogCronjob implements ICronjob { +class DailyCleanUpCronjob extends AbstractCronjob { /** - * @see wcf\system\ICronjob::execute() + * @see wcf\system\cronjob\ICronjob::execute() */ public function execute(Cronjob $cronjob) { - // delete access log + parent::execute($cronjob); + + // clean up cronjob log + $sql = "DELETE FROM wcf".WCF_N."_cronjob_log + WHERE execTime < ?"; + $statement = WCF::getDB()->prepareStatement($sql); + $statement->execute(array( + (TIME_NOW - (86400 * 7)) + )); + + // clean up session access log $sql = "DELETE FROM wcf".WCF_N."_acp_session_access_log WHERE sessionLogID IN ( SELECT sessionLogID @@ -30,7 +40,7 @@ class CleanUpSessionLogCronjob implements ICronjob { (TIME_NOW - (86400 * 30)) )); - // delete session log + // clean up session log $sql = "DELETE FROM wcf".WCF_N."_acp_session_log WHERE lastActivityTime < ?"; $statement = WCF::getDB()->prepareStatement($sql); diff --git a/wcfsetup/install/files/lib/system/cronjob/CleanUpCronjobLogCronjob.class.php b/wcfsetup/install/files/lib/system/cronjob/HourlyCleanUpCronjob.class.php similarity index 50% rename from wcfsetup/install/files/lib/system/cronjob/CleanUpCronjobLogCronjob.class.php rename to wcfsetup/install/files/lib/system/cronjob/HourlyCleanUpCronjob.class.php index 7d69381b24..f27d803674 100644 --- a/wcfsetup/install/files/lib/system/cronjob/CleanUpCronjobLogCronjob.class.php +++ b/wcfsetup/install/files/lib/system/cronjob/HourlyCleanUpCronjob.class.php @@ -4,25 +4,22 @@ use wcf\data\cronjob\Cronjob; use wcf\system\WCF; /** - * Deletes old entries from cronjob log. + * Cronjob for a hourly system cleanup. * * @author Marcel Werk - * @copyright 2001-2011 WoltLab GmbH + * @copyright 2001-2012 WoltLab GmbH * @license GNU Lesser General Public License * @package com.woltlab.wcf * @subpackage system.cronjob * @category Community Framework */ -class CleanUpCronjobLogCronjob implements ICronjob { +class HourlyCleanUpCronjob extends AbstractCronjob { /** - * @see wcf\system\ICronjob::execute() + * @see wcf\system\cronjob\ICronjob::execute() */ public function execute(Cronjob $cronjob) { - $sql = "DELETE FROM wcf".WCF_N."_cronjob_log - WHERE execTime < ?"; - $statement = WCF::getDB()->prepareStatement($sql); - $statement->execute(array( - (TIME_NOW - (86400 * 7)) - )); + parent::execute($cronjob); + + // TODO } } diff --git a/wcfsetup/install/files/lib/system/cronjob/SessionCleanUpCronjob.class.php b/wcfsetup/install/files/lib/system/cronjob/SessionCleanUpCronjob.class.php new file mode 100644 index 0000000000..c59052ecea --- /dev/null +++ b/wcfsetup/install/files/lib/system/cronjob/SessionCleanUpCronjob.class.php @@ -0,0 +1,28 @@ + + * @package com.woltlab.wcf + * @subpackage system.cronjob + * @category Community Framework + */ +class SessionCleanUpCronjob extends AbstractCronjob { + /** + * @see wcf\system\cronjob\ICronjob::execute() + */ + public function execute(Cronjob $cronjob) { + parent::execute($cronjob); + + ACPSessionEditor::deleteExpiredSessions(TIME_NOW - SESSION_TIMEOUT); + SessionEditor::deleteExpiredSessions(TIME_NOW - SESSION_TIMEOUT); + } +} diff --git a/wcfsetup/install/files/lib/system/session/ACPSessionFactory.class.php b/wcfsetup/install/files/lib/system/session/ACPSessionFactory.class.php index 7a368ee042..1f2b71088b 100644 --- a/wcfsetup/install/files/lib/system/session/ACPSessionFactory.class.php +++ b/wcfsetup/install/files/lib/system/session/ACPSessionFactory.class.php @@ -29,14 +29,14 @@ class ACPSessionFactory { // call shouldInit event if (!defined('NO_IMPORTS')) { - EventHandler::getInstance()->fireAction($this, 'shouldInit'); + EventHandler::getInstance()->fireAction($this, 'beforeInit'); } $this->init(); // call didInit event if (!defined('NO_IMPORTS')) { - EventHandler::getInstance()->fireAction($this, 'didInit'); + EventHandler::getInstance()->fireAction($this, 'afterInit'); } } diff --git a/wcfsetup/install/files/lib/system/session/SessionHandler.class.php b/wcfsetup/install/files/lib/system/session/SessionHandler.class.php index ca1f6a1ee5..d108e0bd62 100644 --- a/wcfsetup/install/files/lib/system/session/SessionHandler.class.php +++ b/wcfsetup/install/files/lib/system/session/SessionHandler.class.php @@ -333,11 +333,18 @@ class SessionHandler extends SingletonFactory { $this->user = new User(null); } + if ($this->user->userID != 0) { + // user is no guest + // delete all other sessions of this user + call_user_func(array($this->sessionEditorClassName, 'deleteUserSessions', array($this->user->userID))); + } + // save session $this->session = call_user_func(array($this->sessionEditorClassName, 'create'), array( 'sessionID' => $sessionID, 'packageID' => PACKAGE_ID, 'userID' => $this->user->userID, + 'username' => $this->user->username, 'ipAddress' => UserUtil::getIpAddress(), 'userAgent' => UserUtil::getUserAgent(), 'lastActivityTime' => TIME_NOW, @@ -460,7 +467,7 @@ class SessionHandler extends SingletonFactory { $sessionTable = call_user_func(array($this->sessionClassName, 'getDatabaseTableName')); if ($user->userID) { - // user is not a gest, delete all other sessions of this user + // user is not a guest, delete all other sessions of this user $sql = "SELECT sessionID FROM ".$sessionTable." WHERE sessionID <> ? -- 2.20.1