retry test
This commit is contained in:
parent
273832c057
commit
486895778e
4 changed files with 23 additions and 6 deletions
|
@ -5,7 +5,8 @@ import hmac
|
||||||
import json
|
import json
|
||||||
import time
|
import time
|
||||||
|
|
||||||
from aiohttp_retry import RetryClient, ExponentialRetry
|
import aiohttp
|
||||||
|
from tenacity import retry, wait_fixed, stop_after_attempt
|
||||||
|
|
||||||
from config import Config
|
from config import Config
|
||||||
|
|
||||||
|
@ -21,6 +22,7 @@ def computeMD5hash(my_string):
|
||||||
return m.hexdigest()
|
return m.hexdigest()
|
||||||
|
|
||||||
|
|
||||||
|
@retry(stop=stop_after_attempt(3), wait=wait_fixed(3))
|
||||||
async def check(*text):
|
async def check(*text):
|
||||||
accessKeyId = Config("Check_accessKeyId")
|
accessKeyId = Config("Check_accessKeyId")
|
||||||
accessKeySecret = Config("Check_accessKeySecret")
|
accessKeySecret = Config("Check_accessKeySecret")
|
||||||
|
@ -68,7 +70,7 @@ async def check(*text):
|
||||||
sign = "acs {}:{}".format(accessKeyId, hash_hmac(accessKeySecret, step3, hashlib.sha1))
|
sign = "acs {}:{}".format(accessKeyId, hash_hmac(accessKeySecret, step3, hashlib.sha1))
|
||||||
headers['Authorization'] = sign
|
headers['Authorization'] = sign
|
||||||
# 'Authorization': "acs {}:{}".format(accessKeyId, sign)
|
# 'Authorization': "acs {}:{}".format(accessKeyId, sign)
|
||||||
async with RetryClient(headers=headers, retry_options=ExponentialRetry(attempts=5)) as session:
|
async with aiohttp.ClientSession(headers=headers) as session:
|
||||||
async with session.post('{}{}'.format(root, url), data=json.dumps(body)) as resp:
|
async with session.post('{}{}'.format(root, url), data=json.dumps(body)) as resp:
|
||||||
if resp.status == 200:
|
if resp.status == 200:
|
||||||
result = await resp.json()
|
result = await resp.json()
|
||||||
|
|
|
@ -6,6 +6,7 @@ from database.orm import DBSession
|
||||||
from database.tables import EnabledModules, SenderInfo, TargetAdmin, CommandTriggerTime
|
from database.tables import EnabledModules, SenderInfo, TargetAdmin, CommandTriggerTime
|
||||||
from config import Config
|
from config import Config
|
||||||
|
|
||||||
|
from tenacity import retry, stop_after_attempt
|
||||||
|
|
||||||
cache = Config('db_cache')
|
cache = Config('db_cache')
|
||||||
|
|
||||||
|
@ -63,6 +64,7 @@ class BotDBUtil:
|
||||||
def check_target_enabled_module(self, module_name) -> bool:
|
def check_target_enabled_module(self, module_name) -> bool:
|
||||||
return True if module_name in self.enable_modules_list else False
|
return True if module_name in self.enable_modules_list else False
|
||||||
|
|
||||||
|
@retry(stop=stop_after_attempt(3))
|
||||||
def enable(self, module_name) -> bool:
|
def enable(self, module_name) -> bool:
|
||||||
try:
|
try:
|
||||||
if isinstance(module_name, str):
|
if isinstance(module_name, str):
|
||||||
|
@ -88,6 +90,7 @@ class BotDBUtil:
|
||||||
session.rollback()
|
session.rollback()
|
||||||
raise
|
raise
|
||||||
|
|
||||||
|
@retry(stop=stop_after_attempt(3))
|
||||||
def disable(self, module_name) -> bool:
|
def disable(self, module_name) -> bool:
|
||||||
try:
|
try:
|
||||||
if isinstance(module_name, str):
|
if isinstance(module_name, str):
|
||||||
|
@ -119,6 +122,7 @@ class BotDBUtil:
|
||||||
return targetIds
|
return targetIds
|
||||||
|
|
||||||
class SenderInfo:
|
class SenderInfo:
|
||||||
|
@retry(stop=stop_after_attempt(3))
|
||||||
def __init__(self, senderId):
|
def __init__(self, senderId):
|
||||||
self.senderId = senderId
|
self.senderId = senderId
|
||||||
query_cache = SenderInfoCache.get_cache(self.senderId) if cache else False
|
query_cache = SenderInfoCache.get_cache(self.senderId) if cache else False
|
||||||
|
@ -141,6 +145,7 @@ class BotDBUtil:
|
||||||
def query_SenderInfo(self):
|
def query_SenderInfo(self):
|
||||||
return session.query(SenderInfo).filter_by(id=self.senderId).first()
|
return session.query(SenderInfo).filter_by(id=self.senderId).first()
|
||||||
|
|
||||||
|
@retry(stop=stop_after_attempt(3))
|
||||||
def edit(self, column: str, value):
|
def edit(self, column: str, value):
|
||||||
try:
|
try:
|
||||||
query = self.query_SenderInfo
|
query = self.query_SenderInfo
|
||||||
|
@ -160,6 +165,7 @@ class BotDBUtil:
|
||||||
return query
|
return query
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
@retry(stop=stop_after_attempt(3))
|
||||||
def add_TargetAdmin(self, targetId):
|
def add_TargetAdmin(self, targetId):
|
||||||
try:
|
try:
|
||||||
if not self.check_TargetAdmin(targetId):
|
if not self.check_TargetAdmin(targetId):
|
||||||
|
@ -170,6 +176,7 @@ class BotDBUtil:
|
||||||
session.rollback()
|
session.rollback()
|
||||||
raise
|
raise
|
||||||
|
|
||||||
|
@retry(stop=stop_after_attempt(3))
|
||||||
def remove_TargetAdmin(self, targetId):
|
def remove_TargetAdmin(self, targetId):
|
||||||
try:
|
try:
|
||||||
query = self.check_TargetAdmin(targetId)
|
query = self.check_TargetAdmin(targetId)
|
||||||
|
@ -180,7 +187,6 @@ class BotDBUtil:
|
||||||
session.rollback()
|
session.rollback()
|
||||||
raise
|
raise
|
||||||
|
|
||||||
|
|
||||||
class CoolDown:
|
class CoolDown:
|
||||||
def __init__(self, msg: MessageSession, name):
|
def __init__(self, msg: MessageSession, name):
|
||||||
self.msg = msg
|
self.msg = msg
|
||||||
|
@ -197,6 +203,7 @@ class BotDBUtil:
|
||||||
return now
|
return now
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
@retry(stop=stop_after_attempt(3))
|
||||||
def reset(self):
|
def reset(self):
|
||||||
try:
|
try:
|
||||||
if not self.need_insert:
|
if not self.need_insert:
|
||||||
|
@ -206,4 +213,4 @@ class BotDBUtil:
|
||||||
session.commit()
|
session.commit()
|
||||||
except Exception:
|
except Exception:
|
||||||
session.rollback()
|
session.rollback()
|
||||||
raise
|
raise
|
||||||
|
|
|
@ -4,10 +4,13 @@ from core.elements import MessageSession
|
||||||
from database.orm import DBSession
|
from database.orm import DBSession
|
||||||
from .orm import WikiTargetSetInfo, WikiInfo
|
from .orm import WikiTargetSetInfo, WikiInfo
|
||||||
|
|
||||||
|
from tenacity import retry, stop_after_attempt
|
||||||
|
|
||||||
session = DBSession().session
|
session = DBSession().session
|
||||||
|
|
||||||
|
|
||||||
class WikiTargetInfo:
|
class WikiTargetInfo:
|
||||||
|
@retry(stop=stop_after_attempt(3))
|
||||||
def __init__(self, msg: [MessageSession, str]):
|
def __init__(self, msg: [MessageSession, str]):
|
||||||
if isinstance(msg, MessageSession):
|
if isinstance(msg, MessageSession):
|
||||||
targetId = msg.target.targetId
|
targetId = msg.target.targetId
|
||||||
|
@ -23,6 +26,7 @@ class WikiTargetInfo:
|
||||||
raise
|
raise
|
||||||
self.query = session.query(WikiTargetSetInfo).filter_by(targetId=targetId).first()
|
self.query = session.query(WikiTargetSetInfo).filter_by(targetId=targetId).first()
|
||||||
|
|
||||||
|
@retry(stop=stop_after_attempt(3))
|
||||||
def add_start_wiki(self, url):
|
def add_start_wiki(self, url):
|
||||||
try:
|
try:
|
||||||
self.query.link = url
|
self.query.link = url
|
||||||
|
@ -38,6 +42,7 @@ class WikiTargetInfo:
|
||||||
return self.query.link if self.query.link is not None else False
|
return self.query.link if self.query.link is not None else False
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
@retry(stop=stop_after_attempt(3))
|
||||||
def config_interwikis(self, iw: str, iwlink: str = None, let_it=True):
|
def config_interwikis(self, iw: str, iwlink: str = None, let_it=True):
|
||||||
try:
|
try:
|
||||||
interwikis = json.loads(self.query.iws)
|
interwikis = json.loads(self.query.iws)
|
||||||
|
@ -62,6 +67,7 @@ class WikiTargetInfo:
|
||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
@retry(stop=stop_after_attempt(3))
|
||||||
def config_headers(self, headers, let_it: [bool, None] = True):
|
def config_headers(self, headers, let_it: [bool, None] = True):
|
||||||
try:
|
try:
|
||||||
headers = json.loads(headers)
|
headers = json.loads(headers)
|
||||||
|
@ -101,6 +107,7 @@ class WikiSiteInfo:
|
||||||
return self.query.siteInfo, self.query.timestamp
|
return self.query.siteInfo, self.query.timestamp
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
@retry(stop=stop_after_attempt(3))
|
||||||
def update(self, info: dict):
|
def update(self, info: dict):
|
||||||
try:
|
try:
|
||||||
if self.query is None:
|
if self.query is None:
|
||||||
|
@ -111,4 +118,4 @@ class WikiSiteInfo:
|
||||||
return True
|
return True
|
||||||
except Exception:
|
except Exception:
|
||||||
session.rollback()
|
session.rollback()
|
||||||
raise
|
raise
|
||||||
|
|
|
@ -19,4 +19,5 @@ aiogram
|
||||||
aiohttp_retry
|
aiohttp_retry
|
||||||
ujson
|
ujson
|
||||||
pymysql
|
pymysql
|
||||||
discord.py
|
discord.py
|
||||||
|
tenacity
|
Reference in a new issue