changeset 370:e9a066db3f54

Adding some try/except around some key points in the code to protect against some IntegrityErrors. We log when these happens but otherwise keep going. Tickets: #160 and #169.
author Brian Neal <bgneal@gmail.com>
date Sun, 06 Mar 2011 00:40:50 +0000
parents 1688873085e0
children c8c0fa817a05
files gpp/core/middleware.py gpp/forums/unread.py
diffstat 2 files changed, 27 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/gpp/core/middleware.py	Sat Mar 05 21:29:12 2011 +0000
+++ b/gpp/core/middleware.py	Sun Mar 06 00:40:50 2011 +0000
@@ -1,6 +1,8 @@
 """Common middleware for the entire project."""
 import datetime
+import logging
 
+from django.db import IntegrityError
 from django.contrib.auth import logout
 from django.conf import settings
 
@@ -73,7 +75,13 @@
                             alv = AnonLastVisit(ip=ip)
 
                         alv.last_visit = datetime.datetime.now()
-                        alv.save()
+
+                        # There is a race condition and sometimes another thread
+                        # saves a record before we do; just log this if it happens.
+                        try:
+                            alv.save()
+                        except IntegrityError:
+                            logging.exception('WhosOnline.process_response')
 
                         # set a cookie to expire in 10 minutes or so
                         response.set_cookie(ONLINE_COOKIE, '1', max_age=ONLINE_TIMEOUT)
--- a/gpp/forums/unread.py	Sat Mar 05 21:29:12 2011 +0000
+++ b/gpp/forums/unread.py	Sun Mar 06 00:40:50 2011 +0000
@@ -4,7 +4,9 @@
 been read or not.
 """
 import datetime
+import logging
 
+from django.db import IntegrityError
 from django.db.models import Q
 
 from forums.models import ForumLastVisit, TopicLastVisit, Topic, Forum
@@ -44,7 +46,14 @@
             flv = ForumLastVisit(user=user, forum=forum)
             flv.begin_date = now
             flv.end_date = now
-            flv.save()
+
+            # There is a race condition and sometimes another thread
+            # saves a record before we do; just log this if it happens.
+            try:
+                flv.save()
+            except IntegrityError:
+                logging.exception('get_forum_unread_status')
+
             forum.has_unread = False
             continue
 
@@ -127,7 +136,14 @@
         flv = ForumLastVisit(user=user, forum=forum)
         flv.begin_date = now
         flv.end_date = now
-        flv.save()
+
+        # There is a race condition and sometimes another thread
+        # saves a record before we do; just log this if it happens.
+        try:
+            flv.save()
+        except IntegrityError:
+            logging.exception('get_topic_unread_status')
+
         for topic in topics:
             topic.has_unread = False
         return