Added watchdog
This commit is contained in:
parent
319da9746f
commit
29ee0546c3
20
async-bot.py
20
async-bot.py
|
@ -37,6 +37,7 @@ class AsyncBotManager:
|
||||||
self._last_printout = time_now()
|
self._last_printout = time_now()
|
||||||
|
|
||||||
self._active: set[int] = set()
|
self._active: set[int] = set()
|
||||||
|
self.reader_event = asyncio.Event()
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_text_image(text: str, font: ImageFont.ImageFont | ImageFont.FreeTypeFont) -> Image.Image:
|
def get_text_image(text: str, font: ImageFont.ImageFont | ImageFont.FreeTypeFont) -> Image.Image:
|
||||||
|
@ -160,6 +161,7 @@ class AsyncBotManager:
|
||||||
print("Reconnecting")
|
print("Reconnecting")
|
||||||
await sio.connect(f"{self.base}/socket.io")
|
await sio.connect(f"{self.base}/socket.io")
|
||||||
continue
|
continue
|
||||||
|
self.reader_event.set()
|
||||||
if event == "full_state":
|
if event == "full_state":
|
||||||
buffer = b64decode(data["full_state"].encode() + b"=")
|
buffer = b64decode(data["full_state"].encode() + b"=")
|
||||||
image = Image.frombytes("1", (1000, 1000), buffer)
|
image = Image.frombytes("1", (1000, 1000), buffer)
|
||||||
|
@ -260,7 +262,22 @@ async def amain() -> None:
|
||||||
with open("settings.json", "r") as fp:
|
with open("settings.json", "r") as fp:
|
||||||
settings = load(fp)
|
settings = load(fp)
|
||||||
|
|
||||||
async with AsyncBotManager() as mgr:
|
mgr = AsyncBotManager()
|
||||||
|
|
||||||
|
|
||||||
|
async def watchdog():
|
||||||
|
listener_task = asyncio.create_task(mgr.listener(), name="Listener")
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
async with asyncio.timeout(30):
|
||||||
|
await mgr.reader_event.wait()
|
||||||
|
except TimeoutError:
|
||||||
|
print("WATCHDOG TIMER PASSED")
|
||||||
|
listener_task.cancel()
|
||||||
|
listener_task = asyncio.create_task(mgr.listener())
|
||||||
|
|
||||||
|
watchdog_task = asyncio.create_task(watchdog(), name="Watchdog")
|
||||||
|
|
||||||
for avoid in settings["avoid"]:
|
for avoid in settings["avoid"]:
|
||||||
if avoid["type"] == "rect":
|
if avoid["type"] == "rect":
|
||||||
mgr.add_avoid_rect(
|
mgr.add_avoid_rect(
|
||||||
|
@ -410,6 +427,7 @@ async def amain() -> None:
|
||||||
print("RETURN", repr(ret))
|
print("RETURN", repr(ret))
|
||||||
|
|
||||||
mgr._shutdown = True
|
mgr._shutdown = True
|
||||||
|
watchdog_task.cancel()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
Loading…
Reference in New Issue