Archived
1
0
Fork 0

retry test

This commit is contained in:
yzhh 2021-09-12 20:51:13 +08:00
parent 273832c057
commit 486895778e
4 changed files with 23 additions and 6 deletions

View file

@ -5,7 +5,8 @@ import hmac
import json
import time
from aiohttp_retry import RetryClient, ExponentialRetry
import aiohttp
from tenacity import retry, wait_fixed, stop_after_attempt
from config import Config
@ -21,6 +22,7 @@ def computeMD5hash(my_string):
return m.hexdigest()
@retry(stop=stop_after_attempt(3), wait=wait_fixed(3))
async def check(*text):
accessKeyId = Config("Check_accessKeyId")
accessKeySecret = Config("Check_accessKeySecret")
@ -68,7 +70,7 @@ async def check(*text):
sign = "acs {}:{}".format(accessKeyId, hash_hmac(accessKeySecret, step3, hashlib.sha1))
headers['Authorization'] = 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:
if resp.status == 200:
result = await resp.json()

View file

@ -6,6 +6,7 @@ from database.orm import DBSession
from database.tables import EnabledModules, SenderInfo, TargetAdmin, CommandTriggerTime
from config import Config
from tenacity import retry, stop_after_attempt
cache = Config('db_cache')
@ -63,6 +64,7 @@ class BotDBUtil:
def check_target_enabled_module(self, module_name) -> bool:
return True if module_name in self.enable_modules_list else False
@retry(stop=stop_after_attempt(3))
def enable(self, module_name) -> bool:
try:
if isinstance(module_name, str):
@ -88,6 +90,7 @@ class BotDBUtil:
session.rollback()
raise
@retry(stop=stop_after_attempt(3))
def disable(self, module_name) -> bool:
try:
if isinstance(module_name, str):
@ -119,6 +122,7 @@ class BotDBUtil:
return targetIds
class SenderInfo:
@retry(stop=stop_after_attempt(3))
def __init__(self, senderId):
self.senderId = senderId
query_cache = SenderInfoCache.get_cache(self.senderId) if cache else False
@ -141,6 +145,7 @@ class BotDBUtil:
def query_SenderInfo(self):
return session.query(SenderInfo).filter_by(id=self.senderId).first()
@retry(stop=stop_after_attempt(3))
def edit(self, column: str, value):
try:
query = self.query_SenderInfo
@ -160,6 +165,7 @@ class BotDBUtil:
return query
return False
@retry(stop=stop_after_attempt(3))
def add_TargetAdmin(self, targetId):
try:
if not self.check_TargetAdmin(targetId):
@ -170,6 +176,7 @@ class BotDBUtil:
session.rollback()
raise
@retry(stop=stop_after_attempt(3))
def remove_TargetAdmin(self, targetId):
try:
query = self.check_TargetAdmin(targetId)
@ -180,7 +187,6 @@ class BotDBUtil:
session.rollback()
raise
class CoolDown:
def __init__(self, msg: MessageSession, name):
self.msg = msg
@ -197,6 +203,7 @@ class BotDBUtil:
return now
return 0
@retry(stop=stop_after_attempt(3))
def reset(self):
try:
if not self.need_insert:
@ -206,4 +213,4 @@ class BotDBUtil:
session.commit()
except Exception:
session.rollback()
raise
raise

View file

@ -4,10 +4,13 @@ from core.elements import MessageSession
from database.orm import DBSession
from .orm import WikiTargetSetInfo, WikiInfo
from tenacity import retry, stop_after_attempt
session = DBSession().session
class WikiTargetInfo:
@retry(stop=stop_after_attempt(3))
def __init__(self, msg: [MessageSession, str]):
if isinstance(msg, MessageSession):
targetId = msg.target.targetId
@ -23,6 +26,7 @@ class WikiTargetInfo:
raise
self.query = session.query(WikiTargetSetInfo).filter_by(targetId=targetId).first()
@retry(stop=stop_after_attempt(3))
def add_start_wiki(self, url):
try:
self.query.link = url
@ -38,6 +42,7 @@ class WikiTargetInfo:
return self.query.link if self.query.link is not None else False
return False
@retry(stop=stop_after_attempt(3))
def config_interwikis(self, iw: str, iwlink: str = None, let_it=True):
try:
interwikis = json.loads(self.query.iws)
@ -62,6 +67,7 @@ class WikiTargetInfo:
else:
return False
@retry(stop=stop_after_attempt(3))
def config_headers(self, headers, let_it: [bool, None] = True):
try:
headers = json.loads(headers)
@ -101,6 +107,7 @@ class WikiSiteInfo:
return self.query.siteInfo, self.query.timestamp
return False
@retry(stop=stop_after_attempt(3))
def update(self, info: dict):
try:
if self.query is None:
@ -111,4 +118,4 @@ class WikiSiteInfo:
return True
except Exception:
session.rollback()
raise
raise

View file

@ -19,4 +19,5 @@ aiogram
aiohttp_retry
ujson
pymysql
discord.py
discord.py
tenacity