Fix timeout

This commit is contained in:
blank X 2020-09-16 21:52:26 +07:00
parent b89c7cb32d
commit baefd09f65
1 changed files with 9 additions and 3 deletions

View File

@ -95,13 +95,16 @@ async def main():
show_spoilers_warning = chat_data['show_spoilers_warning'] show_spoilers_warning = chat_data['show_spoilers_warning']
timeout = chat_data.get('timeout') timeout = chat_data.get('timeout')
def _get_submission_blocc(): give_ups = set()
while True: def _get_submission_blocc(unique_id):
while unique_id not in give_ups:
subreddit = reddit.subreddit(random.choice(subreddits)) subreddit = reddit.subreddit(random.choice(subreddits))
random_post = subreddit.random() random_post = subreddit.random()
cpid = cpp = None cpid = cpp = None
if random_post is None: if random_post is None:
for submission in subreddit.hot(): for submission in subreddit.hot():
if unique_id in give_ups:
return
cpid = getattr(submission, 'crosspost_parent', None) cpid = getattr(submission, 'crosspost_parent', None)
if cpid and getattr(random_post, 'crosspost_parent_list', None): if cpid and getattr(random_post, 'crosspost_parent_list', None):
cpid = cpid[3:] cpid = cpid[3:]
@ -158,14 +161,17 @@ async def main():
@aiocron.crontab(cron_duration) @aiocron.crontab(cron_duration)
async def start_post(): async def start_post():
while True: while True:
unique_id = time.time()
try: try:
random_post, cpp = await asyncio.wait_for(client.loop.run_in_executor(None, _get_submission_blocc), timeout) random_post, cpp = await asyncio.wait_for(client.loop.run_in_executor(None, _get_submission_blocc, unique_id), timeout)
except asyncio.TimeoutError: except asyncio.TimeoutError:
give_ups.add(unique_id)
logging.error('%s timed out', chat) logging.error('%s timed out', chat)
for i in bot_admins: for i in bot_admins:
await client.send_message(i, f'{chat} timed out') await client.send_message(i, f'{chat} timed out')
break break
except Exception: except Exception:
give_ups.add(unique_id)
logging.exception(chat) logging.exception(chat)
for i in bot_admins: for i in bot_admins:
await client.send_message(i, f'{chat}\n{traceback.format_exc()}') await client.send_message(i, f'{chat}\n{traceback.format_exc()}')