# HG changeset patch # User Brian Neal # Date 1299372050 0 # Node ID e9a066db3f544e526e7b607bb0ab08c8064a3fb4 # Parent 1688873085e08885247fb6090a45c2a2c494f81b 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. diff -r 1688873085e0 -r e9a066db3f54 gpp/core/middleware.py --- 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) diff -r 1688873085e0 -r e9a066db3f54 gpp/forums/unread.py --- 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