-
Notifications
You must be signed in to change notification settings - Fork 0
/
teleword.py
86 lines (72 loc) · 2.78 KB
/
teleword.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
82
83
84
85
86
import multiprocessing
import sys
import logging
import subprocess
import time
import requests
from config.config import PYTHON_PATH, ADMIN_API_KEY
from app.db_worker import is_connection_alive
########################################################################################################################
logger = logging.getLogger(__name__)
logging.basicConfig(
level=logging.INFO,
stream=sys.stdout,
format='[%(asctime)s]:[%(levelname)s]:[%(filename)s]:[%(lineno)d]: %(message)s',
)
########################################################################################################################
def status_checker(processes: list):
"""
Resurrects all processes from the processes list
:param processes: List of multiprocessing.Process processes
"""
time.sleep(600)
h = 0
while True:
for process in processes:
if not process.is_alive():
logger.warning(f'[{process.name}] Process {process.pid} is dead. I am trying to resurrect')
try:
process.start()
except Exception as e:
logger.error(f'[{process.name}] Failed to reanimate the process {process.pid} {e}. Going to sleep')
if not is_connection_alive():
for process in processes:
process.stop()
process.join()
process.start()
try:
r = requests.get(f'http://localhost/api/lesson/{ADMIN_API_KEY}')
if r.status_code != 200:
logger.error(f'[{h}] Failed to make request \n\n{r.status_code, r.json(), r.text}\n\n I am trying to resurrect...')
raise ConnectionError
except Exception as e:
logger.error(f'[{h}] Failed to make request \n\n{e}\n\n I am trying to resurrect...')
for process in processes:
process.stop()
process.join()
process.start()
h += 1
time.sleep(600)
########################################################################################################################
api_process = multiprocessing.Process(
target=subprocess.run,
kwargs={
'args': f'{PYTHON_PATH} api.py',
'shell': True
},
name="api_process"
)
bot_process = multiprocessing.Process(
target=subprocess.run,
kwargs={
'args': f'{PYTHON_PATH} bot.py',
'shell': True,
},
name="bot_process"
)
########################################################################################################################
if __name__ == '__main__': # Runs an entire two-part application by two different processes
logger.info(": Starting teleword")
api_process.start()
bot_process.start()
status_checker([api_process, bot_process])