From: Roland Häder Date: Mon, 29 May 2023 18:08:22 +0000 (+0200) Subject: Continued: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=6716ed0d07692150a23b83da64530d3da1b8d519;p=fba.git Continued: - introduced error_log table and corresponding log_error() function - added error_log_cleanup for cleaning up old records (default: 7 days) --- diff --git a/blocks_empty.db b/blocks_empty.db index d718541..1854e2e 100644 Binary files a/blocks_empty.db and b/blocks_empty.db differ diff --git a/config.defaults.json b/config.defaults.json index 9ab743b..59d37ae 100644 --- a/config.defaults.json +++ b/config.defaults.json @@ -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 3c62ba7..733809a 100644 --- 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)