(真)修复启动长时间后自动翻译失效的问题

This commit is contained in:
John Smith 2020-05-09 21:38:21 +08:00
parent 63644f5499
commit 4c7bf69275

View File

@ -130,10 +130,10 @@ class TencentTranslate(TranslateProvider):
self._cool_down_future = None self._cool_down_future = None
async def init(self): async def init(self):
if self._reinit_future is not None: self._reinit_future = asyncio.ensure_future(self._reinit_coroutine())
self._reinit_future.cancel() return await self._do_init()
self._reinit_future = None
async def _do_init(self):
try: try:
async with _http_session.get('https://fanyi.qq.com/') as r: async with _http_session.get('https://fanyi.qq.com/') as r:
if r.status != 200: if r.status != 200:
@ -157,13 +157,20 @@ class TencentTranslate(TranslateProvider):
self._qtv = qtv self._qtv = qtv
self._qtk = qtk self._qtk = qtk
self._reinit_future = asyncio.ensure_future(self._reinit_coroutine())
return True return True
async def _reinit_coroutine(self): async def _reinit_coroutine(self):
try: try:
while True:
await asyncio.sleep(55 * 60) await asyncio.sleep(55 * 60)
await self.init() while True:
logger.info('TencentTranslate reinit')
try:
if await self._do_init():
break
except:
logger.exception('TencentTranslate init error:')
await asyncio.sleep(3 * 60)
except asyncio.CancelledError: except asyncio.CancelledError:
pass pass
@ -230,14 +237,13 @@ class TencentTranslate(TranslateProvider):
try: try:
while True: while True:
await asyncio.sleep(3 * 60) await asyncio.sleep(3 * 60)
logger.info('TencentTranslate reinit')
try: try:
is_success = await self.init() if await self._do_init():
except:
logger.exception('TencentTranslate init error:')
continue
if is_success:
self._fail_count = 0 self._fail_count = 0
break break
except:
logger.exception('TencentTranslate init error:')
finally: finally:
logger.warning('TencentTranslate finished cooling down') logger.warning('TencentTranslate finished cooling down')
self._cool_down_future = None self._cool_down_future = None