Bläddra i källkod

Fixing common http timeout errors

Penta 1 månad sedan
förälder
incheckning
dfe4cb7fce
2 ändrade filer med 18 tillägg och 14 borttagningar
  1. 17 14
      myanimebot/discord.py
  2. 1 0
      myanimebot/globals.py

+ 17 - 14
myanimebot/discord.py

@@ -181,20 +181,23 @@ async def background_check_feed(asyncioloop):
 
             try:
                 while stop_boucle == 0 :
-                    try:
-                        async with aiohttp.ClientSession() as httpclient:
-                            if feed_type == 1 :
-                                http_response = await httpclient.request("GET", "https://myanimelist.net/rss.php?type=rm&u=" + user.name, headers=http_headers, timeout=timeout)
-                                media = "manga"
-                            else : 
-                                http_response = await httpclient.request("GET", "https://myanimelist.net/rss.php?type=rw&u=" + user.name, headers=http_headers, timeout=timeout)
-                                media = "anime"
-                        http_data = await http_response.read()
-                    except asyncio.TimeoutError:
-                        globals.logger.error("Error while loading RSS of '{}': Timeout".format(user.name))
-                        break
-                    except Exception as e:
-                        globals.logger.exception("Error while loading RSS ({}) of '{}':\n".format(feed_type, user.name))
+                    for attempt in range(globals.MYANIMELIST_RETRY_FETCH):
+                        try:
+                            async with aiohttp.ClientSession() as httpclient:
+                                if feed_type == 1 :
+                                    http_response = await httpclient.request("GET", "https://myanimelist.net/rss.php?type=rm&u=" + user.name, headers=http_headers, timeout=timeout)
+                                    media = "manga"
+                                else : 
+                                    http_response = await httpclient.request("GET", "https://myanimelist.net/rss.php?type=rw&u=" + user.name, headers=http_headers, timeout=timeout)
+                                    media = "anime"
+
+                                http_data = await http_response.text()
+                                break
+                        except Exception as e:
+                            globals.logger.debug("Error while loading RSS ({}) of '{}':\n".format(feed_type, user.name))
+                    
+                    if http_data is None: 
+                        globals.logger.warning("RSS temporarily unreachable for '{}' after {} retries".format(user.name, globals.MYANIMELIST_RETRY_FETCH))
                         break
 
                     feeds_data = feedparser.parse(http_data)

+ 1 - 0
myanimebot/globals.py

@@ -62,6 +62,7 @@ secondMax = int(get_env_var("MYANIMEBOT_SECOND_MAX", CONFIG.getint("secondMax",
 token = get_env_var("MYANIMEBOT_TOKEN", CONFIG.get("token"))
 prefix = get_env_var("MYANIMEBOT_PREFIX", CONFIG.get("prefix", "!mab"))
 MYANIMELIST_SECONDS_BETWEEN_REQUESTS = int(get_env_var("MYANIMEBOT_MAL_REQUESTS", CONFIG.getint("myanimelist_seconds_between_requests", 2)))
+MYANIMELIST_RETRY_FETCH = int(get_env_var("MYANIMELIST_RETRY_FETCH", CONFIG.getint("myanimelist_retry_fetch", 3)))
 iconBot = get_env_var("MYANIMEBOT_ICONBOT", CONFIG.get("iconBot", "http://myanimebot.pentou.eu/rsc/bot_avatar.jpg"))
 ANILIST_SECONDS_BETWEEN_FETCHES = int(get_env_var("MYANIMEBOT_ANILIST_FETCHES", CONFIG.getint("anilist_seconds_between_fetches", 60)))
 MAL_ICON_URL = get_env_var("MYANIMEBOT_MAL_ICON", CONFIG.get("iconMAL", "https://cdn.myanimelist.net/img/sp/icon/apple-touch-icon-256.png"))