소스 검색

update weight_list

xuekailun 5 달 전
부모
커밋
4e72f4dff4
1개의 변경된 파일41개의 추가작업 그리고 19개의 파일을 삭제
  1. 41 19
      longvideoapi/longvideo_reduce_with_count.py

+ 41 - 19
longvideoapi/longvideo_reduce_with_count.py

@@ -12,24 +12,45 @@ logging.basicConfig(level=logging.INFO,
 def remove_instances(create_client, alb_client, instance_ids):
     """
     停止并释放机器
-    :param create_client:
-    :param alb_client:
-    :param instance_ids: instanceId type-list
+    :param create_client: 创建客户端
+    :param alb_client: ALB 客户端
+    :param instance_ids: instanceId 类型列表
     :return: None
     """
-    # 1. 摘除 ALB
-    for server_group_id in longvideo_config.server_group_id_list:
-        utils.remove_servers_from_server_group(alb_client=alb_client, server_group_id=server_group_id,
-                                               instance_id=instance_ids)
-    logging.info(f"remove from ALB finished, instances: {instance_ids}")
+    # 1. 摘除流量
+    weight_list = [(0, 10)]  # 设置权重为0,等待10秒
+    try:
+        utils.set_instance_weight_process_with_alb(alb_client,
+                                                   longvideo_config.server_group_id_list,
+                                                   instance_ids,
+                                                   weight_list)
+    except Exception as e:
+        logging.error(f"Failed to set instance weight: {e}")
+        sys.exit()
 
+    # 等待 ALB 更新权重
     time.sleep(10)
 
-    # 2. 停止机器
-    utils.stop_instances(client=create_client, instance_ids=instance_ids)
-    logging.info(f"instances stop finished, instances: {instance_ids}")
+    # 2. 从 ALB 服务器组中移除实例
+    for server_group_id in longvideo_config.server_group_id_list:
+        try:
+            utils.remove_servers_from_server_group(alb_client=alb_client, server_group_id=server_group_id,
+                                                   instance_id=instance_ids)
+            logging.info(f"Successfully removed instances from server group {server_group_id}.")
+        except Exception as e:
+            logging.error(f"Failed to remove instances from server group {server_group_id}: {e}")
+
+    logging.info(f"Remove from ALB finished, instances: {instance_ids}")
+
+    # 3. 停止机器
+    stop_response = utils.stop_instances(client=create_client, instance_ids=instance_ids)
+    if stop_response.get('Code') is None:
+        logging.info(f"Instances stop finished, instances: {instance_ids}")
+    else:
+        logging.error(f"Failed to stop instances: {stop_response}")
+        sys.exit()
 
-    # 3. 判断机器运行状态是否为 Stopped
+    # 4. 判断机器运行状态是否为 Stopped
     while True:
         response = utils.get_instances_status(client=create_client, instance_ids=instance_ids)
         if response.get('Code') is None:
@@ -37,21 +58,21 @@ def remove_instances(create_client, alb_client, instance_ids):
             stopped_instances = [instance.get('InstanceId') for instance in instances_list if
                                  instance.get('Status') == 'Stopped']
             if len(stopped_instances) == len(instance_ids):
-                logging.info(f"instances stopped status set success, instances: {stopped_instances}")
+                logging.info(f"Instances stopped status set success, instances: {stopped_instances}")
                 break
             else:
-                logging.info(f"stopped instances count = {len(stopped_instances)}, instances: {stopped_instances}")
+                logging.info(f"Stopped instances count = {len(stopped_instances)}, instances: {stopped_instances}")
                 time.sleep(5)
         else:
             logging.error(response)
             sys.exit()
 
-    # 4. 释放机器
-    response = utils.release_instances(client=create_client, instance_ids=stopped_instances)
-    if response.get('Code') is None:
-        logging.info(f"release instances finished, instances: {stopped_instances}")
+    # 5. 释放机器
+    release_response = utils.release_instances(client=create_client, instance_ids=stopped_instances)
+    if release_response.get('Code') is None:
+        logging.info(f"Release instances finished, instances: {stopped_instances}")
     else:
-        logging.error(f"release instances fail!!!")
+        logging.error(f"Release instances fail!!!")
         sys.exit()
 
 
@@ -92,3 +113,4 @@ def main():
 
 if __name__ == '__main__':
     main()
+