forked from odysseusmax/tg-index
-
Notifications
You must be signed in to change notification settings - Fork 18
/
run.py
51 lines (44 loc) · 1.31 KB
/
run.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
import os
import runpy
import requests
import time
import psutil
from dotenv import load_dotenv
load_dotenv()
APP_CMD = "python3 -m app"
def find_process_cmd(cmdline):
for proc in psutil.process_iter():
is_running = cmdline in proc.cmdline()
if(is_running):
return True
def is_alive():
repl_slug = os.environ.get("REPL_SLUG")
repl_owner = os.environ.get("REPL_OWNER")
repl_url = f"https://{repl_slug}.{repl_owner}.repl.co"
repl_url_local = "http://0.0.0.0:8080"
try:
resp = requests.get(repl_url_local, timeout=60)
except Exception as e:
print("Server response wait timed out!")
return False
return resp.ok
def run_safe():
"prevent session string from expiring due to two instances running"
try:
if not is_alive():
kill_server()
# proc = runpy.run_module('app', alter_sys=True)
if(!find_process_cmd(APP_CMD)):
# prevent running app if it is already running
print("Starting a new instance...")
os.system(APP_CMD)
else:
print("Server is already running...")
except Exception as e:
os.system("python app/generate_session_string.py")
def kill_server():
print("Killing server just incase it is not responding...")
kill_server_cmd = f"pkill -9 -f '{APP_CMD}'"
os.system(kill_server_cmd)
time.sleep(30)
run_safe()