Abort API refresh when API servers unreachable.

Sorry aboot today.
This commit is contained in:
Adarnof 2016-10-21 20:39:00 -04:00
parent bba36d9ae0
commit 1daf77709d
2 changed files with 20 additions and 0 deletions

View File

@ -63,6 +63,8 @@ def refresh_api(api):
message="Your API key ID %s no longer meets access mask requirements. Required: %s Got: %s" % (
api.api_id, e.required_mask, e.api_mask), level="danger")
still_valid = False
except EveApiManager.ApiServerUnreachableError as e:
logger.warn("Error updating API %s\n%s" % (api.api_id, str(e)))
finally:
if not still_valid:
EveManager.delete_characters_by_api_id(api.api_id, api.user.id)
@ -95,6 +97,10 @@ def refresh_user_apis(user):
@periodic_task(run_every=crontab(minute=0, hour="*/3"))
def run_api_refresh():
if not EveApiManager.check_if_api_server_online():
logger.warn("Aborted scheduled API key refresh: API server unreachable")
return
for u in User.objects.all():
refresh_user_apis.delay(u)

View File

@ -6,6 +6,11 @@ from authentication.states import MEMBER_STATE, BLUE_STATE
from authentication.models import AuthServicesInfo
from eveonline.models import EveCharacter
from django.conf import settings
import requests
try:
from urllib2 import HTTPError, URLError
except ImportError: # py3
from urllib.error import URLError, HTTPError
import logging
@ -41,6 +46,13 @@ class EveApiManager:
msg = 'Key is invalid.'
super(EveApiManager.ApiInvalidError, self).__init__(msg, api_id)
class ApiServerUnreachableError(Exception):
def __init__(self, e):
self.error = e
def __str__(self):
return 'Unable to reach API servers: %s' % str(self.error)
@staticmethod
def get_characters_from_api(api_id, api_key):
logger.debug("Getting characters from api id %s" % api_id)
@ -312,6 +324,8 @@ class EveApiManager:
if int(e.code) in [221, 222]:
raise e
raise EveApiManager.ApiInvalidError(api_id)
except (requests.exceptions.RequestExeception, HTTPError, URLError) as e:
raise EveApiManager.ApiServerUnreachableError(e)
except Exception:
raise EveApiManager.ApiInvalidError(api_id)
auth, c = AuthServicesInfo.objects.get_or_create(user=user)