Permalink
Browse files

Small session system / cronjob optimization

  • Loading branch information...
BurntimeX committed May 30, 2012
1 parent 0d3826f commit 05c21784f6b16778b466323fef634a9d485ff945
@@ -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>
@@ -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));
+ }
}
@@ -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');
+ }
+}
@@ -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);
@@ -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
}
}
@@ -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);
+ }
+}
@@ -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');
}
}
@@ -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,

This comment has been minimized.

Show comment
Hide comment
@NetzwergX

NetzwergX Jun 1, 2012

Contributor

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

@NetzwergX

NetzwergX Jun 1, 2012

Contributor

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

'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.