diff gpp/core/middleware.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 a2d388ed106e
children 3fe60148f75c
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)