forked from thomas-xin/Miza
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
81 lines (73 loc) · 2.08 KB
/
main.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import os, time, datetime, traceback, psutil
python = ("python3", "python")[os.name == "nt"]
try:
os.system("color")
except:
print(traceback.format_exc())
def delete(f):
while f in os.listdir():
try:
os.remove(f)
break
except:
print(traceback.format_exc())
time.sleep(1)
sd = "shutdown.json"
rs = "restart.json"
hb = "heartbeat.json"
delete(sd)
delete(rs)
delete(hb)
while not sd in os.listdir():
delete(rs)
delete(hb)
proc = psutil.Popen([python, "bot.py"], shell=True)
print("Bot started with PID \033[1;34;40m" + str(proc.pid) + "\033[1;37;40m.")
time.sleep(8)
try:
print("\033[1;32;40mHeartbeat started\033[1;37;40m.")
alive = True
while alive:
f = open(hb, "wb")
f.close()
print(
"\033[1;36;40m Heartbeat at "
+ str(datetime.datetime.now())
+ "\033[1;37;40m."
)
for i in range(16):
time.sleep(0.5)
ld = os.listdir()
if rs in ld or sd in ld:
alive = False
break
if not alive or hb in os.listdir():
alive = False
break
found = True
while found:
found = False
try:
for child in proc.children():
child.kill()
found = True
except psutil.NoSuchProcess:
break
while True:
try:
proc.kill()
except psutil.NoSuchProcess:
break
if sd in os.listdir():
break
print("\033[1;31;40mBot closed without shutdown signal, restarting...\033[1;37;40m")
except KeyboardInterrupt:
raise
except:
print(traceback.format_exc())
time.sleep(0.5)
delete(hb)
delete(rs)
delete(sd)
print("Shutdown signal confirmed. Press [ENTER] to close.")
input()