فهرست منبع

refactor: improve logging formatting for error handling in cache and squid info fetching

Manuel 7 ماه پیش
والد
کامیت
b29360fe22
3فایلهای تغییر یافته به همراه18 افزوده شده و 5 حذف شده
  1. 4 1
      parsers/cache.py
  2. 3 1
      parsers/squid_info.py
  3. 11 3
      services/squid/fetch_data.py

+ 4 - 1
parsers/cache.py

@@ -166,7 +166,10 @@ def fetch_squid_cache_stats():
                     )
                     break
             except Exception:
-                logger.debug(f"[cache] Error decoding response on attempt {idx + 1}", exc_info=True)
+                logger.debug(
+                    f"[cache] Error decoding response on attempt {idx + 1}",
+                    exc_info=True,
+                )
                 response = b""
                 continue
 

+ 3 - 1
parsers/squid_info.py

@@ -128,7 +128,9 @@ def fetch_squid_info_stats():
         data = parts[1] if len(parts) > 1 else response_text
     except Exception as e:
         if isinstance(e, (TimeoutError, ConnectionRefusedError, socket.gaierror)):
-            logger.warning(f"Could not reach Squid at {SQUID_HOST}:{SQUID_PORT}: {type(e).__name__}: {e}")
+            logger.warning(
+                f"Could not reach Squid at {SQUID_HOST}:{SQUID_PORT}: {type(e).__name__}: {e}"
+            )
         else:
             logger.exception("Error fetching squid info stats")
         default_stats["error"] = "Error obteniendo información de Squid"

+ 11 - 3
services/squid/fetch_data.py

@@ -32,11 +32,15 @@ def get_squid_hosts() -> list[tuple[str, int]]:
                 if entry.startswith("["):
                     bracket_end = entry.rfind("]")
                     host = entry[1:bracket_end]
-                    port = int(entry[bracket_end + 2:]) if bracket_end + 2 < len(entry) else 3128
+                    port = (
+                        int(entry[bracket_end + 2 :])
+                        if bracket_end + 2 < len(entry)
+                        else 3128
+                    )
                 else:
                     last_colon = entry.rfind(":")
                     host = entry[:last_colon]
-                    port_str = entry[last_colon + 1:]
+                    port_str = entry[last_colon + 1 :]
                     try:
                         port = int(port_str)
                     except ValueError:
@@ -168,7 +172,11 @@ def fetch_all_squid_data() -> list[dict]:
     def _fetch(host: str, port: int) -> dict:
         label = f"{host}:{port}"
         raw = fetch_squid_data_from_host(host, port)
-        ok = bool(raw) and not raw.strip().lower().startswith("error") and not raw.strip().lower().startswith("[errno")
+        ok = (
+            bool(raw)
+            and not raw.strip().lower().startswith("error")
+            and not raw.strip().lower().startswith("[errno")
+        )
         return {"host": host, "port": port, "label": label, "data": raw, "ok": ok}
 
     if len(hosts) == 1: