]> git.mxchange.org Git - fba.git/commitdiff
Continued:
authorRoland Häder <roland@mxchange.org>
Fri, 9 Jun 2023 21:29:18 +0000 (23:29 +0200)
committerRoland Häder <roland@mxchange.org>
Fri, 9 Jun 2023 21:29:18 +0000 (23:29 +0200)
- removed remains of code around pending_errors dictionary
- rewrote it towards instances.update_last_error()
- also rewrote/removed some try/except blocks

fba/fba.py
fba/federation.py
fba/instances.py
fba/network.py

index 1dada8cbf21ab8c0607b4eed5554b0b52b7e26a5..ab413e6361a58ac50e5654beca6ea87b147f1f86 100644 (file)
@@ -27,10 +27,6 @@ import requests
 from fba import config
 from fba import network
 
-# Array with pending errors needed to be written to database
-pending_errors = {
-}
-
 # Connect to database
 connection = sqlite3.connect("blocks.db")
 cursor = connection.cursor()
@@ -173,21 +169,6 @@ def strip_until(software: str, until: str) -> str:
     # DEBUG: print(f"DEBUG: software='{software}' - EXIT!")
     return software
 
-def remove_pending_error(domain: str):
-    if not isinstance(domain, str):
-        raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
-    elif domain == "":
-        raise ValueError("Parameter 'domain' is empty")
-
-    try:
-        # Prevent updating any pending errors, nodeinfo was found
-        del pending_errors[domain]
-
-    except:
-        pass
-
-    # DEBUG: print("DEBUG: EXIT!")
-
 def get_hash(domain: str) -> str:
     if not isinstance(domain, str):
         raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
@@ -206,32 +187,28 @@ def log_error(domain: str, response: requests.models.Response):
         # DEBUG: print(f"DEBUG: Writing to error_log is disabled in configuruation file - EXIT!")
         return
 
-    try:
-        # DEBUG: print("DEBUG: BEFORE response[]:", type(response))
-        if isinstance(response, BaseException) or isinstance(response, json.decoder.JSONDecodeError):
-            response = f"response[{type(response)}]='{str(response)}'"
-
-        # DEBUG: print("DEBUG: AFTER response[]:", type(response))
-        if isinstance(response, str):
-            cursor.execute("INSERT INTO error_log (domain, error_code, error_message, created) VALUES (?, 999, ?, ?)",[
-                domain,
-                response,
-                time.time()
-            ])
-        else:
-            cursor.execute("INSERT INTO error_log (domain, error_code, error_message, created) VALUES (?, ?, ?, ?)",[
-                domain,
-                response.status_code,
-                response.reason,
-                time.time()
-            ])
-
-        # Cleanup old entries
-        # DEBUG: print(f"DEBUG: Purging old records (distance: {config.get('error_log_cleanup')})")
-        cursor.execute("DELETE FROM error_log WHERE created < ?", [time.time() - config.get("error_log_cleanup")])
-    except BaseException as exception:
-        print(f"ERROR: failed SQL query: domain='{domain}',exception[{type(exception)}]:'{str(exception)}'")
-        sys.exit(255)
+    # DEBUG: print("DEBUG: BEFORE response[]:", type(response))
+    if isinstance(response, BaseException) or isinstance(response, json.decoder.JSONDecodeError):
+        response = f"response[{type(response)}]='{str(response)}'"
+
+    # DEBUG: print("DEBUG: AFTER response[]:", type(response))
+    if isinstance(response, str):
+        cursor.execute("INSERT INTO error_log (domain, error_code, error_message, created) VALUES (?, 999, ?, ?)",[
+            domain,
+            response,
+            time.time()
+        ])
+    else:
+        cursor.execute("INSERT INTO error_log (domain, error_code, error_message, created) VALUES (?, ?, ?, ?)",[
+            domain,
+            response.status_code,
+            response.reason,
+            time.time()
+        ])
+
+    # Cleanup old entries
+    # DEBUG: print(f"DEBUG: Purging old records (distance: {config.get('error_log_cleanup')})")
+    cursor.execute("DELETE FROM error_log WHERE created < ?", [time.time() - config.get("error_log_cleanup")])
 
     # DEBUG: print("DEBUG: EXIT!")
 
