From 40edb92832832771fd7c0d1d53883c6cc22cefea Mon Sep 17 00:00:00 2001 From: dcd <1151627903@qq.com> Date: Tue, 30 Apr 2024 18:59:11 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=AE=A2=E9=98=85=E5=AE=9E=E4=BE=8B?= =?UTF-8?q?=E5=B1=9E=E6=80=A7is=5Flatest=E4=B8=BAFalse=E5=AF=BC=E8=87=B4?= =?UTF-8?q?=E5=B7=A1=E6=A3=80=E6=9C=AA=E6=89=A7=E8=A1=8C=20(closed=20#2189?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/backend/subscription/tasks.py | 31 +++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/apps/backend/subscription/tasks.py b/apps/backend/subscription/tasks.py index 590bbf652..3fe25c781 100644 --- a/apps/backend/subscription/tasks.py +++ b/apps/backend/subscription/tasks.py @@ -16,7 +16,7 @@ from collections import OrderedDict, defaultdict from copy import deepcopy from functools import wraps -from typing import Any, Dict, List, Optional, Union +from typing import Any, Dict, List, Optional, Set, Union from django.utils.translation import ugettext as _ @@ -710,6 +710,7 @@ def run_subscription_task_and_create_instance( # 如果被删掉的实例在 CMDB 找不到,那么就使用最近一次的 InstanceRecord 的快照数据 not_exist_instance_id = set(instance_not_in_scope) - set(deleted_instance_info) + not_exist_instance_id_list = [] if not_exist_instance_id: records = list( models.SubscriptionInstanceRecord.objects.filter( @@ -718,6 +719,7 @@ def run_subscription_task_and_create_instance( ) for record in records: deleted_instance_info[record.instance_id] = record.instance_info + not_exist_instance_id_list.append(record.instance_id) logger.info( "[sub_lifecycle][run_subscription_task_and_create_instance] " @@ -727,6 +729,33 @@ def run_subscription_task_and_create_instance( not_exist_instance_id, len(records), ) + is_latest_false_instance_ids: Set[str] = not_exist_instance_id - set(not_exist_instance_id_list) + if is_latest_false_instance_ids: + sub_inst_ids_gby_instance_id = defaultdict(list) + max_sub_inst_id_list = [] + subs_inst_records = models.SubscriptionInstanceRecord.objects.filter( + subscription_id=subscription.id, instance_id__in=is_latest_false_instance_ids + ).values("id", "instance_id") + for sub_inst_record_dict in subs_inst_records: + sub_inst_ids_gby_instance_id[sub_inst_record_dict["instance_id"]].append(sub_inst_record_dict["id"]) + + for sub_inst_record_id_list in sub_inst_ids_gby_instance_id.values(): + max_sub_inst_id_list.append(max(sub_inst_record_id_list)) + + subs_inst_records_qs = models.SubscriptionInstanceRecord.objects.filter(id__in=max_sub_inst_id_list).values( + "instance_id", "instance_info" + ) + for sub_inst_record_info in subs_inst_records_qs: + deleted_instance_info[sub_inst_record_info["instance_id"]] = sub_inst_record_info["instance_info"] + + logger.info( + "[sub_lifecycle][run_subscription_task_and_create_instance] " + "deleted instances not exist in cc, find latest record from db -> %s, find num -> %s", + subscription.id, + subscription_task.id, + is_latest_false_instance_ids, + len(subs_inst_records_qs), + ) instances.update(deleted_instance_info)