Added watchdog

This commit is contained in:
Casey 2024-07-07 15:28:38 +03:00
parent 319da9746f
commit 29ee0546c3
Signed by: hkc
GPG Key ID: F0F6CFE11CDB0960
1 changed files with 153 additions and 135 deletions

View File

@ -37,6 +37,7 @@ class AsyncBotManager:
self._last_printout = time_now()
self._active: set[int] = set()
self.reader_event = asyncio.Event()
@staticmethod
def get_text_image(text: str, font: ImageFont.ImageFont | ImageFont.FreeTypeFont) -> Image.Image:
@ -160,6 +161,7 @@ class AsyncBotManager:
print("Reconnecting")
await sio.connect(f"{self.base}/socket.io")
continue
self.reader_event.set()
if event == "full_state":
buffer = b64decode(data["full_state"].encode() + b"=")
image = Image.frombytes("1", (1000, 1000), buffer)
@ -260,7 +262,22 @@ async def amain() -> None:
with open("settings.json", "r") as 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"]:
if avoid["type"] == "rect":
mgr.add_avoid_rect(
@ -410,6 +427,7 @@ async def amain() -> None:
print("RETURN", repr(ret))
mgr._shutdown = True
watchdog_task.cancel()
if __name__ == "__main__":