Add timeout

This commit is contained in:
blank X 2020-09-16 21:21:50 +07:00
parent 98b4a7a1b3
commit b89c7cb32d
2 changed files with 68 additions and 53 deletions

View File

@ -20,6 +20,7 @@ config:
allow_spoilers: true
show_nsfw_warning: true
show_spoilers_warning: true
timeout: 60
-1001284025455:
subreddits:
- HentaiMemes
@ -29,6 +30,7 @@ config:
allow_spoilers: true
show_nsfw_warning: false
show_spoilers_warning: true
timeout: 60
memes10k:
subreddits:
- linuxmemes
@ -41,6 +43,7 @@ config:
allow_spoilers: true
show_nsfw_warning: true
show_spoilers_warning: true
timeout: 60
bot_admins:
- thekneesocks
- 214416808

View File

@ -44,7 +44,7 @@ if isinstance(_send_to_chats, list):
j = {'subreddits': j, 'cron_duration': config_data['config']['cron_duration'],
'allow_selfposts': True, 'allow_nsfw': True,
'allow_spoilers': True, 'show_nsfw_warning': True,
'show_spoilers_warning': True}
'show_spoilers_warning': True, 'timeout': None}
send_to_chats[i] = j
bot_admins = config_data['config']['bot_admins']
@ -93,11 +93,8 @@ async def main():
allow_spoilers = chat_data['allow_spoilers']
show_nsfw_warning = chat_data['show_nsfw_warning']
show_spoilers_warning = chat_data['show_spoilers_warning']
timeout = chat_data.get('timeout')
@aiocron.crontab(cron_duration)
async def start_post():
while True:
random.shuffle(subreddits)
def _get_submission_blocc():
while True:
subreddit = reddit.subreddit(random.choice(subreddits))
@ -157,7 +154,22 @@ async def main():
chat_sp.append(cpid or random_post.id)
print(random_post.id, random_post.shortlink)
return random_post, cpp
random_post, cpp = await client.loop.run_in_executor(None, _get_submission_blocc)
@aiocron.crontab(cron_duration)
async def start_post():
while True:
try:
random_post, cpp = await asyncio.wait_for(client.loop.run_in_executor(None, _get_submission_blocc), timeout)
except asyncio.TimeoutError:
logging.error('%s timed out', chat)
for i in bot_admins:
await client.send_message(i, f'{chat} timed out')
break
except Exception:
logging.exception(chat)
for i in bot_admins:
await client.send_message(i, f'{chat}\n{traceback.format_exc()}')
else:
try:
await _actual_start_post(random_post, [chat], cpp, show_nsfw_warning, show_spoilers_warning)
except Exception:
@ -189,7 +201,7 @@ async def main():
async def _download_file(filename, url):
print(url)
async with session.get(url, timeout=None) as resp:
async with session.get(url) as resp:
with open(filename, 'wb') as file:
while True:
chunk = await resp.content.read(10)