Commit b0eae044 authored by 高雅喆's avatar 高雅喆

增加数据库丢失连接保护

parent a14b8824
...@@ -8,6 +8,7 @@ import traceback ...@@ -8,6 +8,7 @@ import traceback
from libs.cache import redis_client from libs.cache import redis_client
import json import json
import logging import logging
from django.db import connection
def tzlc(dt, truncate_to_sec=True): def tzlc(dt, truncate_to_sec=True):
...@@ -58,3 +59,12 @@ def get_have_read_lin_pictorial_id_list(device_id,user_id,query_type): ...@@ -58,3 +59,12 @@ def get_have_read_lin_pictorial_id_list(device_id,user_id,query_type):
logging.error("catch exception,err_msg:%s" % traceback.format_exc()) logging.error("catch exception,err_msg:%s" % traceback.format_exc())
return list() return list()
def is_connection_usable():
"""判断当前mysql的链接是否正常,不正常就close掉"""
try:
connection.connection.ping()
return True
except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc())
return False
...@@ -11,9 +11,11 @@ from libs.cache import redis_client ...@@ -11,9 +11,11 @@ from libs.cache import redis_client
from search.utils.common import * from search.utils.common import *
from trans2es.models.tag import TopicTag, AccountUserTag, CommunityTagFollow, Tag from trans2es.models.tag import TopicTag, AccountUserTag, CommunityTagFollow, Tag
import time import time
from libs.tools import is_connection_usable
from trans2es.models.tag import CommunityTagSetRelation from trans2es.models.tag import CommunityTagSetRelation
from django.conf import settings from django.conf import settings
from libs.error import logging_exception from libs.error import logging_exception
from django.db import connection
def get_highlight(fields=[]): def get_highlight(fields=[]):
...@@ -235,14 +237,15 @@ def get_same_tagset_ids(tag_list): ...@@ -235,14 +237,15 @@ def get_same_tagset_ids(tag_list):
:param tag_list: :param tag_list:
:return: :return:
""" """
all_tag = list()
if isinstance(tag_list, int):
all_tag.append(tag_list)
else:
all_tag = tag_list
try: try:
all_tag = list() if not is_connection_usable():
if isinstance(tag_list, int): connection.close()
all_tag.append(tag_list) logging.error("unable mysql connection and close")
else:
all_tag = tag_list
tag_set_list_id = list( tag_set_list_id = list(
CommunityTagSetRelation.objects.filter(tag_id__in=all_tag, is_deleted=False).values_list("tag_set_id", CommunityTagSetRelation.objects.filter(tag_id__in=all_tag, is_deleted=False).values_list("tag_set_id",
flat=True)) flat=True))
...@@ -250,12 +253,10 @@ def get_same_tagset_ids(tag_list): ...@@ -250,12 +253,10 @@ def get_same_tagset_ids(tag_list):
CommunityTagSetRelation.objects.filter(tag_set_id__in=tag_set_list_id, is_deleted=False).values_list( CommunityTagSetRelation.objects.filter(tag_set_id__in=tag_set_list_id, is_deleted=False).values_list(
"tag_id", "tag_id",
flat=True)) flat=True))
all_tag.extend(tag_ids) all_tag.extend(tag_ids)
logging.info("get_same_tagset_ids:%s" % str(all_tag)) logging.info("get_same_tagset_ids:%s" % str(all_tag))
return list(set(all_tag)) return list(set(all_tag))
except: except:
logging_exception() logging_exception()
logging.error("catch exception,err_msg:%s" % traceback.format_exc()) logging.error("catch exception,err_msg:%s" % traceback.format_exc())
return [] return all_tag
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment