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

增加数据库丢失连接保护

parent a14b8824
......@@ -8,6 +8,7 @@ import traceback
from libs.cache import redis_client
import json
import logging
from django.db import connection
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):
logging.error("catch exception,err_msg:%s" % traceback.format_exc())
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
from search.utils.common import *
from trans2es.models.tag import TopicTag, AccountUserTag, CommunityTagFollow, Tag
import time
from libs.tools import is_connection_usable
from trans2es.models.tag import CommunityTagSetRelation
from django.conf import settings
from libs.error import logging_exception
from django.db import connection
def get_highlight(fields=[]):
......@@ -235,14 +237,15 @@ def get_same_tagset_ids(tag_list):
:param tag_list:
:return:
"""
try:
all_tag = list()
if isinstance(tag_list, int):
all_tag.append(tag_list)
else:
all_tag = tag_list
try:
if not is_connection_usable():
connection.close()
logging.error("unable mysql connection and close")
tag_set_list_id = list(
CommunityTagSetRelation.objects.filter(tag_id__in=all_tag, is_deleted=False).values_list("tag_set_id",
flat=True))
......@@ -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(
"tag_id",
flat=True))
all_tag.extend(tag_ids)
logging.info("get_same_tagset_ids:%s" % str(all_tag))
return list(set(all_tag))
except:
logging_exception()
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