fix: KingClient 添加日志2

This commit is contained in:
caoqianming 2024-06-19 18:13:48 +08:00
parent c42dafcb44
commit 98d10405e4
1 changed files with 14 additions and 10 deletions

View File

@ -35,19 +35,22 @@ class KingClient(HandleLogMixin):
# is_ok, _ = self.request(**kapis['heartbeat'], raise_exception=False, timeout=10) # is_ok, _ = self.request(**kapis['heartbeat'], raise_exception=False, timeout=10)
# if is_ok == 'success': # if is_ok == 'success':
# return # return
current_val = cache.get('king_token') current_val:str = cache.get('king_token')
is_ok = 'success'
if current_val != 'requesting': if current_val != 'requesting':
json = { json = {
'username': settings.KING_USERNAME, 'username': settings.KING_USERNAME,
'password': settings.KING_PASSWORD 'password': settings.KING_PASSWORD
} }
try:
cache.set('king_token', 'requesting', timeout=None) cache.set('king_token', 'requesting', timeout=None)
_, res = self.request( is_ok, res = self.request(
**kapis['login'], json=json, timeout=10, need_auth=False) **kapis['login'], json=json, timeout=10, need_auth=False, raise_exception=False)
cache.set('king_token', res['Authorization'], timeout=None) if is_ok == "success":
except Exception: current_val = res['Authorization']
cache.set('king_token', current_val, timeout=None) cache.set('king_token', current_val, timeout=None)
return is_ok, current_val
def request(self, url: str, method: str = 'post', params=dict(), json=dict(), timeout=20, raise_exception=True, need_auth=True): def request(self, url: str, method: str = 'post', params=dict(), json=dict(), timeout=20, raise_exception=True, need_auth=True):
if not self.king_enabled: if not self.king_enabled:
@ -74,9 +77,10 @@ class KingClient(HandleLogMixin):
ret = r.json() ret = r.json()
if 'code' in ret: if 'code' in ret:
if ret['code'] != 0: if ret['code'] != 0:
if need_auth and ret['code'] in [-1, -2]: # 认证失败 if need_auth and ret['code'] in [-1, -2] and ret['message'] != 'failed': # 认证失败
myLogger.error(f"亚控认证失败: {url}-{ret}") myLogger.error(f"亚控认证失败: {url}-{ret}")
self._get_token() is_ok, _ = self._get_token()
if is_ok:
return self.request(url, method, params, json, timeout, raise_exception, need_auth) return self.request(url, method, params, json, timeout, raise_exception, need_auth)
err_detail = dict(detail=f"亚控错误: {ret['message']}", err_detail = dict(detail=f"亚控错误: {ret['message']}",