Better handling of temp errors, better reporting of actual failures

This commit is contained in:
2011-09-28 09:41:18 +01:00
parent 6f5b12329d
commit 2e70cb8ee4

View File

@@ -8,15 +8,11 @@ from django.db import models, IntegrityError
from django.conf import settings from django.conf import settings
from eve_proxy.exceptions import * from eve_proxy.exceptions import *
# You generally never want to change this unless you have a very good reason. # API URL, can be overriden in the configuration
try: API_URL = getattr(settings, 'EVE_API_URL', 'https://api.eve-online.com')
API_URL = getattr(settings, 'EVE_API_URL')
except AttributeError:
API_URL = 'https://api.eve-online.com'
# Errors to rollback if we have a cached version of the document # Errors to rollback if we have a cached version of the document
# Errors 500-999 at the moment, this can be trimmed down as needed ROLLBACK_ERRORS = range(516, 902)
ROLLBACK_ERRORS = range(500, 999)
class CachedDocumentManager(models.Manager): class CachedDocumentManager(models.Manager):
""" """
@@ -88,7 +84,6 @@ class CachedDocumentManager(models.Manager):
pass pass
logger.error('URL Error: %s' % e, exc_info=sys.exc_info(), extra={'data': {'api-url': url}}) logger.error('URL Error: %s' % e, exc_info=sys.exc_info(), extra={'data': {'api-url': url}})
raise DocumentRetrievalError(e.reason) raise DocumentRetrievalError(e.reason)
else: else:
doc.body = unicode(conn.read(), 'utf-8') doc.body = unicode(conn.read(), 'utf-8')
doc.time_retrieved = datetime.utcnow() doc.time_retrieved = datetime.utcnow()
@@ -100,22 +95,24 @@ class CachedDocumentManager(models.Manager):
except: except:
doc.cached_until = datetime.utcnow() doc.cached_until = datetime.utcnow()
else: else:
doc.cached_until = dom.getElementsByTagName('cachedUntil')[0].childNodes[0].nodeValue date = datetime.strptime(dom.getElementsByTagName('cachedUntil')[0].childNodes[0].nodeValue, '%Y-%m-%d %H:%M:%S')
# Add 30 seconds to the cache timers, avoid hitting too early and account for some minor clock skew.
doc.cached_until = date + timedelta(seconds=getattr(settings, 'EVE_PROXY_CACHE_ADJUSTMENT', 30))
enode = dom.getElementsByTagName('error') enode = dom.getElementsByTagName('error')
if enode: if enode:
error = enode[0].getAttribute('code') error = enode[0].getAttribute('code')
# If we have a error in the ignored error list use the cached doc, otherwise return the new doc if error:
if not error or not error in ROLLBACK_ERRORS: # If we have a rollback error, try and retreive a correct version from the DB
try: if error in ROLLBACK_ERRORS:
doc.save() try:
except IntegrityError: doc = self.get(pk=doc.pk)
# Ignore IntegrityError for this instance, just reload the doc from store except self.DoesNotExist:
pass doc.save()
doc = self.get(pk=doc.pk) else:
elif error in ROLLBACK_ERRORS and not created: logger.error("API Error %s encountered" % error, extra={'data': {'api-url': url, 'error': error, 'document': doc.body}})
logger.info("API Error %s encountered" % error, extra={'data': {'api-url': url, 'error': error, 'document': doc.body}}) else:
doc = self.get(pk=doc.pk) doc.save()
# If this is user related, write a log instance # If this is user related, write a log instance
if params and (params.get('userid', None) or params.get('keyid', None)): if params and (params.get('userid', None) or params.get('keyid', None)):
@@ -153,6 +150,7 @@ class CachedDocument(models.Model):
verbose_name_plural = 'Cached Documents' verbose_name_plural = 'Cached Documents'
ordering = ['-time_retrieved'] ordering = ['-time_retrieved']
class ApiAccessLog(models.Model): class ApiAccessLog(models.Model):
""" """
Provides a list of API accesses made by applications or Auth Provides a list of API accesses made by applications or Auth