After the first test i lose the ability to ping twitch server and I'm able to reconnect. I think that an error raised (now logged) and ws.keep_running was set to False by lib. So remove handling of keep_running. Log close and error so let's wee see
This commit is contained in:
@@ -248,14 +248,16 @@ class TwitchChannelPointsMiner:
|
||||
while self.running:
|
||||
time.sleep(random.uniform(20, 60))
|
||||
# Do an external control for WebSocket. Check if the thread is running
|
||||
# Check if is not None because maybe we have already created a new connection on array+1 and now index is None
|
||||
for index in range(0, len(self.ws_pool.ws)):
|
||||
if self.ws_pool.ws[index].elapsed_last_ping() > 5:
|
||||
if (
|
||||
self.ws_pool.ws[index] is not None
|
||||
and self.ws_pool.ws[index].elapsed_last_ping() > 10
|
||||
):
|
||||
logger.info(
|
||||
f"#{index} - The last ping was sent more than 5 minutes ago. Reconnecting to the WebSocket..."
|
||||
)
|
||||
WebSocketsPool.handle_websocket_reconnection(
|
||||
self.ws_pool.ws[index]
|
||||
f"#{index} - The last PING was sent more than 10 minutes ago. Reconnecting to the WebSocket..."
|
||||
)
|
||||
WebSocketsPool.handle_reconnection(self.ws_pool.ws[index])
|
||||
|
||||
def end(self, signum, frame):
|
||||
logger.info("CTRL+C Detected! Please wait just a moments!")
|
||||
|
||||
@@ -33,7 +33,6 @@ class TwitchWebSocket(WebSocketApp):
|
||||
|
||||
def reset(self, parent_pool):
|
||||
self.parent_pool = parent_pool
|
||||
self.keep_running = True
|
||||
self.is_closed = False
|
||||
self.is_opened = False
|
||||
self.is_reconneting = False
|
||||
|
||||
@@ -55,7 +55,9 @@ class WebSocketsPool:
|
||||
url=WEBSOCKET,
|
||||
on_message=WebSocketsPool.on_message,
|
||||
on_open=WebSocketsPool.on_open,
|
||||
on_close=WebSocketsPool.handle_websocket_reconnection,
|
||||
on_error=WebSocketsPool.on_error,
|
||||
on_close=WebSocketsPool.on_close
|
||||
# on_close=WebSocketsPool.handle_reconnection, # Do nothing.
|
||||
)
|
||||
)
|
||||
self.ws[-1].reset(self)
|
||||
@@ -66,7 +68,6 @@ class WebSocketsPool:
|
||||
|
||||
def end(self):
|
||||
for index in range(0, len(self.ws)):
|
||||
self.ws[index].keep_running = False
|
||||
self.ws[index].close()
|
||||
|
||||
@staticmethod
|
||||
@@ -81,31 +82,35 @@ class WebSocketsPool:
|
||||
ws.ping()
|
||||
time.sleep(random.uniform(25, 30))
|
||||
|
||||
if ws.elapsed_last_pong() > 15 and ws.is_reconneting is False:
|
||||
if ws.elapsed_last_pong() > 10 and ws.is_reconneting is False:
|
||||
logger.info(
|
||||
f"#{ws.index} - The last pong was received more than 15 minutes ago. Reconnect the WebSocket"
|
||||
f"#{ws.index} - The last PONG was received more than 10 minutes ago. Reconnect the WebSocket"
|
||||
)
|
||||
ws.keep_running = True
|
||||
ws.is_reconneting = True
|
||||
WebSocketsPool.handle_websocket_reconnection(ws)
|
||||
WebSocketsPool.handle_reconnection(ws)
|
||||
|
||||
thread_ws = threading.Thread(target=run)
|
||||
thread_ws.daemon = True
|
||||
thread_ws.start()
|
||||
|
||||
@staticmethod
|
||||
def handle_websocket_reconnection(ws):
|
||||
ws.is_closed = True
|
||||
if ws.keep_running is True:
|
||||
logger.info(
|
||||
f"#{ws.index} - Reconnecting to Twitch PubSub server in 60 seconds"
|
||||
)
|
||||
time.sleep(60)
|
||||
def on_error(ws, error):
|
||||
logger.error(f"#{ws.index} - WebSocket error: {error}")
|
||||
|
||||
self = ws.parent_pool
|
||||
self.ws[ws.index] = None
|
||||
for topic in ws.topics:
|
||||
self.submit(topic)
|
||||
@staticmethod
|
||||
def on_close(ws):
|
||||
logger.info(f"#{ws.index} - WebSocket closed")
|
||||
|
||||
@staticmethod
|
||||
def handle_reconnection(ws):
|
||||
ws.is_closed = True
|
||||
logger.info(f"#{ws.index} - Reconnecting to Twitch PubSub server in 30 seconds")
|
||||
time.sleep(30)
|
||||
|
||||
self = ws.parent_pool
|
||||
self.ws[ws.index] = None
|
||||
for topic in ws.topics:
|
||||
self.submit(topic)
|
||||
|
||||
@staticmethod
|
||||
def on_message(ws, message):
|
||||
@@ -312,11 +317,9 @@ class WebSocketsPool:
|
||||
raise RuntimeError(f"Error while trying to listen for a topic: {response}")
|
||||
|
||||
elif response["type"] == "RECONNECT":
|
||||
logger.info(
|
||||
f"#{ws.index} - Reconnection required and keep running is: {ws.keep_running}"
|
||||
)
|
||||
logger.info(f"#{ws.index} - Reconnection required")
|
||||
ws.is_reconneting = True
|
||||
WebSocketsPool.handle_websocket_reconnection(ws)
|
||||
WebSocketsPool.handle_reconnection(ws)
|
||||
|
||||
elif response["type"] == "PONG":
|
||||
ws.last_pong = time.time()
|
||||
|
||||
Reference in New Issue
Block a user