Skip to content

Commit

Permalink
Small session system / cronjob optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
BurntimeX committed May 30, 2012
1 parent 0d3826f commit 05c2178
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 32 deletions.
26 changes: 13 additions & 13 deletions com.woltlab.wcf/cronjob.xml
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
</cronjob> </cronjob>


<cronjob> <cronjob>
<classname>wcf\system\cronjob\CleanUpCronjobLogCronjob</classname> <classname>wcf\system\cronjob\DailyCleanUpCronjob</classname>
<description>Deletes old entries from cronjob log</description> <description>Daily Cleanup</description>
<startminute>0</startminute> <startminute>0</startminute>
<starthour>12</starthour> <starthour>1</starthour>
<startdom>*</startdom> <startdom>*</startdom>
<startmonth>*</startmonth> <startmonth>*</startmonth>
<startdow>*</startdow> <startdow>*</startdow>
Expand All @@ -41,29 +41,29 @@
</cronjob> </cronjob>


<cronjob> <cronjob>
<classname>wcf\system\cronjob\CleanUpSessionLogCronjob</classname> <classname>wcf\system\cronjob\HourlyCleanUpCronjob</classname>
<description>Deletes old entries from session log</description> <description>Hourly Cleanup</description>
<startminute>0</startminute> <startminute>0</startminute>
<starthour>10</starthour> <starthour>*</starthour>
<startdom>*</startdom> <startdom>*</startdom>
<startmonth>*</startmonth> <startmonth>*</startmonth>
<startdow>*</startdow> <startdow>*</startdow>
<active>1</active> <active>1</active>
<canbeedited>0</canbeedited> <canbeedited>1</canbeedited>
<canbedisabled>0</canbedisabled> <canbedisabled>1</canbedisabled>
</cronjob> </cronjob>


<cronjob> <cronjob>
<classname>wcf\system\cronjob\CleanupListenerCronjob</classname> <classname>wcf\system\cronjob\SessionCleanUpCronjob</classname>
<description>Executes the cleanup system</description> <description>Deletes expired sessions</description>
<startminute>5,20,35,50</startminute> <startminute>*/30</startminute>
<starthour>*</starthour> <starthour>*</starthour>
<startdom>*</startdom> <startdom>*</startdom>
<startmonth>*</startmonth> <startmonth>*</startmonth>
<startdow>*</startdow> <startdow>*</startdow>
<active>1</active> <active>1</active>
<canbeedited>0</canbeedited> <canbeedited>1</canbeedited>
<canbedisabled>0</canbedisabled> <canbedisabled>1</canbedisabled>
</cronjob> </cronjob>
</import> </import>
</data> </data>
Original file line number Original file line Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php <?php
namespace wcf\data\acp\session; namespace wcf\data\acp\session;
use wcf\data\DatabaseObjectEditor; use wcf\data\DatabaseObjectEditor;
use wcf\system\database\util\PreparedStatementConditionBuilder;
use wcf\system\WCF; use wcf\system\WCF;


/** /**
Expand Down Expand Up @@ -46,4 +47,31 @@ public function update(array $parameters = array()) {


return parent::update($parameters); 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));
}
} }
Original file line number Original file line Diff line number Diff line change
@@ -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');
}
}
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -4,21 +4,31 @@
use wcf\system\WCF; use wcf\system\WCF;


/** /**
* Deletes old entries from session log. * Cronjob for a daily system cleanup.
* *
* @author Marcel Werk * @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> * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
* @package com.woltlab.wcf * @package com.woltlab.wcf
* @subpackage system.cronjob * @subpackage system.cronjob
* @category Community Framework * @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) { 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 $sql = "DELETE FROM wcf".WCF_N."_acp_session_access_log
WHERE sessionLogID IN ( WHERE sessionLogID IN (
SELECT sessionLogID SELECT sessionLogID
Expand All @@ -30,7 +40,7 @@ public function execute(Cronjob $cronjob) {
(TIME_NOW - (86400 * 30)) (TIME_NOW - (86400 * 30))
)); ));


// delete session log // clean up session log
$sql = "DELETE FROM wcf".WCF_N."_acp_session_log $sql = "DELETE FROM wcf".WCF_N."_acp_session_log
WHERE lastActivityTime < ?"; WHERE lastActivityTime < ?";
$statement = WCF::getDB()->prepareStatement($sql); $statement = WCF::getDB()->prepareStatement($sql);
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -4,25 +4,22 @@
use wcf\system\WCF; use wcf\system\WCF;


/** /**
* Deletes old entries from cronjob log. * Cronjob for a hourly system cleanup.
* *
* @author Marcel Werk * @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> * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
* @package com.woltlab.wcf * @package com.woltlab.wcf
* @subpackage system.cronjob * @subpackage system.cronjob
* @category Community Framework * @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) { public function execute(Cronjob $cronjob) {
$sql = "DELETE FROM wcf".WCF_N."_cronjob_log parent::execute($cronjob);
WHERE execTime < ?";
$statement = WCF::getDB()->prepareStatement($sql); // TODO
$statement->execute(array(
(TIME_NOW - (86400 * 7))
));
} }
} }
Original file line number Original file line Diff line number Diff line change
@@ -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);
}
}
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ public function load() {


// call shouldInit event // call shouldInit event
if (!defined('NO_IMPORTS')) { if (!defined('NO_IMPORTS')) {
EventHandler::getInstance()->fireAction($this, 'shouldInit'); EventHandler::getInstance()->fireAction($this, 'beforeInit');
} }


$this->init(); $this->init();


// call didInit event // call didInit event
if (!defined('NO_IMPORTS')) { if (!defined('NO_IMPORTS')) {
EventHandler::getInstance()->fireAction($this, 'didInit'); EventHandler::getInstance()->fireAction($this, 'afterInit');
} }
} }


Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -333,11 +333,18 @@ protected function create() {
$this->user = new User(null); $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 // save session
$this->session = call_user_func(array($this->sessionEditorClassName, 'create'), array( $this->session = call_user_func(array($this->sessionEditorClassName, 'create'), array(
'sessionID' => $sessionID, 'sessionID' => $sessionID,
'packageID' => PACKAGE_ID, 'packageID' => PACKAGE_ID,
'userID' => $this->user->userID, 'userID' => $this->user->userID,
'username' => $this->user->username,

This comment has been minimized.

Copy link
@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(), 'ipAddress' => UserUtil::getIpAddress(),
'userAgent' => UserUtil::getUserAgent(), 'userAgent' => UserUtil::getUserAgent(),
'lastActivityTime' => TIME_NOW, 'lastActivityTime' => TIME_NOW,
Expand Down Expand Up @@ -460,7 +467,7 @@ public function changeUser(User $user) {
$sessionTable = call_user_func(array($this->sessionClassName, 'getDatabaseTableName')); $sessionTable = call_user_func(array($this->sessionClassName, 'getDatabaseTableName'));


if ($user->userID) { 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 $sql = "SELECT sessionID
FROM ".$sessionTable." FROM ".$sessionTable."
WHERE sessionID <> ? WHERE sessionID <> ?
Expand Down

0 comments on commit 05c2178

Please sign in to comment.