Remove old visitors in single process

This commit is contained in:
Dmitriy Simushev 2014-10-31 11:26:53 +00:00
parent f10079497c
commit f0c4299ba3

View File

@ -21,6 +21,7 @@
use Mibew\EventDispatcher\EventDispatcher; use Mibew\EventDispatcher\EventDispatcher;
use Mibew\EventDispatcher\Events; use Mibew\EventDispatcher\Events;
use Mibew\Database; use Mibew\Database;
use Mibew\ProcessLock;
use Mibew\Settings; use Mibew\Settings;
use Mibew\Thread; use Mibew\Thread;
@ -196,28 +197,34 @@ function track_retrieve_details($visitor)
*/ */
function track_remove_old_visitors() function track_remove_old_visitors()
{ {
$db = Database::getInstance(); $lock = new ProcessLock('visitors_remove_old');
if ($lock->get()) {
$db = Database::getInstance();
// Remove associations of visitors with closed threads // Remove associations of visitors with closed threads
$db->query( $db->query(
"UPDATE {sitevisitor} SET threadid = NULL " "UPDATE {sitevisitor} SET threadid = NULL "
. "WHERE threadid IS NOT NULL AND " . "WHERE threadid IS NOT NULL AND "
. "(SELECT count(*) FROM {thread} " . "(SELECT count(*) FROM {thread} "
. "WHERE threadid = {sitevisitor}.threadid " . "WHERE threadid = {sitevisitor}.threadid "
. "AND istate <> " . Thread::STATE_CLOSED . " " . "AND istate <> " . Thread::STATE_CLOSED . " "
. "AND istate <> " . Thread::STATE_LEFT . ") = 0 " . "AND istate <> " . Thread::STATE_LEFT . ") = 0 "
); );
// Remove old visitors // Remove old visitors
$db->query( $db->query(
("DELETE FROM {sitevisitor} " ("DELETE FROM {sitevisitor} "
. "WHERE (:now - lasttime) > :lifetime " . "WHERE (:now - lasttime) > :lifetime "
. "AND threadid IS NULL"), . "AND threadid IS NULL"),
array( array(
':lifetime' => Settings::get('tracking_lifetime'), ':lifetime' => Settings::get('tracking_lifetime'),
':now' => time(), ':now' => time(),
) )
); );
// Release the lock
$lock->release();
}
} }
/** /**