monitor_process_config.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. convert_port_list = ip_port_dict.get(ip).get("convert")
  12. ocr_port_list = ip_port_dict.get(ip).get("ocr")
  13. otr_port_list = ip_port_dict.get(ip).get("otr")
  14. soffice_port_list = ip_port_dict.get(ip).get("office")
  15. schedule_port_list = ip_port_dict.get(ip).get("schedule")
  16. python_path = ip_port_dict.get(ip).get("python_path")
  17. project_path = ip_port_dict.get(ip).get("project_path")
  18. interface_path = project_path[:-1]
  19. std_out = " >>/convert.out 2>&1 &"
  20. std_out_gpu = " >>/gpu.out 2>&1 &"
  21. std_out_schedule = " >>/schedule.out 2>&1 &"
  22. convert_comm = "nohup " + python_path + " " + interface_path + "/format_convert/convert.py #" + std_out
  23. ocr_comm = "nohup " + python_path + " " + interface_path + "/ocr/ocr_interface.py # 0" + std_out_gpu
  24. otr_comm = "nohup " + python_path + " " + interface_path + "/otr/otr_interface.py # 0" + std_out_gpu
  25. schedule_comm = "nohup " + python_path + " " + interface_path + "/format_convert/schedule_interface.py #" + std_out_schedule
  26. soffice_comm = "docker run --init -itd --log-opt max-size=10m --log-opt max-file=3 -p #:16000 soffice:v2 bash"
  27. def get_port():
  28. net_conn = psutil.net_connections()
  29. current_port_list = []
  30. for conn in net_conn:
  31. current_port_list.append(str(conn.laddr.port))
  32. current_port_list = list(set(current_port_list))
  33. current_port_list.sort(key=lambda x: x)
  34. # print(current_port_list)
  35. return current_port_list
  36. def restart(process_type, port):
  37. if process_type == "convert":
  38. _comm = re.sub("#", port, convert_comm)
  39. elif process_type == "ocr":
  40. _comm = re.sub("#", port, ocr_comm)
  41. elif process_type == "otr":
  42. _comm = re.sub("#", port, otr_comm)
  43. elif process_type == "soffice":
  44. _comm = re.sub("#", port, soffice_comm)
  45. elif process_type == "schedule":
  46. _comm = re.sub("#", port, schedule_comm)
  47. else:
  48. _comm = "netstat -nltp"
  49. print("no process_type", process_type)
  50. os.system("echo $(date +%F%n%T)")
  51. print("restart comm", _comm)
  52. # os.system("netstat -nltp")
  53. os.system(_comm)
  54. def kill_soffice(limit_sec=15):
  55. pid_list = psutil.pids()
  56. for pid in pid_list:
  57. process = psutil.Process(pid)
  58. process_cmd = ''
  59. for c in process.cmdline():
  60. process_cmd += c + " "
  61. if process_cmd.strip() == "":
  62. continue
  63. if process.status() == "zombie":
  64. print("zombie cmd", process_cmd)
  65. if re.search("soffice", process.exe()):
  66. start_time = process.create_time()
  67. now_time = time.time()
  68. run_time = now_time-start_time
  69. if run_time >= limit_sec:
  70. comm = "kill -9 " + str(pid)
  71. os.system("echo $(date +%F%n%T)")
  72. print("kill process ", str(pid), str(process.exe()), str(run_time), ">", limit_sec)
  73. os.system(comm)
  74. def monitor():
  75. current_port_list = get_port()
  76. if convert_port_list:
  77. for p in convert_port_list:
  78. if p not in current_port_list:
  79. restart("convert", p)
  80. if ocr_port_list:
  81. for p in ocr_port_list:
  82. if p not in current_port_list:
  83. restart("ocr", p)
  84. if otr_port_list:
  85. for p in otr_port_list:
  86. if p not in current_port_list:
  87. restart("otr", p)
  88. if soffice_port_list:
  89. for p in soffice_port_list:
  90. if p not in current_port_list:
  91. restart("soffice", p)
  92. kill_soffice()
  93. # if schedule_port_list:
  94. # for p in schedule_port_list:
  95. # if p not in current_port_list:
  96. # restart("schedule", p)
  97. if __name__ == "__main__":
  98. for i in range(6):
  99. # os.system("echo $(date +%F%n%T)")
  100. monitor()
  101. time.sleep(10)