monitor_process_config.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import logging
  2. import os
  3. import re
  4. import sys
  5. import time
  6. import psutil
  7. sys.path.append(os.path.dirname(os.path.abspath(__file__)) + "/../")
  8. from format_convert.utils import get_ip_port, get_intranet_ip
  9. ip_port_dict = get_ip_port()
  10. ip = "http://" + get_intranet_ip()
  11. # ip = "http://127.0.0.1"
  12. convert_port_list = ip_port_dict.get(ip).get("convert")
  13. ocr_port_list = ip_port_dict.get(ip).get("ocr")
  14. otr_port_list = ip_port_dict.get(ip).get("otr")
  15. soffice_port_list = ip_port_dict.get(ip).get("office")
  16. schedule_port_list = ip_port_dict.get(ip).get("schedule")
  17. python_path = ip_port_dict.get(ip).get("python_path")
  18. project_path = ip_port_dict.get(ip).get("project_path")
  19. interface_path = project_path[:-1]
  20. std_out = " >>/convert.out 2>&1 &"
  21. std_out_gpu = " >>/gpu.out 2>&1 &"
  22. std_out_schedule = " >>/schedule.out 2>&1 &"
  23. convert_comm = "nohup " + python_path + " " + interface_path + "/format_convert/convert.py #" + std_out
  24. ocr_comm = "nohup " + python_path + " " + interface_path + "/ocr/ocr_interface.py # 0" + std_out_gpu
  25. otr_comm = "nohup " + python_path + " " + interface_path + "/otr/otr_interface.py # 0" + std_out_gpu
  26. schedule_comm = "nohup " + python_path + " " + interface_path + "/format_convert/schedule_interface.py #" + std_out_schedule
  27. soffice_comm = "docker run --init -itd --log-opt max-size=10m --log-opt max-file=3 -p #:16000 soffice:v2 bash"
  28. def get_port():
  29. net_conn = psutil.net_connections()
  30. current_port_list = []
  31. for conn in net_conn:
  32. current_port_list.append(str(conn.laddr.port))
  33. current_port_list = list(set(current_port_list))
  34. current_port_list.sort(key=lambda x: x)
  35. # print(current_port_list)
  36. return current_port_list
  37. def restart(process_type, port):
  38. if process_type == "convert":
  39. _comm = re.sub("#", port, convert_comm)
  40. elif process_type == "ocr":
  41. _comm = re.sub("#", port, ocr_comm)
  42. elif process_type == "otr":
  43. _comm = re.sub("#", port, otr_comm)
  44. elif process_type == "soffice":
  45. _comm = re.sub("#", port, soffice_comm)
  46. elif process_type == "schedule":
  47. _comm = re.sub("#", port, schedule_comm)
  48. else:
  49. _comm = "netstat -nltp"
  50. print("no process_type", process_type)
  51. os.system("echo $(date +%F%n%T)")
  52. print("restart comm", _comm)
  53. # os.system("netstat -nltp")
  54. os.system(_comm)
  55. def kill_soffice(limit_sec=30):
  56. pid_list = psutil.pids()
  57. for pid in pid_list:
  58. process = psutil.Process(pid)
  59. process_cmd = ''
  60. for c in process.cmdline():
  61. process_cmd += c + " "
  62. if process_cmd.strip() == "":
  63. continue
  64. if process.status() == "zombie":
  65. print("zombie cmd", process_cmd)
  66. if re.search("soffice", process.exe()):
  67. start_time = process.create_time()
  68. now_time = time.time()
  69. run_time = now_time-start_time
  70. if run_time >= limit_sec:
  71. comm = "kill -9 " + str(pid)
  72. os.system("echo $(date +%F%n%T)")
  73. print("kill process ", str(pid), str(process.exe()), str(run_time), ">", limit_sec)
  74. os.system(comm)
  75. def kill_nested_timeout_process():
  76. pid_list = psutil.pids()
  77. suspect_pid_list = []
  78. for pid in pid_list:
  79. process = psutil.Process(pid)
  80. process_cmd = ''
  81. for c in process.cmdline():
  82. process_cmd += c + " "
  83. if process_cmd.strip() == "":
  84. continue
  85. if re.search("convert\.py|gunicorn", process_cmd):
  86. ppid = process.ppid()
  87. start_time = process.create_time()
  88. now_time = time.time()
  89. run_time = now_time-start_time
  90. if str(ppid) == "1":
  91. suspect_pid_list.append([str(pid), float(run_time)])
  92. # 时间最久的父进程为1的不能杀,是接口主进程
  93. # print("suspect_pid_list", str(suspect_pid_list))
  94. if len(suspect_pid_list) <= 1:
  95. return
  96. else:
  97. suspect_pid_list.sort(key=lambda x: x[1], reverse=True)
  98. for pid, run_time in suspect_pid_list[1:]:
  99. # print("pid", pid, run_time)
  100. comm = "kill -9 " + str(pid)
  101. print("kill process ", str(pid), "father is 1", process_cmd)
  102. os.system(comm)
  103. def monitor():
  104. current_port_list = get_port()
  105. # if convert_port_list:
  106. # for p in convert_port_list:
  107. # if p not in current_port_list:
  108. # restart("convert", p)
  109. if ocr_port_list:
  110. for p in ocr_port_list:
  111. if p not in current_port_list:
  112. restart("ocr", p)
  113. if otr_port_list:
  114. for p in otr_port_list:
  115. if p not in current_port_list:
  116. restart("otr", p)
  117. if soffice_port_list:
  118. for p in soffice_port_list:
  119. if p not in current_port_list:
  120. restart("soffice", p)
  121. kill_soffice()
  122. kill_nested_timeout_process()
  123. # if schedule_port_list:
  124. # for p in schedule_port_list:
  125. # if p not in current_port_list:
  126. # restart("schedule", p)
  127. if __name__ == "__main__":
  128. for i in range(6):
  129. # os.system("echo $(date +%F%n%T)")
  130. monitor()
  131. time.sleep(10)