From d486f979bb8d05669a85762b0319d67adb6162df Mon Sep 17 00:00:00 2001 From: Adarnof Date: Thu, 10 Dec 2015 07:50:56 +0000 Subject: [PATCH] Tolerate random 221 errors from API servers Implements #185 --- celerytask/tasks.py | 42 ++++++++++++++++++++++++---- eveonline/models.py | 3 +- services/managers/eve_api_manager.py | 10 +++---- 3 files changed, 43 insertions(+), 12 deletions(-) diff --git a/celerytask/tasks.py b/celerytask/tasks.py index f1c26465..8b220a51 100755 --- a/celerytask/tasks.py +++ b/celerytask/tasks.py @@ -321,25 +321,57 @@ def run_api_refresh(): still_valid = True if authserviceinfo.is_blue: if settings.BLUE_API_ACCOUNT: - if not EveApiManager.check_api_is_type_account(api_key_pair.api_id, api_key_pair.api_key): + type = EveApiManager.check_api_is_type_account(api_key_pair.api_id, api_key_pair.api_key) + if type == None: + api_key_pair.error_count += 1 + api_key_pair.save() + logger.info("API key %s incurred an error checking if type account. Error count is now %s" % (api_key_pair.api_id, api_key_pair.error_count)) + still_valid = None + elif type == False: logger.info("Determined api key %s for blue user %s is no longer type account as requred." % (api_key_pair.api_id, user)) still_valid = False - if not EveApiManager.check_blue_api_is_full(api_key_pair.api_id, api_key_pair.api_key): + full = EveApiManager.check_blue_api_is_full(api_key_pair.api_id, api_key_pair.api_key) + if full == None: + api_key_pair.error_count += 1 + api_key_pair.save() + logger.info("API key %s incurred an error checking if meets mask requirements. Error count is now %s" % (api_key_pair.api_id, api_key_pair.error_count)) + still_valid = None + elif full == False: logger.info("Determined api key %s for blue user %s no longer meets minimum access mask as required." % (api_key_pair.api_id, user)) still_valid = False else: if settings.MEMBER_API_ACCOUNT: - if not EveApiManager.check_api_is_type_account(api_key_pair.api_id, api_key_pair.api_key): + type = EveApiManager.check_api_is_type_account(api_key_pair.api_id, api_key_pair.api_key) + if type == None: + api_key_pair.error_count += 1 + api_key_pair.save() + logger.info("API key %s incurred an error checking if type account. Error count is now %s" % (api_key_pair.api_id, api_key_pair.error_count)) + still_valid = None + elif type == False: logger.info("Determined api key %s for user %s is no longer type account as required." % (api_key_pair.api_id, user)) still_valid = False - if not EveApiManager.check_api_is_full(api_key_pair.api_id, api_key_pair.api_key): + full = EveApiManager.check_api_is_full(api_key_pair.api_id, api_key_pair.api_key) + if full == None: + api_key_pair.error_count += 1 + api_key_pair.save() + logger.info("API key %s incurred an error checking if meets mask requirements. Error count is now %s" % (api_key_pair.api_id, api_key_pair.error_count)) + still_valid = None + elif full == False: logger.info("Determined api key %s for user %s no longer meets minimum access mask as required." % (api_key_pair.api_id, user)) still_valid = False - if still_valid is not True: + if still_valid == None: + if api_key_pair.error_count >= 3: + logger.info("API key %s has incurred 3 or more errors. Assuming invalid." % api_key_pair.api_id) + still_valid = False + if still_valid == False: logger.debug("API key %s has failed validation; it and its characters will be deleted." % api_key_pair.api_id) EveManager.delete_characters_by_api_id(api_key_pair.api_id, user.id) EveManager.delete_api_key_pair(api_key_pair.api_id, user.id) else: + if api_key_pair.error_count != 0: + logger.info("Clearing error count for api %s as it passed validation" % api_key_pair.api_id) + api_key_pair.error_count = 0 + api_key_pair.save() logger.info("Determined api key %s still meets requirements." % api_key_pair.api_id) # Update characters characters = EveApiManager.get_characters_from_api(api_key_pair.api_id, diff --git a/eveonline/models.py b/eveonline/models.py index 564b91ef..f55b4b11 100644 --- a/eveonline/models.py +++ b/eveonline/models.py @@ -21,6 +21,7 @@ class EveApiKeyPair(models.Model): api_id = models.CharField(max_length=254) api_key = models.CharField(max_length=254) user = models.ForeignKey(User) + error_count = models.PositiveIntegerField(default=0) def __str__(self): return self.user.username + " - ApiKeyPair" @@ -47,4 +48,4 @@ class EveCorporationInfo(models.Model): alliance = models.ForeignKey(EveAllianceInfo, blank=True, null=True) def __str__(self): - return self.corporation_name \ No newline at end of file + return self.corporation_name diff --git a/services/managers/eve_api_manager.py b/services/managers/eve_api_manager.py index 8df6f6f7..ce5650c4 100644 --- a/services/managers/eve_api_manager.py +++ b/services/managers/eve_api_manager.py @@ -83,8 +83,7 @@ class EveApiManager(): except evelink.api.APIError as error: logger.exception("Unhandled APIError occured.", exc_info=True) - - return False + return None @staticmethod @@ -99,8 +98,7 @@ class EveApiManager(): except evelink.api.APIError as error: logger.exception("Unhandled APIError occured.", exc_info=True) - - return False + return None @staticmethod def check_blue_api_is_full(api_id, api_key): @@ -114,6 +112,7 @@ class EveApiManager(): except evelink.api.APIError as error: logger.exception("Unhandled APIError occured.", exc_info=True) + return None @staticmethod @@ -128,8 +127,7 @@ class EveApiManager(): except evelink.api.APIError as error: logger.exception("Unhandled APIError occured.", exc_info=True) - - return False + return None @staticmethod def api_key_is_valid(api_id, api_key):