Skip to content

Commit

Permalink
fix: 订阅实例属性is_latest为False导致巡检未执行 (closed TencentBlueKing#2189)
Browse files Browse the repository at this point in the history
  • Loading branch information
Huayeaaa committed May 10, 2024
1 parent 921636f commit 40edb92
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion apps/backend/subscription/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 _

Expand Down Expand Up @@ -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(
Expand All @@ -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<sub(%s), task(%s)>][run_subscription_task_and_create_instance] "
Expand All @@ -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<sub(%s), task(%s)>][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)

Expand Down

0 comments on commit 40edb92

Please sign in to comment.