diff --git a/dongtai_conf/settings.py b/dongtai_conf/settings.py index ca712e89..b314b027 100644 --- a/dongtai_conf/settings.py +++ b/dongtai_conf/settings.py @@ -1003,7 +1003,7 @@ def safe_execute(default, exception, function, *args): try: TANTIVY_INDEX_PATH = config.get("tantivy", "index_path") except Exception: - TANTIVY_INDEX_PATH = urljoin(TMP_COMMON_PATH, "tantivy") + TANTIVY_INDEX_PATH = os.path.join(TMP_COMMON_PATH, "tantivy") ELASTICSEARCH_STATE = config.get("elastic_search", "enable") == "true" diff --git a/dongtai_web/aggr_vul/app_vul_list.py b/dongtai_web/aggr_vul/app_vul_list.py index 36799253..08c6005d 100644 --- a/dongtai_web/aggr_vul/app_vul_list.py +++ b/dongtai_web/aggr_vul/app_vul_list.py @@ -220,12 +220,12 @@ def post(self, request): es_query["order"] = order_type_desc + order_type if ELASTICSEARCH_STATE: vul_data = get_vul_list_from_elastic_search(projects, page=page, page_size=page_size, **es_query) - if TANTIVY_STATE: + if TANTIVY_STATE and keywords: ids = set() id_score = [] tantivy_query_str = "" for key, value in tantivy_query.items(): - if isinstance(value, str): + if isinstance(value, (str, int)): tantivy_query_str += f'+{key}:"{value}" ' elif isinstance(value, list): tantivy_query_str += f"+{key}: IN [{' '.join(map(str, value))}] " diff --git a/dongtai_web/aggr_vul/tasks.py b/dongtai_web/aggr_vul/tasks.py index bc1cced0..398f4654 100644 --- a/dongtai_web/aggr_vul/tasks.py +++ b/dongtai_web/aggr_vul/tasks.py @@ -1,12 +1,31 @@ +import logging + import tantivy from celery import shared_task +from celery_singleton import Singleton +from django.db.models.signals import m2m_changed, post_delete, post_save +from django.dispatch import receiver from dongtai_common.models.vulnerablity import IastVulnerabilityModel, tantivy_index from dongtai_conf.settings import TANTIVY_STATE +logger = logging.getLogger("dongtai-core") + + +@receiver(post_delete, sender=IastVulnerabilityModel, dispatch_uid="update_vul_tantivy_index") +@receiver(post_save, sender=IastVulnerabilityModel, dispatch_uid="update_vul_tantivy_index") +@receiver(m2m_changed, sender=IastVulnerabilityModel, dispatch_uid="update_vul_tantivy_index") +def update_vul_tantivy_index_receiver(sender, instance, **kwargs): + update_vul_tantivy_index.apply_async(countdown=120) -@shared_task(queue="dongtai-periodic-task") + +@shared_task( + queue="dongtai-periodic-task", + base=Singleton, + lock_expiry=20, +) def update_vul_tantivy_index(): + logger.info("Start update vul tantivy index") if not TANTIVY_STATE: return index = tantivy_index() @@ -46,3 +65,5 @@ def update_vul_tantivy_index(): ) writer.commit() + + logger.info("Update vul tantivy index success!")