proxy: Watchdog
This commit is contained in:
parent
f530c72014
commit
1f0271099e
24
proxy.py
24
proxy.py
|
@ -1,4 +1,4 @@
|
||||||
import http.client, re, socket, sys, time, _thread
|
import http.client, re, socket, sys, time, _thread, threading, os
|
||||||
|
|
||||||
RECBUF = 1024
|
RECBUF = 1024
|
||||||
|
|
||||||
|
@ -108,6 +108,13 @@ def handle(client):
|
||||||
|
|
||||||
client.close()
|
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 __name__ == "__main__":
|
||||||
if "--help" in sys.argv or "-h" in sys.argv:
|
if "--help" in sys.argv or "-h" in sys.argv:
|
||||||
print("""MineTest Matrix Bridge mod proxy
|
print("""MineTest Matrix Bridge mod proxy
|
||||||
|
@ -122,6 +129,10 @@ Options: (with defaults)
|
||||||
-A matrix.txmn.tk Matrix address (without protocol and port)
|
-A matrix.txmn.tk Matrix address (without protocol and port)
|
||||||
-P 8448 Matrix port
|
-P 8448 Matrix port
|
||||||
-s Use HTTP instead of HTTPS (default: no)
|
-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()
|
exit()
|
||||||
|
|
||||||
|
@ -130,6 +141,14 @@ Options: (with defaults)
|
||||||
matrix_addr = getargv("-A", "matrix.txmn.tk")
|
matrix_addr = getargv("-A", "matrix.txmn.tk")
|
||||||
matrix_port = int(getargv("-P", 8448))
|
matrix_port = int(getargv("-P", 8448))
|
||||||
matrix_https = not "-s" in sys.argv
|
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:
|
if matrix_https:
|
||||||
import ssl
|
import ssl
|
||||||
|
@ -146,8 +165,11 @@ Options: (with defaults)
|
||||||
try:
|
try:
|
||||||
client, addr = sock.accept()
|
client, addr = sock.accept()
|
||||||
except socket.timeout:
|
except socket.timeout:
|
||||||
|
last_request = time.time()
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
last_request = time.time()
|
||||||
|
|
||||||
_thread.start_new_thread(handle, (client,))
|
_thread.start_new_thread(handle, (client,))
|
||||||
sock.close()
|
sock.close()
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
|
|
Loading…
Reference in New Issue