Small session system / cronjob optimization
authorMarcel Werk <burntime@woltlab.com>
Wed, 30 May 2012 16:02:14 +0000 (18:02 +0200)
committerMarcel Werk <burntime@woltlab.com>
Wed, 30 May 2012 16:02:14 +0000 (18:02 +0200)
com.woltlab.wcf/cronjob.xml
wcfsetup/install/files/lib/data/acp/session/ACPSessionEditor.class.php
wcfsetup/install/files/lib/system/cronjob/AbstractCronjob.class.php [new file with mode: 0644]
wcfsetup/install/files/lib/system/cronjob/CleanUpCronjobLogCronjob.class.php [deleted file]
wcfsetup/install/files/lib/system/cronjob/CleanUpSessionLogCronjob.class.php [deleted file]
wcfsetup/install/files/lib/system/cronjob/DailyCleanUpCronjob.class.php [new file with mode: 0644]
wcfsetup/install/files/lib/system/cronjob/HourlyCleanUpCronjob.class.php [new file with mode: 0644]
wcfsetup/install/files/lib/system/cronjob/SessionCleanUpCronjob.class.php [new file with mode: 0644]
wcfsetup/install/files/lib/system/session/ACPSessionFactory.class.php
wcfsetup/install/files/lib/system/session/SessionHandler.class.php

index 549c13d7da05f8b85aa07a8ff3d366fbd4ca11bf..c94b823ff036a3f8954924a691d71f870143d686 100644 (file)
                </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>
                </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>
\ No newline at end of file
index 9c90afb88d5ba923cd25facef73d47f9408b30da..87a305d37eecf21766b6a09c871349348c4b28cf 100644 (file)
@@ -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 @@ class ACPSessionEditor extends DatabaseObjectEditor {
                
                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));
+       }
 }
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 (file)
index 0000000..a0d183b
--- /dev/null
@@ -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');
+       }
+}
diff --git a/wcfsetup/install/files/lib/system/cronjob/CleanUpCronjobLogCronjob.class.php b/wcfsetup/install/files/lib/system/cronjob/CleanUpCronjobLogCronjob.class.php
deleted file mode 100644 (file)
index 7d69381..0000000
+++ /dev/null
@@ -1,28 +0,0 @@
-<?php
-namespace wcf\system\cronjob;
-use wcf\data\cronjob\Cronjob;
-use wcf\system\WCF;
-
-/**
- * Deletes old entries from cronjob log.
- * 
- * @author     Marcel Werk
- * @copyright  2001-2011 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 {
-       /**
-        * @see wcf\system\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))
-               ));
-       }
-}
diff --git a/wcfsetup/install/files/lib/system/cronjob/CleanUpSessionLogCronjob.class.php b/wcfsetup/install/files/lib/system/cronjob/CleanUpSessionLogCronjob.class.php
deleted file mode 100644 (file)
index 13a0d84..0000000
+++ /dev/null
@@ -1,41 +0,0 @@
-<?php
-namespace wcf\system\cronjob;
-use wcf\data\cronjob\Cronjob;
-use wcf\system\WCF;
-
-/**
- * Deletes old entries from session log.
- * 
- * @author     Marcel Werk
- * @copyright  2001-2011 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 {
-       /**
-        * @see wcf\system\ICronjob::execute()
-        */
-       public function execute(Cronjob $cronjob) {
-               // delete access log
-               $sql = "DELETE FROM     wcf".WCF_N."_acp_session_access_log
-                       WHERE           sessionLogID IN (
-                                               SELECT  sessionLogID
-                                               FROM    wcf".WCF_N."_acp_session_log
-                                               WHERE   lastActivityTime < ?
-                                       )";
-               $statement = WCF::getDB()->prepareStatement($sql);
-               $statement->execute(array(
-                       (TIME_NOW - (86400 * 30))
-               ));
-               
-               // delete session log
-               $sql = "DELETE FROM     wcf".WCF_N."_acp_session_log
-                       WHERE           lastActivityTime < ?";
-               $statement = WCF::getDB()->prepareStatement($sql);
-               $statement->execute(array(
-                       (TIME_NOW - (86400 * 30))
-               ));
-       }
-}
diff --git a/wcfsetup/install/files/lib/system/cronjob/DailyCleanUpCronjob.class.php b/wcfsetup/install/files/lib/system/cronjob/DailyCleanUpCronjob.class.php
new file mode 100644 (file)
index 0000000..6b87d66
--- /dev/null
@@ -0,0 +1,51 @@
+<?php
+namespace wcf\system\cronjob;
+use wcf\data\cronjob\Cronjob;
+use wcf\system\WCF;
+
+/**
+ * Cronjob for a daily system cleanup.
+ * 
+ * @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 DailyCleanUpCronjob extends AbstractCronjob {
+       /**
+        * @see wcf\system\cronjob\ICronjob::execute()
+        */
+       public function execute(Cronjob $cronjob) {
+               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
+                                               FROM    wcf".WCF_N."_acp_session_log
+                                               WHERE   lastActivityTime < ?
+                                       )";
+               $statement = WCF::getDB()->prepareStatement($sql);
+               $statement->execute(array(
+                       (TIME_NOW - (86400 * 30))
+               ));
+               
+               // clean up session log
+               $sql = "DELETE FROM     wcf".WCF_N."_acp_session_log
+                       WHERE           lastActivityTime < ?";
+               $statement = WCF::getDB()->prepareStatement($sql);
+               $statement->execute(array(
+                       (TIME_NOW - (86400 * 30))
+               ));
+       }
+}
diff --git a/wcfsetup/install/files/lib/system/cronjob/HourlyCleanUpCronjob.class.php b/wcfsetup/install/files/lib/system/cronjob/HourlyCleanUpCronjob.class.php
new file mode 100644 (file)
index 0000000..f27d803
--- /dev/null
@@ -0,0 +1,25 @@
+<?php
+namespace wcf\system\cronjob;
+use wcf\data\cronjob\Cronjob;
+use wcf\system\WCF;
+
+/**
+ * Cronjob for a hourly system cleanup.
+ * 
+ * @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 HourlyCleanUpCronjob extends AbstractCronjob {
+       /**
+        * @see wcf\system\cronjob\ICronjob::execute()
+        */
+       public function execute(Cronjob $cronjob) {
+               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 (file)
index 0000000..c59052e
--- /dev/null
@@ -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);
+       }
+}
index 7a368ee0428e4f604e619f88cd9a62708b3cb889..1f2b71088b8e6e2d58067d091f7c2abdba527240 100644 (file)
@@ -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');
                }
        }
        
index ca1f6a1ee5f16cac8c954a27b61dffd67b5fec78..d108e0bd622a11ee5ef680fab195b3ae63d1f08f 100644 (file)
@@ -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 <> ?