monitor_process_config.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. import datetime
  2. import logging
  3. import os
  4. import re
  5. import sys
  6. import time
  7. import psutil
  8. sys.path.append(os.path.dirname(os.path.abspath(__file__)) + "/../")
  9. from format_convert.utils import get_ip_port, get_intranet_ip, get_args_from_config, get_all_ip, get_using_ip
  10. # 解析配置文件
  11. ip_port_dict = get_ip_port()
  12. ip = get_using_ip()
  13. print("local ip:", ip)
  14. # 自定义输出
  15. std_out = " >>/convert.out 2>&1 &"
  16. std_out_gpu = " >>/gpu.out 2>&1 &"
  17. std_out_schedule = " >>/schedule.out 2>&1 &"
  18. # 获取接口各个参数,提前生成命令
  19. python_path = get_args_from_config(ip_port_dict, ip, "python_path")[0]
  20. project_path = get_args_from_config(ip_port_dict, ip, "project_path")[0]
  21. gunicorn_path = get_args_from_config(ip_port_dict, ip, "gunicorn_path")[0]
  22. interface_list = ['convert', 'ocr', 'otr', 'idc', 'isr', 'atc', 'yolo', 'office']
  23. comm_dict = {}
  24. interface_port_dict = {}
  25. for name in interface_list:
  26. if get_args_from_config(ip_port_dict, ip, name, 'MASTER'):
  27. port_list, num_list, gpu_list = get_args_from_config(ip_port_dict, ip, name, 'MASTER')[0]
  28. else:
  29. port_list, num_list, gpu_list = get_args_from_config(ip_port_dict, ip, name)[0]
  30. interface_port_dict[name] = [port_list, num_list, gpu_list]
  31. for i, port in enumerate(port_list):
  32. port_num = num_list[i]
  33. if int(port_num) == 0:
  34. continue
  35. # 设置gpu
  36. if gpu_list:
  37. gpu = gpu_list[i]
  38. else:
  39. gpu = -1
  40. os.environ["CUDA_VISIBLE_DEVICES"] = str(gpu)
  41. gpu_comm = 'export CUDA_VISIBLE_DEVICES=' + str(gpu) + ' && '
  42. # 设置命令
  43. if name == 'convert':
  44. comm = "nohup " + gunicorn_path + " -w " + str(port_num) + " -t 300 --keep-alive 600 -b 0.0.0.0:" + str(port) + " --chdir " + project_path + "format_convert" + ' ' + name + ":app" + std_out
  45. elif name == 'yolo':
  46. comm = "nohup " + gunicorn_path + " -w " + str(port_num) + " -t 300 --keep-alive 600 -b 0.0.0.0:" + str(port) + " --chdir " + project_path + "/botr/yolov8" + ' ' + name + "_interface:app" + std_out_gpu
  47. elif name == 'office':
  48. comm = "docker run --init -itd --log-opt max-size=10m --log-opt max-file=3 -p #:16000 soffice:v2 bash"
  49. office_port_comm_list = []
  50. for office_port in range(port, port + port_num):
  51. office_port_comm_list = re.sub("#", str(office_port), comm)
  52. comm_dict[name] = office_port_comm_list
  53. else:
  54. comm = "nohup " + gunicorn_path + " -w " + str(port_num) + " -t 300 --keep-alive 600 -b 0.0.0.0:" + str(port) + " --chdir " + project_path + "/" + name + ' ' + name + "_interface:app" + std_out_gpu
  55. if name == 'office':
  56. continue
  57. if name in comm_dict.keys():
  58. comm_dict[name] += [gpu_comm + comm]
  59. else:
  60. comm_dict[name] = [gpu_comm + comm]
  61. # print(name, port_list, num_list, gpu_list)
  62. # convert_port_list = get_args_from_config(ip_port_dict, ip, "convert", "MASTER")
  63. # if convert_port_list:
  64. # convert_port_list = convert_port_list[0]
  65. # ocr_port_list = get_args_from_config(ip_port_dict, ip, "ocr")
  66. # otr_port_list = get_args_from_config(ip_port_dict, ip, "otr")
  67. # idc_port_list = get_args_from_config(ip_port_dict, ip, "idc")
  68. # isr_port_list = get_args_from_config(ip_port_dict, ip, "isr")
  69. # atc_port_list = get_args_from_config(ip_port_dict, ip, "atc")
  70. # yolo_port_list = get_args_from_config(ip_port_dict, ip, "yolo")
  71. # soffice_port_list = get_args_from_config(ip_port_dict, ip, "office", "MASTER")
  72. # if soffice_port_list:
  73. # soffice_port_list = soffice_port_list[0]
  74. # python_path_list = get_args_from_config(ip_port_dict, ip, "python_path")
  75. # project_path_list = get_args_from_config(ip_port_dict, ip, "project_path")
  76. # gunicorn_path_list = get_args_from_config(ip_port_dict, ip, "gunicorn_path")
  77. # std_out = " >>/convert.out 2>&1 &"
  78. # std_out_gpu = " >>/gpu.out 2>&1 &"
  79. # std_out_schedule = " >>/schedule.out 2>&1 &"
  80. #
  81. # print("convert_port_list", convert_port_list)
  82. # print("ocr_port_list", ocr_port_list)
  83. # print("otr_port_list", otr_port_list)
  84. # print("idc_port_list", idc_port_list)
  85. # print("isr_port_list", isr_port_list)
  86. # print("atc_port_list", atc_port_list)
  87. # print("yolo_port_list", yolo_port_list)
  88. # print("soffice_port_list", soffice_port_list)
  89. #
  90. # # 根据port生成gunicorn语句
  91. # ocr_comm_list = []
  92. # otr_comm_list = []
  93. # isr_comm_list = []
  94. # idc_comm_list = []
  95. # atc_comm_list = []
  96. # yolo_comm_list = []
  97. # for i in range(len(ocr_port_list)):
  98. # ocr_comm_list.append("nohup " + gunicorn_path_list[i] + " -w " + str(len(ocr_port_list[i]))
  99. # + " -t 300 --keep-alive 600 -b 0.0.0.0:# --chdir "
  100. # + project_path_list[i] + "/ocr ocr_interface:app" + std_out_gpu)
  101. # for i in range(len(otr_port_list)):
  102. # otr_comm_list.append("nohup " + gunicorn_path_list[i] + " -w " + str(len(otr_port_list[i]))
  103. # + " -t 300 --keep-alive 600 -b 0.0.0.0:# --chdir "
  104. # + project_path_list[i] + "/otr otr_interface:app" + std_out_gpu)
  105. # for i in range(len(idc_port_list)):
  106. # idc_comm_list.append("nohup " + gunicorn_path_list[i] + " -w " + str(len(idc_port_list[i]))
  107. # + " -t 300 --keep-alive 600 -b 0.0.0.0:# --chdir "
  108. # + project_path_list[i] + "/idc idc_interface:app" + std_out_gpu)
  109. # for i in range(len(isr_port_list)):
  110. # isr_comm_list.append("nohup " + gunicorn_path_list[i] + " -w " + str(len(isr_port_list[i]))
  111. # + " -t 300 --keep-alive 600 -b 0.0.0.0:# --chdir "
  112. # + project_path_list[i] + "/isr isr_interface:app" + std_out_gpu)
  113. # for i in range(len(atc_port_list)):
  114. # atc_comm_list.append("nohup " + gunicorn_path_list[i] + " -w " + str(len(atc_port_list[i]))
  115. # + " -t 300 --keep-alive 600 -b 0.0.0.0:# --chdir "
  116. # + project_path_list[i] + "/atc atc_interface:app" + std_out_gpu)
  117. # for i in range(len(yolo_port_list)):
  118. # yolo_comm_list.append("nohup " + gunicorn_path_list[i] + " -w " + str(len(yolo_port_list[i]))
  119. # + " -t 300 --keep-alive 600 -b 0.0.0.0:# --chdir "
  120. # + project_path_list[i] + "/botr/yolov8 yolo_interface:app" + std_out_gpu)
  121. #
  122. # convert_comm = "nohup " + gunicorn_path_list[0] + " -w " + str(len(convert_port_list)) + " -t 300 -b 0.0.0.0:# --chdir " \
  123. # + project_path_list[0] + "/format_convert convert:app" + std_out
  124. # soffice_comm = "docker run --init -itd --log-opt max-size=10m --log-opt max-file=3 -p #:16000 soffice:v2 bash"
  125. def get_port():
  126. net_conn = psutil.net_connections()
  127. current_port_list = []
  128. for conn in net_conn:
  129. current_port_list.append(str(conn.laddr.port))
  130. current_port_list = list(set(current_port_list))
  131. current_port_list.sort(key=lambda x: x)
  132. return current_port_list
  133. def restart(interface_type, port, index=0):
  134. # if process_type == "convert":
  135. # _comm = re.sub("#", port, convert_comm)
  136. # elif process_type == "ocr":
  137. # _comm = re.sub("#", port, ocr_comm_list[index])
  138. # elif process_type == "otr":
  139. # _comm = re.sub("#", port, otr_comm_list[index])
  140. # elif process_type == "soffice":
  141. # _comm = re.sub("#", port, soffice_comm)
  142. # elif process_type == "idc":
  143. # _comm = re.sub("#", port, idc_comm_list[index])
  144. # elif process_type == "isr":
  145. # _comm = re.sub("#", port, isr_comm_list[index])
  146. # elif process_type == "atc":
  147. # _comm = re.sub("#", port, atc_comm_list[index])
  148. # elif process_type == "yolo":
  149. # _comm = re.sub("#", port, yolo_comm_list[index])
  150. # else:
  151. # _comm = "netstat -nltp"
  152. # print("no process_type", process_type)
  153. #
  154. _comm_list = comm_dict.get(interface_type)
  155. if not _comm_list:
  156. print('monitor_process_config restart command error! check config!')
  157. raise
  158. for _comm in _comm_list:
  159. if str(port) in _comm:
  160. print(datetime.datetime.now(), "restart comm", _comm)
  161. os.system(_comm)
  162. def kill_soffice(limit_sec=30):
  163. try:
  164. pid_list = psutil.pids()
  165. for pid in pid_list:
  166. process = psutil.Process(pid)
  167. process_cmd = ''
  168. for c in process.cmdline():
  169. process_cmd += c + " "
  170. if process_cmd.strip() == "":
  171. continue
  172. if process.status() == "zombie":
  173. print("zombie cmd", process_cmd)
  174. if re.search("soffice", process.exe()):
  175. start_time = process.create_time()
  176. now_time = time.time()
  177. run_time = now_time-start_time
  178. if run_time >= limit_sec:
  179. comm = "kill -9 " + str(pid)
  180. print(datetime.datetime.now(), "kill process ", str(pid), str(process.exe()), str(run_time), ">", limit_sec)
  181. os.system(comm)
  182. except:
  183. pass
  184. def kill_nested_timeout_process():
  185. try:
  186. pid_list = psutil.pids()
  187. suspect_pid_list = []
  188. for pid in pid_list:
  189. process = psutil.Process(pid)
  190. process_cmd = ''
  191. for c in process.cmdline():
  192. process_cmd += c + " "
  193. if process_cmd.strip() == "":
  194. continue
  195. if re.search("convert:app", process_cmd):
  196. ppid = process.ppid()
  197. start_time = process.create_time()
  198. now_time = time.time()
  199. run_time = now_time-start_time
  200. if str(ppid) == "1":
  201. suspect_pid_list.append([str(pid), float(run_time)])
  202. # 时间最久的父进程为1的不能杀,是接口主进程
  203. if len(suspect_pid_list) <= 1:
  204. return
  205. else:
  206. suspect_pid_list.sort(key=lambda x: x[1], reverse=True)
  207. for pid, run_time in suspect_pid_list[1:]:
  208. # print("pid", pid, run_time)
  209. comm = "kill -9 " + str(pid)
  210. print(datetime.datetime.now(), "kill process ", str(pid), "father is 1", process_cmd)
  211. os.system(comm)
  212. except:
  213. pass
  214. def monitor():
  215. for _name in interface_list:
  216. if interface_port_dict.get(_name):
  217. _port_list, _num_list, _gpu_list = interface_port_dict.get(_name)
  218. current_port_list = get_port()
  219. for j, p in enumerate(_port_list):
  220. if str(p) not in current_port_list:
  221. restart(_name, p)
  222. # if convert_port_list:
  223. # for p in convert_port_list[:1]:
  224. # if p not in current_port_list:
  225. # restart("convert", p)
  226. #
  227. # if ocr_port_list:
  228. # for j in range(len(ocr_port_list)):
  229. # for p in ocr_port_list[j][:1]:
  230. # if p not in current_port_list:
  231. # restart("ocr", p, index=j)
  232. #
  233. # if otr_port_list:
  234. # for j in range(len(otr_port_list)):
  235. # for p in otr_port_list[j][:1]:
  236. # if p not in current_port_list:
  237. # restart("otr", p, index=j)
  238. #
  239. # if idc_port_list:
  240. # for j in range(len(idc_port_list)):
  241. # for p in idc_port_list[j][:1]:
  242. # if p not in current_port_list:
  243. # restart("idc", p, index=j)
  244. #
  245. # if isr_port_list:
  246. # for j in range(len(isr_port_list)):
  247. # for p in isr_port_list[j][:1]:
  248. # if p not in current_port_list:
  249. # restart("isr", p, index=j)
  250. #
  251. # if atc_port_list:
  252. # for j in range(len(atc_port_list)):
  253. # for p in atc_port_list[j][:1]:
  254. # if p not in current_port_list:
  255. # restart("atc", p, index=j)
  256. #
  257. # if yolo_port_list:
  258. # for j in range(len(yolo_port_list)):
  259. # for p in yolo_port_list[j][:1]:
  260. # if p not in current_port_list:
  261. # restart("yolo", p, index=j)
  262. #
  263. # if soffice_port_list:
  264. # for p in soffice_port_list:
  265. # if p not in current_port_list:
  266. # restart("soffice", p)
  267. kill_soffice()
  268. kill_nested_timeout_process()
  269. # if schedule_port_list:
  270. # for p in schedule_port_list:
  271. # if p not in current_port_list:
  272. # restart("schedule", p)
  273. if __name__ == "__main__":
  274. for i in range(3):
  275. # os.system("echo $(date +%F%n%T)")
  276. monitor()
  277. time.sleep(10)