-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatcher.py
56 lines (51 loc) · 1.42 KB
/
watcher.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
import subprocess
import time
import sys
f = open("watcher.txt", "a")
f.write("\n\n\n\n\n\n\n\n")
f.flush()
f.close()
if len(sys.argv) >= 2:
codespace = sys.argv[1]
def printf(*data: object, end: str = "\n"):
f = open("watcher.txt", "a")
f.write(" ".join([str(x) for x in data]))
f.write(end)
f.flush()
f.close()
else:
codespace = input("Enter the codespace name\n")
def printf(*data: object, end: str = "\n"):
print(*data, end=end)
def start_server():
global server
try:
server.send_signal(2)
server.wait()
except NameError: pass
server = subprocess.Popen(["python3", "main.py"])
time.sleep(1)
subprocess.run(["gh", "codespace", "ports", "visibility", "12344:public", "--codespace", codespace])
return server
server = start_server()
while True:
p = subprocess.Popen(["curl", "-s", "-o", "/dev/null", "-w", "%{http_code}", f"https://{codespace}-12344.app.github.dev/home.html"], stdout=subprocess.PIPE)
time.sleep(1)
if p.poll() == None or server.poll() != None:
p.kill()
printf("\nServer is not responding!")
server.send_signal(2)
server.wait()
printf("Killed the server!")
time.sleep(3)
start_server()
printf("Restarted the server.")
elif p.stdout == None:
printf("DANGER! Process is not reporting standard out!")
else:
out = int(p.stdout.read().decode("UTF-8"))
if out == 200:
printf(f"[fine 200]", end="")
else:
printf(f"\npossible problem! status code: {out}")
sys.stdout.flush()