]> git.mxchange.org Git - fba.git/blobdiff - fba/instances.py
WIP(?):
[fba.git] / fba / instances.py
index e002fb3969302bddafd6f52877413cc2d5da0bc6..7e3d2012949f18fbef5c0c5da28842693493fd71 100644 (file)
 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
 
 import json
-import requests
 import sys
 import time
+
+import requests
 import validators
 
 from fba import blacklist
 from fba import cache
 from fba import fba
+from fba import federation
 
 # Found info from node, such as nodeinfo URL, detection mode that needs to be
 # written to database. Both arrays must be filled at the same time or else
@@ -49,7 +51,7 @@ _pending = {
     "last_error_details" : {},
 }
 
-def set(key: str, domain: str, value: any):
+def set_data(key: str, domain: str, value: any):
     # DEBUG: print(f"DEBUG: key='{key}',domain='{domain}',value[]='{type(value)}' - CALLED!")
     if not isinstance(key, str):
         raise ValueError("Parameter key[]='{type(key)}' is not 'str'")
@@ -101,9 +103,9 @@ def update_data(domain: str):
     for key in _pending:
         # DEBUG: print("DEBUG: key:", key)
         if domain in _pending[key]:
-           # DEBUG: print(f"DEBUG: Adding '{_pending[key][domain]}' for key='{key}' ...")
-           fields.append(_pending[key][domain])
-           sql_string += f" {key} = ?,"
+            # DEBUG: print(f"DEBUG: Adding '{_pending[key][domain]}' for key='{key}' ...")
+            fields.append(_pending[key][domain])
+            sql_string += f" {key} = ?,"
 
     fields.append(time.time())
     fields.append(domain)
@@ -149,7 +151,7 @@ def update_last_instance_fetch(domain: str):
         raise ValueError("Parameter 'domain' is empty")
 
     # DEBUG: print("DEBUG: Updating last_instance_fetch for domain:", domain)
-    set("last_instance_fetch", domain, time.time())
+    set_data("last_instance_fetch", domain, time.time())
 
     # Running pending updated
     # DEBUG: print(f"DEBUG: Invoking update_data({domain}) ...")
@@ -164,7 +166,7 @@ def update_last_blocked(domain: str):
         raise ValueError("Parameter 'domain' is empty")
 
     # DEBUG: print("DEBUG: Updating last_blocked for domain", domain)
-    set("last_blocked", domain, time.time())
+    set_data("last_blocked", domain, time.time())
 
     # Running pending updated
     # DEBUG: print(f"DEBUG: Invoking update_data({domain}) ...")
@@ -172,8 +174,8 @@ def update_last_blocked(domain: str):
 
     # DEBUG: print("DEBUG: EXIT!")
 
-def add(domain: str, origin: str, originator: str, path: str = None):
-    # DEBUG: print(f"DEBUG: domain='{domain}',origin='{origin}',originator='{originator}',path='{path}' - CALLED!")
+def add(domain: str, origin: str, command: str, path: str = None):
+    # DEBUG: print(f"DEBUG: domain='{domain}',origin='{origin}',command='{command}',path='{path}' - CALLED!")
     if not isinstance(domain, str):
         raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
     elif domain == "":
@@ -182,10 +184,10 @@ def add(domain: str, origin: str, originator: str, path: str = None):
         raise ValueError(f"origin[]={type(origin)} is not 'str'")
     elif origin == "":
         raise ValueError("Parameter 'origin' is empty")
-    elif not isinstance(originator, str):
-        raise ValueError(f"originator[]={type(originator)} is not 'str'")
-    elif originator == "":
-        raise ValueError("Parameter 'originator' is empty")
+    elif not isinstance(command, str):
+        raise ValueError(f"command[]={type(command)} is not 'str'")
+    elif command == "":
+        raise ValueError("Parameter 'command' is empty")
     elif not validators.domain(domain.split("/")[0]):
         raise ValueError(f"Bad domain name='{domain}'")
     elif origin is not None and not validators.domain(origin.split("/")[0]):
@@ -195,10 +197,11 @@ def add(domain: str, origin: str, originator: str, path: str = None):
     elif domain.find("/profile/") > 0 or domain.find("/users/") > 0:
         raise Exception(f"domain='{domain}' is a single user")
 
-    # DEBUG: print("DEBUG: domain,origin,originator,path:", domain, origin, originator, path)
-    software = fba.determine_software(domain, path)
+    # DEBUG: print("DEBUG: domain,origin,command,path:", domain, origin, command, path)
+    software = federation.determine_software(domain, path)
+
     # DEBUG: print("DEBUG: Determined software:", software)
-    if domain.find("/c/") > 0 and software == "lemmy":
+    if software == "lemmy" and domain.find("/c/") > 0:
         domain = domain.split("/c/")[0]
         if is_registered(domain):
             print(f"WARNING: domain='{domain}' already registered after cutting off user part. - EXIT!")
