diff gpp/forums/unread.py @ 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 7e19180b128d
children 42a4e66972d5
line wrap: on
line diff
--- 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