123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- import logging
- import os
- import re
- import sys
- import time
- import psutil
- sys.path.append(os.path.dirname(os.path.abspath(__file__)) + "/../")
- from format_convert.utils import get_ip_port, get_intranet_ip
- ip_port_dict = get_ip_port()
- ip = "http://" + get_intranet_ip()
- # ip = "http://127.0.0.1"
- convert_port_list = ip_port_dict.get(ip).get("convert")
- ocr_port_list = ip_port_dict.get(ip).get("ocr")
- otr_port_list = ip_port_dict.get(ip).get("otr")
- soffice_port_list = ip_port_dict.get(ip).get("office")
- schedule_port_list = ip_port_dict.get(ip).get("schedule")
- python_path = ip_port_dict.get(ip).get("python_path")
- project_path = ip_port_dict.get(ip).get("project_path")
- interface_path = project_path[:-1]
- std_out = " >>/convert.out 2>&1 &"
- std_out_gpu = " >>/gpu.out 2>&1 &"
- std_out_schedule = " >>/schedule.out 2>&1 &"
- convert_comm = "nohup " + python_path + " " + interface_path + "/format_convert/convert.py #" + std_out
- ocr_comm = "nohup " + python_path + " " + interface_path + "/ocr/ocr_interface.py # 0" + std_out_gpu
- otr_comm = "nohup " + python_path + " " + interface_path + "/otr/otr_interface.py # 0" + std_out_gpu
- schedule_comm = "nohup " + python_path + " " + interface_path + "/format_convert/schedule_interface.py #" + std_out_schedule
- soffice_comm = "docker run --init -itd --log-opt max-size=10m --log-opt max-file=3 -p #:16000 soffice:v2 bash"
- def get_port():
- net_conn = psutil.net_connections()
- current_port_list = []
- for conn in net_conn:
- current_port_list.append(str(conn.laddr.port))
- current_port_list = list(set(current_port_list))
- current_port_list.sort(key=lambda x: x)
- # print(current_port_list)
- return current_port_list
- def restart(process_type, port):
- if process_type == "convert":
- _comm = re.sub("#", port, convert_comm)
- elif process_type == "ocr":
- _comm = re.sub("#", port, ocr_comm)
- elif process_type == "otr":
- _comm = re.sub("#", port, otr_comm)
- elif process_type == "soffice":
- _comm = re.sub("#", port, soffice_comm)
- elif process_type == "schedule":
- _comm = re.sub("#", port, schedule_comm)
- else:
- _comm = "netstat -nltp"
- print("no process_type", process_type)
- os.system("echo $(date +%F%n%T)")
- print("restart comm", _comm)
- # os.system("netstat -nltp")
- os.system(_comm)
- def kill_soffice(limit_sec=30):
- pid_list = psutil.pids()
- for pid in pid_list:
- process = psutil.Process(pid)
- process_cmd = ''
- for c in process.cmdline():
- process_cmd += c + " "
- if process_cmd.strip() == "":
- continue
- if process.status() == "zombie":
- print("zombie cmd", process_cmd)
- if re.search("soffice", process.exe()):
- start_time = process.create_time()
- now_time = time.time()
- run_time = now_time-start_time
- if run_time >= limit_sec:
- comm = "kill -9 " + str(pid)
- os.system("echo $(date +%F%n%T)")
- print("kill process ", str(pid), str(process.exe()), str(run_time), ">", limit_sec)
- os.system(comm)
- def kill_nested_timeout_process():
- pid_list = psutil.pids()
- suspect_pid_list = []
- for pid in pid_list:
- process = psutil.Process(pid)
- process_cmd = ''
- for c in process.cmdline():
- process_cmd += c + " "
- if process_cmd.strip() == "":
- continue
- if re.search("convert\.py|gunicorn", process_cmd):
- ppid = process.ppid()
- start_time = process.create_time()
- now_time = time.time()
- run_time = now_time-start_time
- if str(ppid) == "1":
- suspect_pid_list.append([str(pid), float(run_time)])
- # 时间最久的父进程为1的不能杀,是接口主进程
- # print("suspect_pid_list", str(suspect_pid_list))
- if len(suspect_pid_list) <= 1:
- return
- else:
- suspect_pid_list.sort(key=lambda x: x[1], reverse=True)
- for pid, run_time in suspect_pid_list[1:]:
- # print("pid", pid, run_time)
- comm = "kill -9 " + str(pid)
- print("kill process ", str(pid), "father is 1", process_cmd)
- os.system(comm)
- def monitor():
- current_port_list = get_port()
- # if convert_port_list:
- # for p in convert_port_list:
- # if p not in current_port_list:
- # restart("convert", p)
- if ocr_port_list:
- for p in ocr_port_list:
- if p not in current_port_list:
- restart("ocr", p)
- if otr_port_list:
- for p in otr_port_list:
- if p not in current_port_list:
- restart("otr", p)
- if soffice_port_list:
- for p in soffice_port_list:
- if p not in current_port_list:
- restart("soffice", p)
- kill_soffice()
- kill_nested_timeout_process()
- # if schedule_port_list:
- # for p in schedule_port_list:
- # if p not in current_port_list:
- # restart("schedule", p)
- if __name__ == "__main__":
- for i in range(6):
- # os.system("echo $(date +%F%n%T)")
- monitor()
- time.sleep(10)
|