@@ -207,11 +210,11 @@ def add(domain: str, origin: str, originator: str, path: str = None):
     print(f"INFO: Adding instance domain='{domain}' (origin='{origin}',software='{software}')")
     try:
         fba.cursor.execute(
-            "INSERT INTO instances (domain, origin, originator, hash, software, first_seen) VALUES (?, ?, ?, ?, ?, ?)",
+            "INSERT INTO instances (domain, origin, command, hash, software, first_seen) VALUES (?, ?, ?, ?, ?, ?)",
             (
                domain,
                origin,
-               originator,
+               command,
                fba.get_hash(domain),
                software,
                time.time()
@@ -222,19 +225,13 @@ def add(domain: str, origin: str, originator: str, path: str = None):
 
         if has_pending_instance_data(domain):
             # DEBUG: print(f"DEBUG: domain='{domain}' has pending nodeinfo being updated ...")
-            set("last_status_code"  , domain, None)
-            set("last_error_details", domain, 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)
@@ -249,8 +246,8 @@ def update_last_nodeinfo(domain: str):
         raise ValueError("Parameter 'domain' is empty")
 
     # DEBUG: print("DEBUG: Updating last_nodeinfo for domain:", domain)
-    set("last_nodeinfo", domain, time.time())
-    set("last_updated" , domain, time.time())
+    set_data("last_nodeinfo", domain, time.time())
+    set_data("last_updated" , domain, time.time())
 
     # Running pending updated
     # DEBUG: print(f"DEBUG: Invoking update_data({domain}) ...")
@@ -258,32 +255,36 @@ def update_last_nodeinfo(domain: str):
 
     # DEBUG: print("DEBUG: EXIT!")
 
-def update_last_error(domain: str, response: requests.models.Response):
-    # DEBUG: print("DEBUG: domain,response[]:", domain, type(response))
+def update_last_error(domain: str, error: dict):
+    # DEBUG: print("DEBUG: domain,error[]:", domain, type(error))
     if not isinstance(domain, str):
         raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
     elif domain == "":
         raise ValueError("Parameter 'domain' is empty")
 
-    # DEBUG: print("DEBUG: BEFORE response[]:", type(response))
-    if isinstance(response, BaseException) or isinstance(response, json.decoder.JSONDecodeError):
-        response = f"{type}:str(response)"
-
-    # DEBUG: print("DEBUG: AFTER response[]:", type(response))
-    if isinstance(response, str):
-        # DEBUG: print(f"DEBUG: Setting last_error_details='{response}'");
-        set("last_status_code"  , domain, 999)
-        set("last_error_details", domain, response)
+    # DEBUG: print("DEBUG: BEFORE error[]:", type(error))
+    if isinstance(error, BaseException) or isinstance(error, json.decoder.JSONDecodeError):
+        error = f"error[{type(error)}]='{str(error)}'"
+    # DEBUG: print("DEBUG: AFTER error[]:", type(error))
+
+    if isinstance(error, str):
+        # DEBUG: print(f"DEBUG: Setting last_error_details='{error}'")
+        set_data("last_status_code"  , domain, 999)
+        set_data("last_error_details", domain, error)
+    elif isinstance(error, requests.models.Response):
+        # DEBUG: print(f"DEBUG: Setting last_error_details='{error.reason}'")
+        set_data("last_status_code"  , domain, error.status_code)
+        set_data("last_error_details", domain, error.reason)
     else:
-        # DEBUG: print(f"DEBUG: Setting last_error_details='{response.reason}'");
-        set("last_status_code"  , domain, response.status_code)
-        set("last_error_details", domain, response.reason)
+        # DEBUG: print(f"DEBUG: Setting last_error_details='{error['error_message']}'")
+        set_data("last_status_code"  , domain, error["status_code"])
+        set_data("last_error_details", domain, error["error_message"])
 
     # Running pending updated
     # DEBUG: print(f"DEBUG: Invoking update_data({domain}) ...")
     update_data(domain)
 
-    fba.log_error(domain, response)
+    fba.log_error(domain, error)
 
     # DEBUG: print("DEBUG: EXIT!")
 
@@ -296,15 +297,15 @@ def is_registered(domain: str) -> bool:
 
     # DEBUG: print(f"DEBUG: domain='{domain}' - CALLED!")
     if not cache.key_exists("is_registered"):
-        # DEBUG: print(f"DEBUG: Cache for 'is_registered' not initialized, fetching all rows ...")
+        # DEBUG: print("DEBUG: Cache for 'is_registered' not initialized, fetching all rows ...")
         try:
             fba.cursor.execute("SELECT domain FROM instances")
 
             # 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)