Skip to content
Browse files

Small session system / cronjob optimization

  • Loading branch information...
1 parent 0d3826f commit 05c21784f6b16778b466323fef634a9d485ff945 @BurntimeX BurntimeX committed May 30, 2012
View
26 com.woltlab.wcf/cronjob.xml
@@ -28,10 +28,10 @@
</cronjob>
<cronjob>
- <classname>wcf\system\cronjob\CleanUpCronjobLogCronjob</classname>
- <description>Deletes old entries from cronjob log</description>
+ <classname>wcf\system\cronjob\DailyCleanUpCronjob</classname>
+ <description>Daily Cleanup</description>
<startminute>0</startminute>
- <starthour>12</starthour>
+ <starthour>1</starthour>
<startdom>*</startdom>
<startmonth>*</startmonth>
<startdow>*</startdow>
@@ -41,29 +41,29 @@
</cronjob>
<cronjob>
- <classname>wcf\system\cronjob\CleanUpSessionLogCronjob</classname>
- <description>Deletes old entries from session log</description>
+ <classname>wcf\system\cronjob\HourlyCleanUpCronjob</classname>
+ <description>Hourly Cleanup</description>
<startminute>0</startminute>
- <starthour>10</starthour>
+ <starthour>*</starthour>
<startdom>*</startdom>
<startmonth>*</startmonth>
<startdow>*</startdow>
<active>1</active>
- <canbeedited>0</canbeedited>
- <canbedisabled>0</canbedisabled>
+ <canbeedited>1</canbeedited>
+ <canbedisabled>1</canbedisabled>
</cronjob>
<cronjob>
- <classname>wcf\system\cronjob\CleanupListenerCronjob</classname>
- <description>Executes the cleanup system</description>
- <startminute>5,20,35,50</startminute>
+ <classname>wcf\system\cronjob\SessionCleanUpCronjob</classname>
+ <description>Deletes expired sessions</description>
+ <startminute>*/30</startminute>
<starthour>*</starthour>
<startdom>*</startdom>
<startmonth>*</startmonth>
<startdow>*</startdow>
<active>1</active>
- <canbeedited>0</canbeedited>
- <canbedisabled>0</canbedisabled>
+ <canbeedited>1</canbeedited>
+ <canbedisabled>1</canbedisabled>
</cronjob>
</import>
</data>
View
28 wcfsetup/install/files/lib/data/acp/session/ACPSessionEditor.class.php
@@ -1,6 +1,7 @@
<?php
namespace wcf\data\acp\session;
use wcf\data\DatabaseObjectEditor;
+use wcf\system\database\util\PreparedStatementConditionBuilder;
use wcf\system\WCF;
/**
@@ -46,4 +47,31 @@ public function update(array $parameters = array()) {
return parent::update($parameters);
}
+
+ /**
+ * Deletes active sessions of the given users.
+ *
+ * @param array<integer> $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));
+ }
}
View
23 wcfsetup/install/files/lib/system/cronjob/AbstractCronjob.class.php
@@ -0,0 +1,23 @@
+<?php
+namespace wcf\system\cronjob;
+use wcf\data\cronjob\Cronjob;
+use wcf\system\event\EventHandler;
+
+/**
+ * Provides a default implementation for cronjobs.
+ *
+ * @author Marcel Werk
+ * @copyright 2001-2012 WoltLab GmbH
+ * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @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');
+ }
+}
View
22 ...ronjob/CleanUpSessionLogCronjob.class.php → ...tem/cronjob/DailyCleanUpCronjob.class.php
@@ -4,21 +4,31 @@
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 <http://opensource.org/licenses/lgpl-license.php>
* @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 @@ public function execute(Cronjob $cronjob) {
(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);
View
17 ...ronjob/CleanUpCronjobLogCronjob.class.php → ...em/cronjob/HourlyCleanUpCronjob.class.php
@@ -4,25 +4,22 @@
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 <http://opensource.org/licenses/lgpl-license.php>
* @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
}
}
View
28 wcfsetup/install/files/lib/system/cronjob/SessionCleanUpCronjob.class.php
@@ -0,0 +1,28 @@
+<?php
+namespace wcf\system\cronjob;
+use wcf\data\acp\session\ACPSessionEditor;
+use wcf\data\cronjob\Cronjob;
+use wcf\data\session\SessionEditor;
+use wcf\system\WCF;
+
+/**
+ * Deletes expired sesions.
+ *
+ * @author Marcel Werk
+ * @copyright 2001-2012 WoltLab GmbH
+ * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @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);
+ }
+}
View
4 wcfsetup/install/files/lib/system/session/ACPSessionFactory.class.php
@@ -29,14 +29,14 @@ public function load() {
// 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');
}
}
View
9 wcfsetup/install/files/lib/system/session/SessionHandler.class.php
@@ -333,11 +333,18 @@ protected function create() {
$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,
@NetzwergX
NetzwergX added a note Jun 1, 2012

This broke the installation. During installation, the original user has no username and is switched to the newly created admin afterwards.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
'ipAddress' => UserUtil::getIpAddress(),
'userAgent' => UserUtil::getUserAgent(),
'lastActivityTime' => TIME_NOW,
@@ -460,7 +467,7 @@ public function changeUser(User $user) {
$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 <> ?

0 comments on commit 05c2178

Please sign in to comment.
Something went wrong with that request. Please try again.