proxy: Watchdog

This commit is contained in:
Pascal Engélibert 2022-12-09 00:31:42 +01:00
parent f530c72014
commit 1f0271099e
Signed by: tuxmain
GPG Key ID: 3504BC6D362F7DCA
1 changed files with 23 additions and 1 deletions

View File

@ -1,4 +1,4 @@
import http.client, re, socket, sys, time, _thread
import http.client, re, socket, sys, time, _thread, threading, os
RECBUF = 1024
@ -108,6 +108,13 @@ def handle(client):
client.close()
def run_watchdog(service_name, timeout):
while True:
if time.time() > last_request + timeout:
os.system("systemctl restart {}".format(service_name))
last_request = time.time()
time.sleep(60)
if __name__ == "__main__":
if "--help" in sys.argv or "-h" in sys.argv:
print("""MineTest Matrix Bridge mod proxy
@ -122,6 +129,10 @@ Options: (with defaults)
-A matrix.txmn.tk Matrix address (without protocol and port)
-P 8448 Matrix port
-s Use HTTP instead of HTTPS (default: no)
-w <service-name> Enable watchdog
Restarts the given systemd service when no request from
the minetest server after a given time. Needs root.
--wtime 600 Watchdog timeout (seconds)
""")
exit()
@ -130,6 +141,14 @@ Options: (with defaults)
matrix_addr = getargv("-A", "matrix.txmn.tk")
matrix_port = int(getargv("-P", 8448))
matrix_https = not "-s" in sys.argv
watchdog = getargv("-w", None)
watchdog_timeout = int(getargv("--wtime", 600))
last_request = time.time()
if watchdog:
t = threading.Thread(target=run_watchdog, args=(watchdog, watchdog_timeout,))
t.start()
if matrix_https:
import ssl
@ -146,8 +165,11 @@ Options: (with defaults)
try:
client, addr = sock.accept()
except socket.timeout:
last_request = time.time()
continue
last_request = time.time()
_thread.start_new_thread(handle, (client,))
sock.close()
except KeyboardInterrupt: