monitor_process_config.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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
  10. # ip_port_dict = get_ip_port()
  11. # ip = "http://" + get_intranet_ip()
  12. # ip = "http://127.0.0.1"
  13. # convert_port_list = ip_port_dict.get(ip).get("convert")
  14. # ocr_port_list = ip_port_dict.get(ip).get("ocr")
  15. # otr_port_list = ip_port_dict.get(ip).get("otr")
  16. # soffice_port_list = ip_port_dict.get(ip).get("office")
  17. # if not convert_port_list:
  18. # convert_port_list = []
  19. # if not ocr_port_list:
  20. # ocr_port_list = []
  21. # if not otr_port_list:
  22. # otr_port_list = []
  23. # if not soffice_port_list:
  24. # soffice_port_list = []
  25. #
  26. # # schedule_port_list = ip_port_dict.get(ip).get("schedule")
  27. # # python_path = ip_port_dict.get(ip).get("python_path")
  28. # # project_path = ip_port_dict.get(ip).get("project_path")
  29. #
  30. # interface_path = project_path[:-1]
  31. # std_out = " >>/convert.out 2>&1 &"
  32. # std_out_gpu = " >>/gpu.out 2>&1 &"
  33. # std_out_schedule = " >>/schedule.out 2>&1 &"
  34. # # convert_comm = "nohup " + python_path + " " + interface_path + "/format_convert/convert.py #" + std_out
  35. # # ocr_comm = "nohup " + python_path + " " + interface_path + "/ocr/ocr_gpu_interface.py # 0" + std_out_gpu
  36. # # otr_comm = "nohup " + python_path + " " + interface_path + "/otr/otr_gpu_interface.py # 0" + std_out_gpu
  37. # schedule_comm = "nohup " + python_path + " " + interface_path + "/format_convert/schedule_interface.py #" + std_out_schedule
  38. # soffice_comm = "docker run --init -itd --log-opt max-size=10m --log-opt max-file=3 -p #:16000 soffice:v2 bash"
  39. #
  40. #
  41. # gunicorn_path = python_path
  42. # # print("convert_port_list", len(convert_port_list))
  43. # convert_comm = "nohup " + gunicorn_path + " -w " + str(len(convert_port_list)) + " -t 300 -b 0.0.0.0:# --chdir " \
  44. # + interface_path + "/format_convert convert:app" + std_out
  45. # ocr_comm = "nohup " + gunicorn_path + " -w " + str(len(ocr_port_list)) + " -t 300 --keep-alive 600 -b 0.0.0.0:# --chdir " \
  46. # + interface_path + "/ocr ocr_gpu_interface:app" + std_out_gpu
  47. # otr_comm = "nohup " + gunicorn_path + " -w " + str(len(otr_port_list)) + " -t 300 --keep-alive 600 -b 0.0.0.0:# --chdir " \
  48. # + interface_path + "/otr otr_gpu_interface:app" + std_out_gpu
  49. ip_port_dict = get_ip_port()
  50. ip = "http://127.0.0.1"
  51. convert_port_list = get_args_from_config(ip_port_dict, ip, "convert", "MASTER")[0]
  52. ocr_port_list = get_args_from_config(ip_port_dict, ip, "ocr")
  53. otr_port_list = get_args_from_config(ip_port_dict, ip, "otr")
  54. soffice_port_list = get_args_from_config(ip_port_dict, ip, "office", "MASTER")[0]
  55. python_path_list = get_args_from_config(ip_port_dict, ip, "python_path")
  56. project_path_list = get_args_from_config(ip_port_dict, ip, "project_path")
  57. gunicorn_path_list = get_args_from_config(ip_port_dict, ip, "gunicorn_path")
  58. std_out = " >>/convert.out 2>&1 &"
  59. std_out_gpu = " >>/gpu.out 2>&1 &"
  60. std_out_schedule = " >>/schedule.out 2>&1 &"
  61. print("ocr_port_list", ocr_port_list)
  62. print("otr_port_list", otr_port_list)
  63. print("soffice_port_list", soffice_port_list)
  64. ocr_comm_list = []
  65. otr_comm_list = []
  66. for i in range(len(ocr_port_list)):
  67. ocr_comm_list.append("nohup " + gunicorn_path_list[i] + " -w " + str(len(ocr_port_list[i]))
  68. + " -t 300 --keep-alive 600 -b 0.0.0.0:# --chdir "
  69. + project_path_list[i] + "/ocr ocr_interface:app" + std_out_gpu)
  70. for i in range(len(otr_port_list)):
  71. otr_comm_list.append("nohup " + gunicorn_path_list[i] + " -w " + str(len(otr_port_list[i]))
  72. + " -t 300 --keep-alive 600 -b 0.0.0.0:# --chdir "
  73. + project_path_list[i] + "/otr otr_interface:app" + std_out_gpu)
  74. convert_comm = "nohup " + gunicorn_path_list[0] + " -w " + str(len(convert_port_list)) + " -t 300 -b 0.0.0.0:# --chdir " \
  75. + project_path_list[0] + "/format_convert convert:app" + std_out
  76. schedule_comm = "nohup " + python_path_list[0] + " " + project_path_list[0] + "/format_convert/schedule_interface.py #" + std_out_schedule
  77. soffice_comm = "docker run --init -itd --log-opt max-size=10m --log-opt max-file=3 -p #:16000 soffice:v2 bash"
  78. def get_port():
  79. net_conn = psutil.net_connections()
  80. current_port_list = []
  81. for conn in net_conn:
  82. current_port_list.append(str(conn.laddr.port))
  83. current_port_list = list(set(current_port_list))
  84. current_port_list.sort(key=lambda x: x)
  85. return current_port_list
  86. def restart(process_type, port, index=0):
  87. if process_type == "convert":
  88. _comm = re.sub("#", port, convert_comm)
  89. elif process_type == "ocr":
  90. _comm = re.sub("#", port, ocr_comm_list[index])
  91. elif process_type == "otr":
  92. _comm = re.sub("#", port, otr_comm_list[index])
  93. elif process_type == "soffice":
  94. _comm = re.sub("#", port, soffice_comm)
  95. elif process_type == "schedule":
  96. _comm = re.sub("#", port, schedule_comm)
  97. else:
  98. _comm = "netstat -nltp"
  99. print("no process_type", process_type)
  100. # os.system("echo $(date +%F%n%T)")
  101. print(datetime.datetime.now(), "restart comm", _comm)
  102. os.system(_comm)
  103. def kill_soffice(limit_sec=30):
  104. pid_list = psutil.pids()
  105. for pid in pid_list:
  106. process = psutil.Process(pid)
  107. process_cmd = ''
  108. for c in process.cmdline():
  109. process_cmd += c + " "
  110. if process_cmd.strip() == "":
  111. continue
  112. if process.status() == "zombie":
  113. print("zombie cmd", process_cmd)
  114. if re.search("soffice", process.exe()):
  115. start_time = process.create_time()
  116. now_time = time.time()
  117. run_time = now_time-start_time
  118. if run_time >= limit_sec:
  119. comm = "kill -9 " + str(pid)
  120. print(datetime.datetime.now(), "kill process ", str(pid), str(process.exe()), str(run_time), ">", limit_sec)
  121. os.system(comm)
  122. def kill_nested_timeout_process():
  123. pid_list = psutil.pids()
  124. suspect_pid_list = []
  125. for pid in pid_list:
  126. process = psutil.Process(pid)
  127. process_cmd = ''
  128. for c in process.cmdline():
  129. process_cmd += c + " "
  130. if process_cmd.strip() == "":
  131. continue
  132. if re.search("convert:app", process_cmd):
  133. ppid = process.ppid()
  134. start_time = process.create_time()
  135. now_time = time.time()
  136. run_time = now_time-start_time
  137. if str(ppid) == "1":
  138. suspect_pid_list.append([str(pid), float(run_time)])
  139. # 时间最久的父进程为1的不能杀,是接口主进程
  140. if len(suspect_pid_list) <= 1:
  141. return
  142. else:
  143. suspect_pid_list.sort(key=lambda x: x[1], reverse=True)
  144. for pid, run_time in suspect_pid_list[1:]:
  145. # print("pid", pid, run_time)
  146. comm = "kill -9 " + str(pid)
  147. print(datetime.datetime.now(), "kill process ", str(pid), "father is 1", process_cmd)
  148. os.system(comm)
  149. def monitor():
  150. current_port_list = get_port()
  151. if convert_port_list:
  152. for p in convert_port_list[:1]:
  153. if p not in current_port_list:
  154. restart("convert", p)
  155. if ocr_port_list:
  156. for j in range(len(ocr_port_list)):
  157. for p in ocr_port_list[j][:1]:
  158. if p not in current_port_list:
  159. restart("ocr", p, index=j)
  160. if otr_port_list:
  161. for j in range(len(otr_port_list)):
  162. for p in otr_port_list[j][:1]:
  163. if p not in current_port_list:
  164. restart("otr", p, index=j)
  165. if soffice_port_list:
  166. for p in soffice_port_list:
  167. if p not in current_port_list:
  168. restart("soffice", p)
  169. kill_soffice()
  170. kill_nested_timeout_process()
  171. # if schedule_port_list:
  172. # for p in schedule_port_list:
  173. # if p not in current_port_list:
  174. # restart("schedule", p)
  175. if __name__ == "__main__":
  176. for i in range(6):
  177. # os.system("echo $(date +%F%n%T)")
  178. monitor()
  179. time.sleep(10)