Notice when connection runs out of retries

This commit is contained in:
Joscha 2018-08-01 21:41:08 +00:00
parent df72a3d9cf
commit d55d05826f
3 changed files with 33 additions and 13 deletions

View file

@ -77,6 +77,11 @@ class Bot(Inhabitant):
if room: if room:
await room.exit() await room.exit()
# INHABITED FUNCTIONS
async def on_stopped(self, room):
await self.part_room(room.roomname)
# BOTRULEZ # BOTRULEZ
@command("ping", specific=False, args=False) @command("ping", specific=False, args=False)

View file

@ -12,10 +12,11 @@ __all__ = ["Connection"]
class Connection: class Connection:
def __init__(self, url, packet_callback, disconnect_callback, cookiejar=None, ping_timeout=10, ping_delay=30, reconnect_attempts=10): def __init__(self, url, packet_callback, disconnect_callback, stop_callback, cookiejar=None, ping_timeout=10, ping_delay=30, reconnect_attempts=10):
self.url = url self.url = url
self.packet_callback = packet_callback self.packet_callback = packet_callback
self.disconnect_callback = disconnect_callback self.disconnect_callback = disconnect_callback
self.stop_callback = stop_callback # is called when the connection stops on its own
self.cookiejar = cookiejar self.cookiejar = cookiejar
self.ping_timeout = ping_timeout # how long to wait for websocket ping reply self.ping_timeout = ping_timeout # how long to wait for websocket ping reply
self.ping_delay = ping_delay # how long to wait between pings self.ping_delay = ping_delay # how long to wait between pings
@ -65,6 +66,7 @@ class Connection:
This means that stop() can only be called once. This means that stop() can only be called once.
""" """
if not self._stopped:
self._stopped = True self._stopped = True
await self.reconnect() # _run() does the cleaning up now. await self.reconnect() # _run() does the cleaning up now.
await self._runtask await self._runtask
@ -153,9 +155,9 @@ class Connection:
""" """
while not self._stopped: while not self._stopped:
await self._connect(self.reconnect_attempts) connected = await self._connect(self.reconnect_attempts)
if connected:
logger.debug(f"{self.url}:Connected") logger.debug(f"{self.url}:Connected")
try: try:
while True: while True:
await self._handle_next_message() await self._handle_next_message()
@ -163,6 +165,12 @@ class Connection:
pass pass
finally: finally:
await self._disconnect() # disconnect and clean up await self._disconnect() # disconnect and clean up
else:
logger.debug(f"{self.url}:Stopping")
asyncio.ensure_future(self.stop_callback)
self._stopped = True
await self._disconnect()
async def _ping(self): async def _ping(self):
""" """

View file

@ -58,6 +58,7 @@ class Room:
self.format_room_url(self.roomname, human=self.human), self.format_room_url(self.roomname, human=self.human),
self._receive_packet, self._receive_packet,
self._disconnected, self._disconnected,
self._stopped,
cookiejar cookiejar
) )
@ -172,6 +173,9 @@ class Room:
await self._inhabitant.on_disconnected(self) await self._inhabitant.on_disconnected(self)
async def _stopped(self):
await self._inhabitant.on_stopped(self)
async def _receive_packet(self, ptype, data, error, throttled): async def _receive_packet(self, ptype, data, error, throttled):
# Ignoring errors and throttling for now # Ignoring errors and throttling for now
functions = { functions = {
@ -410,6 +414,9 @@ class Inhabitant:
async def on_disconnected(self, room): async def on_disconnected(self, room):
pass pass
async def on_stopped(self, room):
pass
async def on_join(self, room, session): async def on_join(self, room, session):
pass pass