diff --git a/sysom_api/apps/vul/views.py b/sysom_api/apps/vul/views.py index 1d94b719ef030510a3a1418259a36abf09ccbc49..63181aa1b532bcbe10483d1df8318ef0eba56c74 100644 --- a/sysom_api/apps/vul/views.py +++ b/sysom_api/apps/vul/views.py @@ -274,10 +274,6 @@ class SaFixHistDetailHostView(APIView): return success(result=data) -# 因为sysom未使用redis等缓存,所以使用全局变量,在内存中存储上次更新时间 -LAST_UPDATE_SA_TIME = None - - class UpdateSaView(APIView): authentication_classes = [Authentication] @@ -285,21 +281,24 @@ class UpdateSaView(APIView): """ 检测最近更新时间,如果小于时间间隔,则直接返回成功 """ - global LAST_UPDATE_SA_TIME - if LAST_UPDATE_SA_TIME is None: + try: + last_update_sa_time = VulJobModel.objects.filter(job_name="update_sa").order_by( + '-job_end_time').first().job_end_time + except AttributeError: + last_update_sa_time = None + + if last_update_sa_time is None: upsa() - LAST_UPDATE_SA_TIME = time.time() return success(result="Update security advisory data") else: # 默认间隔时间为10分 interval_time = 60 * 10 - current_time = time.time() - if current_time - LAST_UPDATE_SA_TIME < interval_time: + current_time = localtime() + if (current_time - last_update_sa_time).seconds < interval_time: return success(message="forbidden", result="The data has been updated recently,no need to update it again") else: upsa() - LAST_UPDATE_SA_TIME = time.time() return success(result="Update security advisory data")