Przeglądaj źródła

update add_servers_to_server_group

xuekailun 4 miesięcy temu
rodzic
commit
be7d59e267

+ 5 - 2
longvideoapi/longvideoapi_scaling_k_count.py

@@ -88,9 +88,12 @@ async def ess_instance(ecs_client, alb_client, ess_count, max_workers, port):
     if len(health_instances) > 0:
         # 所有机器探活成功
         time.sleep(20)
+        # instance_id_list = []
         # for instance_id, ip in health_instances:
-        #     for server_group_id in longvideoapi_config.server_group_id_list:
-        utils.add_servers_to_server_group(alb_client, longvideoapi_config.server_group_id_list, health_instances, weight=100, port=port)
+        #     instance_id_list.append(instance_id)
+        # #     for server_group_id in longvideoapi_config.server_group_id_list:
+        health_instance_ids = [instance_id for instance_id, _ in health_instances]
+        utils.add_servers_to_server_group(alb_client, longvideoapi_config.server_group_id_list, health_instance_ids, weight=100, port=port)
         logging.info(f"ess count: {ess_count}, "
                      f"create count: {len(ess_instance_ids)}, "
                      f"finished count: {len(health_instances)}")

+ 2 - 47
longvideoapi/utils.py

@@ -384,9 +384,9 @@ def add_servers_to_server_group(alb_client, server_group_ids, instance_ids, weig
         runtime = util_models.RuntimeOptions()
         try:
             alb_client.add_servers_to_server_group_with_options(request, runtime)
-            logging.info(f"Successfully added servers {servers} to server group {server_group_id} with weight {weight}.")
+            logging.info(f"Successfully added servers {instance_ids} to server group {server_group_id} with weight {weight}.")
         except Exception as e:
-            logging.error(f"Failed to add servers {servers} to server group {server_group_id}: {str(e)}")
+            logging.error(f"Failed to add servers {instance_ids} to server group {server_group_id}: {str(e)}")
 
 
 
@@ -512,48 +512,3 @@ def update_server_group_servers_attribute(alb_client, server_group_id_list, inst
     time.sleep(sleep_time)
 
 
-
-
-async def update_server_group_server_weight_async(alb_client, server_group_id, instance_id, weight, port):
-    """
-    异步更新特定服务器在服务器组中的权重
-    :param alb_client: ALB客户端
-    :param server_group_id: 服务器组ID
-    :param instance_id: 实例ID
-    :param weight: 权重值
-    :param port: 后端服务器使用的端口
-    """
-    server = alb_20200616_models.UpdateServerGroupServersAttributeRequestServers(
-        server_type='Ecs',
-        server_id=instance_id,
-        weight=weight,
-        port=port
-    )
-    request = alb_20200616_models.UpdateServerGroupServersAttributeRequest(
-        servers=[server],
-        server_group_id=server_group_id
-    )
-    runtime = util_models.RuntimeOptions()
-    try:
-        await alb_client.update_server_group_servers_attribute_with_options_async(request, runtime)
-        print(f"Successfully updated server {instance_id} in group {server_group_id} to weight {weight} asynchronously.")
-    except Exception as error:
-        print(str(error))
-        UtilClient.assert_as_string(str(error))
-
-async def update_server_group_servers_attribute_async(alb_client, server_group_id_list, instance_ids, weight_list, port):
-    """
-    异步更新服务器组中的服务器属性
-    :param alb_client: ALB客户端
-    :param server_group_id_list: 服务器组ID列表
-    :param instance_ids: 实例ID列表
-    :param weight_list: 权重修改列表 type-list [(weight, sleep_time), ...]
-    :param port: 服务器端口
-    """
-    tasks = []
-    for server_group_id in server_group_id_list:
-        for instance_id in instance_ids:
-            for weight, sleep_time in weight_list:
-                tasks.append(update_server_group_server_weight_async(alb_client, server_group_id, instance_id, weight, port))
-                await asyncio.sleep(sleep_time)
-    await asyncio.gather(*tasks)