summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJesusaves <cpntb1@ymail.com>2023-07-01 15:51:52 -0300
committerJesusaves <cpntb1@ymail.com>2023-07-01 15:51:52 -0300
commit5b43c565a1b23f01fa3f3c5153f67cad2219c90f (patch)
tree4f0410f8f9b0e219a09f9b7d78422e4412609898
parenta939ab7375c33434269c11238879bed16d1e48bd (diff)
downloadserverdata-5b43c565a1b23f01fa3f3c5153f67cad2219c90f.tar.gz
serverdata-5b43c565a1b23f01fa3f3c5153f67cad2219c90f.tar.bz2
serverdata-5b43c565a1b23f01fa3f3c5153f67cad2219c90f.tar.xz
serverdata-5b43c565a1b23f01fa3f3c5153f67cad2219c90f.zip
Rewrite logmaster daemon
-rwxr-xr-xlogmaster.py51
1 files changed, 22 insertions, 29 deletions
diff --git a/logmaster.py b/logmaster.py
index ebd92cc..5ace6c5 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,39 +105,19 @@ 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
while running:
+ try:
bf = sys.stdin.readline()
if bf is None or bf == "":
continue
#stdout("Buffer set: %s" % str(bf))
sqli.append(str(bf).replace("\n","").replace("\r",""))
+ except:
+ traceback.print_exc()
+ time.sleep(SQL_FLUSH)
return
## Handle term signals
@@ -147,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)
@@ -161,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()
@@ -212,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.")