From 6aca4e34f9d7168f4d6a6431ee579c28cb141dd3 Mon Sep 17 00:00:00 2001 From: Chris Morgan Date: Thu, 6 Jul 2006 04:39:02 +0000 Subject: [PATCH] Purge orphaned messages from sessionMessages that are older than 1 day after notifying admins of the number of orphaned messages. We currently have over 150k messages stuck in this table with the earliest dating back to 2004. We need to ensure that this doesn't occur again and that we can detect leaked messages as these represent bugs in the appdb code. --- cron/cleanup.php | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/cron/cleanup.php b/cron/cleanup.php index d5ea492..d24bbd3 100644 --- a/cron/cleanup.php +++ b/cron/cleanup.php @@ -72,7 +72,8 @@ notifyAdminsOfCleanupExecution($usersWarned, $usersUnwarnedWithData, $usersDelet /* check to see if there are orphaned versions in the database */ orphanVersionCheck(); - +/* check to see if we have any orphaned messages stuck in sessionMessages table */ +orphanSessionMessagesCheck(); /* Users that are unwarned and inactive since $iMonths */ @@ -149,7 +150,7 @@ function orphanVersionCheck() $found_orphans = false; $sMsg = "Found these orphaned versions in the database with\r\n"; - $sMSg = "this sql command '".$sQuery."'\r\n"; + $sMsg.= "this sql command '".$sQuery."'\r\n"; /* don't report anything if no orphans are found */ if(mysql_num_rows($hResult) == 0) @@ -167,3 +168,32 @@ function orphanVersionCheck() if($sEmail) mail_appdb($sEmail, $sSubject, $sMsg); } + +/* this function checks to see if we have any orphaned session messages */ +/* These orphaned messages are an indication that we've put a message into */ +/* the system without displaying it and it becomes effectively lost forever */ +/* so we'll want to purge them here after reporting how many we have */ +function orphanSessionMessagesCheck() +{ + $iSessionMessageDayLimit = 1; /* the number of days a session message must be stuck before being purges */ + + /* get a count of the messages older than $iSessionMessageDayLimit */ + $sQuery = "SELECT count(*) as cnt from sessionMessages where TO_DAYS(NOW()) - TO_DAYS(time) > ?"; + $hResult = query_parameters($sQuery, $iSessionMessageDayLimit); + + $oRow = mysql_fetch_object($hResult); + $iMessages = $oRow->cnt; + + $sMsg = "Found ".$iMessages." that have been orphaned in the sessionMessages table for longer than ".$iSessionMessageDayLimit." days\r\n"; + $sMsg.= " Purging these messages.\r\n"; + + $sSubject = "Messages orphaned in sessionMessages\r\n"; + + $sEmail = User::get_notify_email_address_list(null, null); /* get list admins */ + if($sEmail) + mail_appdb($sEmail, $sSubject, $sMsg); + + /* purge the messages older than $iSessionMessageDayLimit */ + $sQuery = "DELETE from sessionMessages where TO_DAYS(NOW()) - TO_DAYS(time) > ?"; + $hResult = query_parameters($sQuery, $iSessionMessageDayLimit); +}