Commit 81fd2288 authored by jinkaiqiang's avatar jinkaiqiang

新增超时检测装饰器和应用修改修复bug

parent 9c48f7cf
Pipeline #25159 passed with stage
in 22 seconds
......@@ -4,10 +4,11 @@ import os
import re
import time
from func_timeout import func_set_timeout
from git import GitCommandError
from app.conncetion.conncetion_service import ConnectionService
from app.utils.common_tools import CommonTools
from app.utils.common_tools import CommonTools, time_out
from app.utils.db_tools import db_driver
from flask_app import git_path, repo
from huansi_utils.exception.exception import HSException
......@@ -118,13 +119,13 @@ where B.default_version=1'''
with open(download_log_path, 'a+') as f:
log_content = f.read()
log_content = f"{log_content}{time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))}===>[{app_list_str}]"
log_content = f"{log_content}{time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))}===>[{app_list_str}]"
with open(download_log_path, 'w', encoding='utf8') as f:
f.write(log_content)
repo.git.add([self.compose_path, download_log_path])
repo.git.commit(f"-m {time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))}===>[{app_list_str}]")
repo.git.commit(f"-m {time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))}===>[{app_list_str}]")
repo.git.push()
def get_port_list(self, project_no):
......@@ -196,20 +197,19 @@ where B.default_version=1'''
info_dict = {}
info_dict['port'] = port_info['port']
info_dict['app_code'] = port_info['app_code']
try:
self.test_connect_port(host_ip, port_info['port'])
if self.test_connect_port(host_ip, port_info['port']):
info_dict['status'] = '测试通过'
info_list_success.append(info_dict)
except Exception:
else:
info_dict['status'] = '测试失败'
info_list_fail.append(info_dict)
# info_list.append(info_dict)
info_list = info_list_fail + info_list_success
return info_list
import timeout_decorator
# 超过两秒,基本上没救了,告辞~~~~
@timeout_decorator.timeout(2)
@time_out
@func_set_timeout(2)
def test_connect_port(self, ip, port):
'''
检查端口是否开放
......@@ -224,5 +224,6 @@ where B.default_version=1'''
try:
sk.connect((ip, int(port)))
sk.shutdown(2)
return True
finally:
sk.close()
......@@ -454,7 +454,7 @@ docker-compose -f /data/upgrade_tools_data/backup/{upgrade_no}/docker-compose.ym
out, err = ssh.exec_command(
f'''export HUANSI_REGISTRY_URL=47.110.145.204:8084 &&\
source /etc/profile.d/huansi.sh &&\
docker-compose -f /data/upgrade_tools_data/backup/{upgrade_no}/docker-compose.yml -p deploy restart -t 100''')
docker-compose -f /data/upgrade_tools_data/backup/{upgrade_no}/docker-compose.yml -p deploy up -d --force-recreate''')
logger.info(f'应用操作信息:{out},其他信息:{err}')
logger.info("应用修改成功")
......
# -*- coding:utf-8 -*-
import os
import timeout_decorator
from func_timeout import func_set_timeout, FunctionTimedOut
from huansi_utils.app.apploader import logger
from huansi_utils.exception.exception import HSException
def time_out(fn, *args, **kwargs):
def wrapper(*args, **kwargs):
try:
result = fn(*args, **kwargs)
return result
except FunctionTimedOut:
print('timeout')
return False
return wrapper
class CommonTools:
@classmethod
def validate_dict_value_all_not_none(cls, dict_data: dict, mapping=None):
......@@ -23,6 +35,8 @@ class CommonTools:
# 超过四秒,基本上没救了,告辞~~~~
# @timeout_decorator.timeout(2)
@time_out
@func_set_timeout(2)
def test_db_connection(self, ip: int, port: int, user_name: str, password: str, db_name: str):
'''
测试数据库连接
......@@ -56,7 +70,8 @@ class CommonTools:
return False
except Exception as e:
if isinstance(e, timeout_decorator.TimeoutError):
from func_timeout import FunctionTimedOut
if isinstance(e, FunctionTimedOut):
logger.error("数据库连接失败,失败原因:{}".format(e))
return False
orig = getattr(e, 'orig')
......
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