From a3e2ed7614778009d0bfc400bd6d5e2d97e734a1 Mon Sep 17 00:00:00 2001 From: Jesusalva Jesusalva Date: Sat, 27 May 2023 19:30:57 +0000 Subject: Harden logmaster and fix `@discord` Not extensively tested. --- logmaster.py | 47 ++++++++++++++++++----------------------------- npc/commands/discord.txt | 10 +++++----- 2 files changed, 23 insertions(+), 34 deletions(-) diff --git a/logmaster.py b/logmaster.py index 90d149ab0..5ace6c55e 100755 --- a/logmaster.py +++ b/logmaster.py @@ -24,7 +24,8 @@ import mysql.connector, signal, sys, threading, time, traceback ## Default values HOST="127.0.0.1"; PORT=0; USER=""; PASS=""; DBXT=""; db=None; sqli = []; running=True -SQL_PINGTIME=300.0; SQL_FLUSH=1.0 +SQL_PINGTIME=300.0; SQL_FLUSH=1.0 # <- Constants, can be tweaked +PINGED = 0.0 # <- Not a constant, Do not edit ## Warnings ERR=0 @@ -83,13 +84,19 @@ def connect(): port=str(PORT), user=USER, passwd=PASS, - database=DBXT + database=DBXT, + buffered=True ) return ## Function to keep a database alive def keep_alive(): - global db + global db, PINGED, SQL_FLUSH, SQL_PINGTIME + PINGED += SQL_FLUSH + if PINGED < SQL_PINGTIME: + return + ## We should send a ping now + PINGED = 0.0 try: db.ping(reconnect=True, attempts=10, delay=1) except: @@ -98,30 +105,6 @@ def keep_alive(): db.reconnect(attempts=12, delay=10) return -## Start keep_alive in a thread and keep it running -def keep_alive_runner(): - global db - sqlt_db1=threading.Thread(target=keep_alive, daemon=True) - sqlt_db1.start() - #################################################### - ## Run forever - time.sleep(0.05) - stall=0.05 - while sqlt_db1.is_alive(): - stdout("keep_alive: Waiting for ping") - time.sleep(2.0) - stall+=2.0 - ## Rebuild connection if the stall time exceeds the ping time - if stall > SQL_PINGTIME: - if sqlt_db1.is_alive(): - stdout("keep_alive: DBXT Connection Restarted", WRN) - connect() - break - sql_keep_alive=threading.Timer(max(1.0, SQL_PINGTIME-stall), keep_alive_runner) - sql_keep_alive.daemon=True - sql_keep_alive.start() - return - ## Read stdin for as long as possible def run_forever(): global sqli, running @@ -151,10 +134,9 @@ def EXIT_NOW(sn, frame): #signal.signal(signal.SIGTERM, EXIT_NOW) signal.signal(signal.SIGABRT, EXIT_NOW) -## Create the SQL connection and keep it alive +## Create the SQL connection time.sleep(1.0) connect() -keep_alive_runner() ## Watch for stdin runner=threading.Thread(target=run_forever, daemon=True) @@ -165,6 +147,7 @@ stdout("Logmaster started", WRN) bf="" while running: try: + w = None ## We have stuff to push if len(sqli) > 0: w = db.cursor() @@ -216,10 +199,16 @@ while running: break except: traceback.print_exc() + try: + if "close" in dir(w): + w.close() + except: + pass ## No need to flush ALL the time if running: time.sleep(SQL_FLUSH) + keep_alive() db.close() stdout("Logmaster finished.") diff --git a/npc/commands/discord.txt b/npc/commands/discord.txt index a5773af77..8ceb09e3f 100644 --- a/npc/commands/discord.txt +++ b/npc/commands/discord.txt @@ -78,9 +78,9 @@ OnCall: input .@discord$; if (.@discord$ == "") close; if (strtolower(.@discord$) == "username#0000") { break; } - .@i = explode(.@d$, .@discord$, "#"); - if (.@i != 2) { mesc l("Invalid Discord ID."); next; break; } - if (getstrlen(.@d$[1]) != 4) { mesc l("Invalid Discord ID."); next; break; } + //.@i = explode(.@d$, .@discord$, "#"); + //if (.@i != 2) { mesc l("Invalid Discord ID."); next; break; } + //if (getstrlen(.@d$[1]) != 4) { mesc l("Invalid Discord ID."); next; break; } mes ""; clear; mesc l("Linking the following Discord account:"); @@ -93,7 +93,7 @@ OnCall: if ($@HAS_API) { apiasync("SQL", sprintf("UPDATE `discord` SET `discord_name` = '?1', `verified` = '0', `discord_id` = '?2' WHERE `account_id`='%d'", getcharid(3))); apiasync("SAD1", .@discord$); - apiasync("DISCORDID2", .@discord$); + apiasync("DISCORDID", .@discord$); apiasync("SQLRUN", ""); } else { query_sql(sprintf("UPDATE `discord` SET `discord_name` = '%s', `verified` = '0', `discord_id` = '' WHERE `account_id`='%d'", @@ -103,7 +103,7 @@ OnCall: if ($@HAS_API) { apiasync("SQL", sprintf("INSERT INTO `discord` (`discord_name`, `verified`, `discord_id`, `account_id`) VALUES ('?1', '0', '?2', '%d')", getcharid(3))); apiasync("SAD1", .@discord$); - apiasync("DISCORDID2", .@discord$); + apiasync("DISCORDID", .@discord$); apiasync("SQLRUN", ""); } else { query_sql(sprintf("INSERT INTO `discord` (`discord_name`, `verified`, `discord_id`, `account_id`) VALUES ('%s', '0', '', '%d')", -- cgit v1.2.3-60-g2f50