Stability improovements

This commit is contained in:
Kylmakalle 2017-07-04 22:12:16 +03:00
parent 2e58fd3fce
commit 77a2e43400
2 changed files with 32 additions and 15 deletions

31
bot.py
View File

@ -9,6 +9,7 @@ import traceback
import ujson
import vk
import wget
import time
from PIL import Image
from telebot import types
@ -17,8 +18,6 @@ import cherrypy
from credentials import token, vk_app_id, bot_url, local_port
from vk_messages import VkMessage, VkPolling
logging.basicConfig(format='%(levelname)-8s [%(asctime)s] %(message)s', level=logging.WARNING, filename='vk.log')
vk_threads = {}
vk_dialogs = {}
@ -170,11 +169,13 @@ def callback_buttons(call):
def create_thread(uid, vk_token):
a = VkPolling()
t = threading.Thread(name='vk' + str(uid), target=a.run, args=(VkMessage(vk_token), bot, uid,))
longpoller = VkMessage(vk_token)
t = threading.Thread(name='vk' + str(uid), target=a.run, args=(longpoller, bot, uid,))
t.setDaemon(True)
t.start()
vk_threads[str(uid)] = a
vk_tokens.set(str(uid), vk_token)
vk.API(longpoller.session).account.setOffline()
def check_thread(uid):
@ -184,11 +185,22 @@ def check_thread(uid):
return True
# Creating VkPolling threads and dialogs info after bot reboot using existing tokens
for uid in vk_tokens.scan_iter():
if check_thread(uid.decode("utf-8")):
create_thread(uid.decode("utf-8"), vk_tokens.get(uid))
request_user_dialogs(VkMessage(vk_tokens.get(uid.decode("utf-8"))).session, uid.decode("utf-8"))
# Creating VkPolling threads and dialogs info after bot's reboot/exception using existing tokens
def thread_supervisor():
while True:
for uid in vk_tokens.scan_iter():
if check_thread(uid.decode("utf-8")):
try:
create_thread(uid.decode("utf-8"), vk_tokens.get(uid))
request_user_dialogs(VkMessage(vk_tokens.get(uid.decode("utf-8"))).session, uid.decode("utf-8"))
except requests.exceptions.ReadTimeout as e:
time.sleep(10)
time.sleep(120)
supervisor = threading.Thread(name='supervisor', target=thread_supervisor)
supervisor.setDaemon(True)
supervisor.start()
def stop_thread(message):
@ -627,7 +639,7 @@ def reply_text(message):
except Exception:
bot.reply_to(message, 'Произошла неизвестная ошибка при отправке',
parse_mode='Markdown').wait()
print('Error: {}'.format(traceback.format_exc()))
print('Error: {}'.format(traceback.format_exc()))
# bot.polling(none_stop=True)
@ -643,6 +655,7 @@ class WebhookServer(object):
if __name__ == '__main__':
logging.basicConfig(format='%(levelname)-8s [%(asctime)s] %(message)s', level=logging.WARNING, filename='vk.log')
bot.remove_webhook()
bot.set_webhook('https://{}/{}/'.format(bot_url, token))
cherrypy.config.update(

View File

@ -1,8 +1,8 @@
import logging
import os
import redis
import requests
import time
import traceback
import vk
import wget
@ -11,6 +11,8 @@ vk_tokens = redis.StrictRedis(connection_pool=tokens_pool)
class VkPolling:
logging.basicConfig(format='%(levelname)-8s [%(asctime)s] %(message)s', level=logging.WARNING, filename='vk.log')
def __init__(self):
self._running = True
@ -19,14 +21,16 @@ class VkPolling:
def run(self, vk_user, bot, chat_id):
while self._running:
updates = []
timeout = 50
try:
updates = vk_user.get_new_messages()
if updates:
handle_updates(vk_user, bot, chat_id, updates)
except requests.exceptions.ReadTimeout as e:
print('Error: {}'.format(e))
if updates:
handle_updates(vk_user, bot, chat_id, updates)
for i in range(60):
print(e)
timeout *= 2
print('Retry VK Polling in {} sec'.format(timeout))
for i in range(timeout):
if self._running:
time.sleep(0.1)
else: