diff options
Diffstat (limited to 'logmaster.py')
-rwxr-xr-x | logmaster.py | 47 |
1 files changed, 18 insertions, 29 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.") |