Continued:
authorRoland Häder <roland@mxchange.org>
Mon, 29 May 2023 18:08:22 +0000 (20:08 +0200)
committerRoland Häder <roland@mxchange.org>
Mon, 29 May 2023 18:16:47 +0000 (20:16 +0200)
- introduced error_log table and corresponding log_error() function
- added error_log_cleanup for cleaning up old records (default: 7 days)

blocks_empty.db
config.defaults.json
fba.py

index d71854123e51723ffba2036f461511f2699b2a55..1854e2ec34f86c7f766076dd243a424f0d3854e2 100644 (file)
Binary files a/blocks_empty.db and b/blocks_empty.db differ
index 9ab743bec9e8921181e2b5146675477094797641..59d37ae2042830d52c73d387d8dc2e87f2adece6 100644 (file)
@@ -13,5 +13,6 @@
     "slogan"            : "### Your footer slogan ###",
     "recheck_instance"  : 43200,
     "recheck_block"     : 43200,
-    "misskey_offset"    : 100
+    "misskey_offset"    : 100,
+    "error_log_cleanup" : 604800
 }
diff --git a/fba.py b/fba.py
index 3c62ba7c53f44c591b147421a58dfb7809d40777..733809ae5df61dd76f3480e033c9cb025a98d89e 100644 (file)
--- a/fba.py
+++ b/fba.py
@@ -370,6 +370,37 @@ def update_nodeinfos(domain: str):
 
     # DEBUG: print("DEBUG: EXIT!")
 
+def log_error(domain: str, res: any):
+    # DEBUG: print("DEBUG: domain,res[]:", domain, type(res))
+    try:
+        # DEBUG: print("DEBUG: BEFORE res[]:", type(res))
+        if isinstance(res, BaseException) or isinstance(res, json.JSONDecodeError):
+            res = str(res)
+
+        # DEBUG: print("DEBUG: AFTER res[]:", type(res))
+        if type(res) is str:
+            cursor.execute("INSERT INTO error_log (domain, error_code, error_message, created) VALUES (?, 999, ?, ?)",[
+                domain,
+                res,
+                time.time()
+            ])
+        else:
+            cursor.execute("INSERT INTO error_log (domain, error_code, error_message, created) VALUES (?, ?, ?, ?)",[
+                domain,
+                res.status_code,
+                res.reason,
+                time.time()
+            ])
+
+        # Cleanup old entries
+        # DEBUG: print(f"DEBUG: Purging old records (distance: {config['error_log_cleanup'])")
+        cursor.execute("DELETE FROM error_log WHERE created < ?", [time.time() - config["error_log_cleanup"]])
+    except BaseException as e:
+        print(f"ERROR: failed SQL query: domain='{domain}',exception[{type(e)}]:'{str(e)}'")
+        sys.exit(255)
+
+    # DEBUG: print("DEBUG: EXIT!")
+
 def update_last_error(domain: str, res: any):
     # DEBUG: print("DEBUG: domain,res[]:", domain, type(res))
     try:
@@ -398,6 +429,8 @@ def update_last_error(domain: str, res: any):
             # DEBUG: print("DEBUG: Did not update any rows:", domain)
             pending_errors[domain] = res
 
+        log_error(domain, res)
+
     except BaseException as e:
         print(f"ERROR: failed SQL query: domain='{domain}',exception[{type(e)}]:'{str(e)}'")
         sys.exit(255)