summaryrefslogtreecommitdiff
path: root/logmaster.py
blob: 90d149ab0fc1e278522a9a075221d7c1620bba48 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#!/usr/bin/python3
########################################################################################
#     This file is part of Moubootaur Legends API.
#     Copyright (C) 2019-2022  Jesusalva

#     This library is free software; you can redistribute it and/or
#     modify it under the terms of the GNU Lesser General Public
#     License as published by the Free Software Foundation; either
#     version 2.1 of the License, or (at your option) any later version.

#     This library is distributed in the hope that it will be useful,
#     but WITHOUT ANY WARRANTY; without even the implied warranty of
#     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#     Lesser General Public License for more details.

#     You should have received a copy of the GNU Lesser General Public
#     License along with this library; if not, write to the Free Software
#     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
########################################################################################
# This is the log master, to improve performance

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

## Warnings
ERR=0
WRN=1
DBG=2
def stdout(mes, code=DBG):
    if code == ERR:
        color="31;1"
        title="ERROR"
    elif code == WRN:
        color="33;1"
        title="WARNING"
    else:
        color="32;1"
        title="INFO"
    print("\033[%sm[%s]\033[0m %s" % (color, title, mes))
    return

## Rudimentary parser
with open("conf/import/sql_connection.conf", "r") as f:
    for l in f:
        r=l.replace("\"", "").replace(" ", "").replace("\t", "").replace("\n", "").replace("\r", "").split(":")
        try:
            if r[0] == "db_hostname":
                HOST=str(r[1])
            elif r[0] == "db_port":
                PORT=int(r[1])
            elif r[0] == "db_username":
                USER=str(r[1])
            elif r[0] == "db_password":
                PASS=str(r[1])
            elif r[0] == "db_database":
                DBXT=str(r[1])
            else:
                pass
        except:
            traceback.print_exc()

## Check for fails
if USER == "":
    stdout("Lacking user! Check conf/import/sql_connection.conf", ERR)
    exit(1)
if PASS == "":
    stdout("Lacking password! Check conf/import/sql_connection.conf", ERR)
    exit(1)
if DBXT == "":
    stdout("Lacking database! Check conf/import/sql_connection.conf", ERR)
    exit(1)

## Init the database
def connect():
    global db, HOST, USER, PASS, DBXT
    stdout("Connecting to %s:%d (%s @ %s)" % (HOST, PORT, USER, DBXT))
    db = mysql.connector.connect(
          host=HOST,
          port=str(PORT),
          user=USER,
          passwd=PASS,
          database=DBXT
        )
    return

## Function to keep a database alive
def keep_alive():
    global db
    try:
        db.ping(reconnect=True, attempts=10, delay=1)
    except:
        # SQL error
        stdout("keep_alive: INTERNAL ERROR (ping timeout!)", ERR)
        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
def EXIT_NOW(sn, frame):
    global running, db
    stdout("Exit Signal received!", ERR)
    running = False
    time.sleep(SQL_FLUSH)
    db.close()
    stdout("Terminated", WRN)
    return

## Try to close Database and finish safely
#signal.signal(signal.SIGTERM, EXIT_NOW)
signal.signal(signal.SIGABRT, EXIT_NOW)

## Create the SQL connection and keep it alive
time.sleep(1.0)
connect()
keep_alive_runner()

## Watch for stdin
runner=threading.Thread(target=run_forever, daemon=True)
runner.start()

## Handle the input
stdout("Logmaster started", WRN)
bf=""
while running:
    try:
        ## We have stuff to push
        if len(sqli) > 0:
            w = db.cursor()
            for com in list(sqli):
                try:
                    cmd=com.split("→")[0]
                    args=com.replace("%s→" % cmd, "")

                    ## Command: SQL
                    ## Description: Prepares a SQL statement. No escapping.
                    ## Supports "?1", "?2" etc. for use with SAD
                    if cmd == "SQL":
                        bf=str(args)
                    ## Command: SAD
                    ## Description: Replaces "?" with escaped data.
                    elif cmd.startswith("SAD"):
                        bf=bf.replace("?%s" % cmd.replace("SAD", ""), args.replace("\\", "\\\\").replace('"','\\"').replace("'", "\\'").replace('\n','').replace('\r','').replace('\0',''))
                    ## Command: SQLRUN
                    ## Description: Executes the prepared SQL statement.
                    ## Sanitization must be done using SAD commands.
                    elif cmd == "SQLRUN":
                        w.execute(bf)
                        #stdout("Query OK: %s" % bf)
                        bf=""
                    ## Command: DISCORDID
                    ## Description: Replaces "?" with the Discord ID
                    ## For the associated argument.
                    ## Requires the API (WIP)
                    elif cmd == "DISCORDID":
                        ## FIXME: Query the API
                        bf=bf.replace("?%s" % cmd.replace("DISCORDID", ""), "")
                        pass
                    ## Command: PING
                    ## Description: Does nothing
                    elif cmd == "PING":
                        pass
                    ## TODO: Integration with the API
                    else:
                        stdout("Unrecognized command: %s" % cmd, ERR)
                except:
                    stdout("Statement failed: %s" % cmd, ERR)
                    traceback.print_exc()
                sqli.remove(com)
            db.commit()
            w.close()
    except KeyboardInterrupt:
        running=False
        stdout("Shutdown in progress!")
        break
    except:
        traceback.print_exc()

    ## No need to flush ALL the time
    if running:
        time.sleep(SQL_FLUSH)

db.close()
stdout("Logmaster finished.")