index acc58e2d189df2a4601ec777fec22b0951404edf..fac06c08a976fb6c548546d33747dea6b47021f9 100644 (file)
@@ -78,7 +78,7 @@ def fetch_instances(domain: str, origin: str, software: str, script: str, path:
     print(f"INFO: Checking {len(peerlist)} instances from {domain} ...")
     for instance in peerlist:
         if instance is None:
-            # Skip "None" types as tidup() cannot parse them
+            # Skip "None" types as tidup.domain() cannot parse them
             continue
 
         # DEBUG: print(f"DEBUG: instance='{instance}' - BEFORE")
@@ -96,13 +96,9 @@ def fetch_instances(domain: str, origin: str, software: str, script: str, path:
             continue
 
         # DEBUG: print("DEBUG: Handling instance:", instance)
-        try:
-            if not instances.is_registered(instance):
-                # DEBUG: print("DEBUG: Adding new instance:", instance, domain)
-                instances.add(instance, domain, script)
-        except BaseException as exception:
-            print(f"ERROR: instance='{instance}',exception[{type(exception)}]:'{str(exception)}'")
-            continue
+        if not instances.is_registered(instance):
+            # DEBUG: print("DEBUG: Adding new instance:", instance, domain)
+            instances.add(instance, domain, script)
 
     # DEBUG: print("DEBUG: EXIT!")
 
@@ -278,7 +274,7 @@ def fetch_generator_from_path(domain: str, path: str = "/") -> str:
         doc = bs4.BeautifulSoup(response.text, "html.parser")
 
         # DEBUG: print("DEBUG: doc[]:", type(doc))
-        generator = doc.find("meta", {"name": "generator"})
+        generator = doc.find("meta", {"name"    : "generator"})
         site_name = doc.find("meta", {"property": "og:site_name"})
 
         # DEBUG: print(f"DEBUG: generator='{generator}',site_name='{site_name}'")
index 6ac8bcb0ea468cc5bbd65bf924a3182f720a2a0b..a345e132147a43df3ccc12d1adb568ce78120382 100644 (file)
@@ -227,16 +227,10 @@ def add(domain: str, origin: str, command: str, path: str = None):
             set_data("last_status_code"  , domain, None)
             set_data("last_error_details", domain, None)
             update_data(domain)
-            fba.remove_pending_error(domain)
-
-        if domain in fba.pending_errors:
-            # DEBUG: print("DEBUG: domain has pending error being updated:", domain)
-            update_last_error(domain, fba.pending_errors[domain])
-            fba.remove_pending_error(domain)
 
     except BaseException as exception:
-        print(f"ERROR: failed SQL query: domain='{domain}',exception[{type(exception)}]:'{str(exception)}'")
-        sys.exit(255)
+        update_last_error(domain, exception)
+        raise Exception(f"ERROR: failed SQL query: domain='{domain}',exception[{type(exception)}]:'{str(exception)}'") from exception
     else:
         # DEBUG: print("DEBUG: Updating nodeinfo for domain:", domain)
         update_last_nodeinfo(domain)
@@ -305,8 +299,8 @@ def is_registered(domain: str) -> bool:
             # Check Set all
             cache.set_all("is_registered", fba.cursor.fetchall(), True)
         except BaseException as exception:
-            print(f"ERROR: failed SQL query: domain='{domain}',exception[{type(exception)}]:'{str(exception)}'")
-            sys.exit(255)
+            update_last_error(domain, exception)
+            raise Exception(f"ERROR: failed SQL query: domain='{domain}',exception[{type(exception)}]:'{str(exception)}'") from exception
 
     # Is cache found?
     registered = cache.sub_key_exists("is_registered", domain)
index 261d266c85c564112ec0bef99fff230b3987f272..751c7d1a0973be42b2c60fadabb376c41c35ec4d 100644 (file)
@@ -60,14 +60,16 @@ def post_json_api(domain: str, path: str, parameter: str, extra_headers: dict =
             timeout=(config.get("connection_timeout"), config.get("read_timeout"))
         )
 
-        data = json_from_response(response)
-        # DEBUG: print(f"DEBUG: response.ok={response.ok},response.status_code={response.status_code},data[]='{type(data)}'")
-        if not response.ok or response.status_code >= 400:
-            print(f"WARNING: Cannot query JSON API: domain='{domain}',path='{path}',parameter()={len(parameter)},response.status_code='{response.status_code}',data[]='{type(data)}'")
-            instances.update_last_error(domain, response)
-
-    except BaseException as exception:
-        print(f"WARNING: Some error during post(): domain='{domain}',path='{path}',parameter()={len(parameter)},exception[{type(exception)}]:'{str(exception)}'")
+    except requests.exceptions.ConnectionError as exception:
+        # DEBUG: print(f"DEBUG: Fetching '{path}' from '{domain}' failed. exception[{type(exception)}]='{str(exception)}'")
+        instances.update_last_error(domain, exception)
+        raise exception
+
+    data = json_from_response(response)
+    # DEBUG: print(f"DEBUG: response.ok={response.ok},response.status_code={response.status_code},data[]='{type(data)}'")
+    if not response.ok or response.status_code >= 400:
+        print(f"WARNING: Cannot query JSON API: domain='{domain}',path='{path}',parameter()={len(parameter)},response.status_code='{response.status_code}',data[]='{type(data)}'")
+        instances.update_last_error(domain, response)
 
     # DEBUG: print(f"DEBUG: Returning data({len(data)})=[]:{type(data)}")
     return data