Reconnects the ongpoll chat server after a ReadTimeout or ConnectionError
This commit is contained in:
parent
ed624f5bf1
commit
0144a5787a
@ -2,6 +2,8 @@
|
|||||||
import threading
|
import threading
|
||||||
from vk_api.longpoll import VkLongPoll, VkEventType
|
from vk_api.longpoll import VkLongPoll, VkEventType
|
||||||
from pubsub import pub
|
from pubsub import pub
|
||||||
|
from requests.exceptions import ReadTimeout
|
||||||
|
|
||||||
from logging import getLogger
|
from logging import getLogger
|
||||||
log = getLogger("controller.longpolThread")
|
log = getLogger("controller.longpolThread")
|
||||||
|
|
||||||
@ -13,6 +15,7 @@ class worker(threading.Thread):
|
|||||||
self.longpoll = VkLongPoll(self.session.vk.session_object)
|
self.longpoll = VkLongPoll(self.session.vk.session_object)
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
|
try:
|
||||||
for event in self.longpoll.listen():
|
for event in self.longpoll.listen():
|
||||||
if event.type == VkEventType.MESSAGE_NEW:
|
if event.type == VkEventType.MESSAGE_NEW:
|
||||||
pub.sendMessage("order-sent-message", obj=event)
|
pub.sendMessage("order-sent-message", obj=event)
|
||||||
@ -20,3 +23,5 @@ class worker(threading.Thread):
|
|||||||
pub.sendMessage("user-online", event=event)
|
pub.sendMessage("user-online", event=event)
|
||||||
elif event.type == VkEventType.USER_OFFLINE:
|
elif event.type == VkEventType.USER_OFFLINE:
|
||||||
pub.sendMessage("user-offline", event=event)
|
pub.sendMessage("user-offline", event=event)
|
||||||
|
except ReadTimeout:
|
||||||
|
pub.sendMessage("longpoll-read-timeout")
|
@ -15,6 +15,7 @@ import output
|
|||||||
import longpollthread
|
import longpollthread
|
||||||
import selector
|
import selector
|
||||||
from vk_api.exceptions import LoginRequired, VkApiError
|
from vk_api.exceptions import LoginRequired, VkApiError
|
||||||
|
from requests.exceptions import ConnectionError
|
||||||
from pubsub import pub
|
from pubsub import pub
|
||||||
from mysc.repeating_timer import RepeatingTimer
|
from mysc.repeating_timer import RepeatingTimer
|
||||||
from mysc.thread_utils import call_threaded
|
from mysc.thread_utils import call_threaded
|
||||||
@ -139,6 +140,7 @@ class Controller(object):
|
|||||||
pub.subscribe(self.user_online, "user-online")
|
pub.subscribe(self.user_online, "user-online")
|
||||||
pub.subscribe(self.user_offline, "user-offline")
|
pub.subscribe(self.user_offline, "user-offline")
|
||||||
pub.subscribe(self.notify, "notify")
|
pub.subscribe(self.notify, "notify")
|
||||||
|
pub.subscribe(self.handle_longpoll_read_timeout, "longpoll-read-timeout")
|
||||||
widgetUtils.connect_event(self.window, widgetUtils.CLOSE_EVENT, self.exit)
|
widgetUtils.connect_event(self.window, widgetUtils.CLOSE_EVENT, self.exit)
|
||||||
widgetUtils.connect_event(self.window, widgetUtils.MENU, self.update_buffer, menuitem=self.window.update_buffer)
|
widgetUtils.connect_event(self.window, widgetUtils.MENU, self.update_buffer, menuitem=self.window.update_buffer)
|
||||||
widgetUtils.connect_event(self.window, widgetUtils.MENU, self.check_for_updates, menuitem=self.window.check_for_updates)
|
widgetUtils.connect_event(self.window, widgetUtils.MENU, self.check_for_updates, menuitem=self.window.check_for_updates)
|
||||||
@ -193,8 +195,7 @@ class Controller(object):
|
|||||||
self.window.change_status(_(u"Loading items for {0}").format(i.name,))
|
self.window.change_status(_(u"Loading items for {0}").format(i.name,))
|
||||||
i.get_items()
|
i.get_items()
|
||||||
self.window.change_status(_(u"Ready"))
|
self.window.change_status(_(u"Ready"))
|
||||||
self.longpoll = longpollthread.worker(self.session)
|
self.create_longpoll_thread()
|
||||||
self.longpoll.start()
|
|
||||||
self.status_setter = RepeatingTimer(900, self.set_online)
|
self.status_setter = RepeatingTimer(900, self.set_online)
|
||||||
self.status_setter.start()
|
self.status_setter.start()
|
||||||
self.set_online()
|
self.set_online()
|
||||||
@ -202,6 +203,15 @@ class Controller(object):
|
|||||||
wx.CallAfter(self.get_audio_albums, self.session.user_id)
|
wx.CallAfter(self.get_audio_albums, self.session.user_id)
|
||||||
wx.CallAfter(self.get_video_albums, self.session.user_id)
|
wx.CallAfter(self.get_video_albums, self.session.user_id)
|
||||||
|
|
||||||
|
def create_longpoll_thread(self, notify=False):
|
||||||
|
try:
|
||||||
|
self.longpoll = longpollthread.worker(self.session)
|
||||||
|
self.longpoll.start()
|
||||||
|
if notify:
|
||||||
|
self.notify(message=_(u"Chat server reconnected"))
|
||||||
|
except ConnectionError:
|
||||||
|
pub.sendMessage("longpoll-read-timeout")
|
||||||
|
|
||||||
def in_post(self, buffer):
|
def in_post(self, buffer):
|
||||||
buffer = self.search(buffer)
|
buffer = self.search(buffer)
|
||||||
buffer.get_items()
|
buffer.get_items()
|
||||||
@ -628,3 +638,11 @@ class Controller(object):
|
|||||||
self.session.soundplayer.play(sound)
|
self.session.soundplayer.play(sound)
|
||||||
if message != "":
|
if message != "":
|
||||||
output.speak(message)
|
output.speak(message)
|
||||||
|
|
||||||
|
def handle_longpoll_read_timeout(self):
|
||||||
|
if hasattr(self, "longpoll"):
|
||||||
|
self.notify(message=_(u"Chat disconnected. Trying to connect in 60 seconds"))
|
||||||
|
time.sleep(60)
|
||||||
|
if hasattr(self, "longpoll"):
|
||||||
|
del self.longpoll
|
||||||
|
self.create_longpoll_thread(notify=True)
|
Loading…
Reference in New Issue
Block a user