Stability improovements
This commit is contained in:
parent
2e58fd3fce
commit
77a2e43400
23
bot.py
23
bot.py
@ -9,6 +9,7 @@ import traceback
|
|||||||
import ujson
|
import ujson
|
||||||
import vk
|
import vk
|
||||||
import wget
|
import wget
|
||||||
|
import time
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
from telebot import types
|
from telebot import types
|
||||||
|
|
||||||
@ -17,8 +18,6 @@ import cherrypy
|
|||||||
from credentials import token, vk_app_id, bot_url, local_port
|
from credentials import token, vk_app_id, bot_url, local_port
|
||||||
from vk_messages import VkMessage, VkPolling
|
from vk_messages import VkMessage, VkPolling
|
||||||
|
|
||||||
logging.basicConfig(format='%(levelname)-8s [%(asctime)s] %(message)s', level=logging.WARNING, filename='vk.log')
|
|
||||||
|
|
||||||
vk_threads = {}
|
vk_threads = {}
|
||||||
|
|
||||||
vk_dialogs = {}
|
vk_dialogs = {}
|
||||||
@ -170,11 +169,13 @@ def callback_buttons(call):
|
|||||||
|
|
||||||
def create_thread(uid, vk_token):
|
def create_thread(uid, vk_token):
|
||||||
a = VkPolling()
|
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.setDaemon(True)
|
||||||
t.start()
|
t.start()
|
||||||
vk_threads[str(uid)] = a
|
vk_threads[str(uid)] = a
|
||||||
vk_tokens.set(str(uid), vk_token)
|
vk_tokens.set(str(uid), vk_token)
|
||||||
|
vk.API(longpoller.session).account.setOffline()
|
||||||
|
|
||||||
|
|
||||||
def check_thread(uid):
|
def check_thread(uid):
|
||||||
@ -184,11 +185,22 @@ def check_thread(uid):
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
# Creating VkPolling threads and dialogs info after bot reboot using existing tokens
|
# Creating VkPolling threads and dialogs info after bot's reboot/exception using existing tokens
|
||||||
for uid in vk_tokens.scan_iter():
|
def thread_supervisor():
|
||||||
|
while True:
|
||||||
|
for uid in vk_tokens.scan_iter():
|
||||||
if check_thread(uid.decode("utf-8")):
|
if check_thread(uid.decode("utf-8")):
|
||||||
|
try:
|
||||||
create_thread(uid.decode("utf-8"), vk_tokens.get(uid))
|
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"))
|
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):
|
def stop_thread(message):
|
||||||
@ -643,6 +655,7 @@ class WebhookServer(object):
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
logging.basicConfig(format='%(levelname)-8s [%(asctime)s] %(message)s', level=logging.WARNING, filename='vk.log')
|
||||||
bot.remove_webhook()
|
bot.remove_webhook()
|
||||||
bot.set_webhook('https://{}/{}/'.format(bot_url, token))
|
bot.set_webhook('https://{}/{}/'.format(bot_url, token))
|
||||||
cherrypy.config.update(
|
cherrypy.config.update(
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
|
import logging
|
||||||
import os
|
import os
|
||||||
import redis
|
import redis
|
||||||
import requests
|
import requests
|
||||||
import time
|
import time
|
||||||
import traceback
|
|
||||||
import vk
|
import vk
|
||||||
import wget
|
import wget
|
||||||
|
|
||||||
@ -11,6 +11,8 @@ vk_tokens = redis.StrictRedis(connection_pool=tokens_pool)
|
|||||||
|
|
||||||
|
|
||||||
class VkPolling:
|
class VkPolling:
|
||||||
|
logging.basicConfig(format='%(levelname)-8s [%(asctime)s] %(message)s', level=logging.WARNING, filename='vk.log')
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self._running = True
|
self._running = True
|
||||||
|
|
||||||
@ -19,14 +21,16 @@ class VkPolling:
|
|||||||
|
|
||||||
def run(self, vk_user, bot, chat_id):
|
def run(self, vk_user, bot, chat_id):
|
||||||
while self._running:
|
while self._running:
|
||||||
updates = []
|
timeout = 50
|
||||||
try:
|
try:
|
||||||
updates = vk_user.get_new_messages()
|
updates = vk_user.get_new_messages()
|
||||||
except requests.exceptions.ReadTimeout as e:
|
|
||||||
print('Error: {}'.format(e))
|
|
||||||
if updates:
|
if updates:
|
||||||
handle_updates(vk_user, bot, chat_id, updates)
|
handle_updates(vk_user, bot, chat_id, updates)
|
||||||
for i in range(60):
|
except requests.exceptions.ReadTimeout as e:
|
||||||
|
print(e)
|
||||||
|
timeout *= 2
|
||||||
|
print('Retry VK Polling in {} sec'.format(timeout))
|
||||||
|
for i in range(timeout):
|
||||||
if self._running:
|
if self._running:
|
||||||
time.sleep(0.1)
|
time.sleep(0.1)
|
||||||
else:
|
else:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user