| 
					
				 | 
			
			
				@@ -3,6 +3,7 @@ import os 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import asyncio 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import logging 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import time 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import requests 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import utils 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import rov_server_config 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -14,6 +15,65 @@ health_instances = [] 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 ess_instances = [] 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+def rov_server_health_check(client, instance_id, max_wait_time=None): 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    """ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    服务健康检查 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    :param client: 客户端连接 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    :param instance_id: instanceId 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    :param max_wait_time: 最长等待时间,单位:s 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    :return: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    """ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    global health_instances 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    start_time = time.time() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    ip_address = utils.get_ip_address(client=client, instance_id=instance_id) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    while True: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        health_check_url = f"http://{ip_address}:5001/healthcheck" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        try: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            http_code = requests.get(health_check_url).status_code 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        except: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            logging.info("images is downloading") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            http_code = 0 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if http_code == 200: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            health_instances.append((instance_id, ip_address)) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            logging.info(f"health check success, instance: {instance_id}/{ip_address}") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            break 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        elif max_wait_time is not None: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            now = time.time() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            if (now - start_time) >= max_wait_time: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                logging.info(f"health check error, instance: {instance_id}/{ip_address}") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                break 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            else: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                time.sleep(10) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        else: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            time.sleep(10) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+def server_health_check(client, instance_id): 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    """ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    服务健康检查 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    :param client: 客户端连接 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    :param instance_id: instanceId 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    :return: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    """ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    global health_instances 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    ip_address = utils.get_ip_address(client=client, instance_id=instance_id) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    while True: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        health_check_url = f"http://{ip_address}:5001/healthcheck" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        try: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            http_code = requests.get(health_check_url).status_code 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        except: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            logging.info("images is downloading") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            http_code = 0 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if http_code == 200: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            health_instances.append((instance_id, ip_address)) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            break 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        else: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            # time.sleep(20) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            await asyncio.sleep(10) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 async def ess_instance(create_client, slb_client, ess_count, max_workers): 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     """ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     扩容机器并运行新服务 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -45,7 +105,7 @@ async def ess_instance(create_client, slb_client, ess_count, max_workers): 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     loop = asyncio.get_running_loop() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     executor = ThreadPoolExecutor(max_workers=max_workers) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     tasks = [ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        loop.run_in_executor(executor, utils.rov_server_health_check, *args) for args in 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        loop.run_in_executor(executor, rov_server_health_check, *args) for args in 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         [(slb_client, instance_id, max_wait_time) for instance_id in ess_instance_ids] 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     ] 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     await asyncio.wait(tasks) 
			 |