monitor_process_config.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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:v1 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=15):
  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 monitor():
  76. current_port_list = get_port()
  77. if convert_port_list:
  78. for p in convert_port_list:
  79. if p not in current_port_list:
  80. restart("convert", p)
  81. if ocr_port_list:
  82. for p in ocr_port_list:
  83. if p not in current_port_list:
  84. restart("ocr", p)
  85. if otr_port_list:
  86. for p in otr_port_list:
  87. if p not in current_port_list:
  88. restart("otr", p)
  89. if soffice_port_list:
  90. for p in soffice_port_list:
  91. if p not in current_port_list:
  92. restart("soffice", p)
  93. kill_soffice()
  94. # if schedule_port_list:
  95. # for p in schedule_port_list:
  96. # if p not in current_port_list:
  97. # restart("schedule", p)
  98. if __name__ == "__main__":
  99. for i in range(6):
  100. # os.system("echo $(date +%F%n%T)")
  101. monitor()
  102. time.sleep(10)