forked from Kav-K/GPTDiscord
-
Notifications
You must be signed in to change notification settings - Fork 1
/
auto_restarter.py
65 lines (53 loc) · 1.8 KB
/
auto_restarter.py
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
import requests
import time
import subprocess
from typing import Optional
def check_health(url: str, threshold: int, interval: int) -> bool:
"""
Check the health of a URL. Returns True if the service is healthy,
False otherwise.
"""
try:
response = requests.get(url)
if response.status_code == 200:
data = response.json()
return data.get("status") == "ok"
except requests.RequestException:
pass
return False
def restart_service():
"""
Restarts the service by running a series of system commands.
"""
commands = [
"cd /home/GPTDiscord",
"kill -9 $(cat bot.pid)",
"rm bot.pid",
"screen -dmS GPTBot python3.9 gpt3discord.py",
]
full_command = "; ".join(commands)
subprocess.run(full_command, shell=True, check=True)
def monitor_service(url: str, threshold: int = 3, interval: int = 30):
"""
Monitors a service at the given URL, restarting it if it fails
the health check consecutively for a given threshold number of times.
"""
failure_count = 0
while True:
if not check_health(url, threshold, interval):
failure_count += 1
print(f"Health check failed {failure_count} times")
else:
failure_count = 0
if failure_count >= threshold:
print(f"Restarting service after {failure_count} consecutive failures.")
try:
restart_service()
except subprocess.SubprocessError as e:
print(f"Error restarting service: {e}")
finally:
failure_count = 0
time.sleep(interval)
if __name__ == "__main__":
# Adjust the threshold and interval as needed
monitor_service("http://localhost:8181/healthz", threshold=30, interval=10)