method call listener retry bug fix, method call response serializaiton failure fix,

- Added retry logic to the QuickAccess tab fetching in the method call listener.
- Added exception handling, in case a plugin method returns something that can't be serialized as JSON.
- Changed a few log calls from info to debug to prevent spam
- Added a filter for asyncio base_event log records, since they get spamy and don't provide any useful info most of the time. This can be turned off with the LOG_BASE_EVENTS envar.
This commit is contained in:
marios
2022-04-26 23:37:01 +03:00
parent 73559ae8c7
commit fe1f6473e9
3 changed files with 36 additions and 14 deletions
+6 -4
View File
@@ -3,6 +3,7 @@
from aiohttp import ClientSession from aiohttp import ClientSession
from logging import debug, getLogger from logging import debug, getLogger
from asyncio import sleep from asyncio import sleep
from traceback import format_exc
BASE_ADDRESS = "http://localhost:8080" BASE_ADDRESS = "http://localhost:8080"
@@ -61,14 +62,15 @@ async def get_tabs():
res = await web.get(f"{BASE_ADDRESS}/json") res = await web.get(f"{BASE_ADDRESS}/json")
break break
except: except:
logger.info("Steam isn't available yet. Wait for a moment...") logger.debug("Steam isn't available yet. Wait for a moment...")
logger.debug(format_exc())
await sleep(5) await sleep(5)
if res.status == 200: if res.status == 200:
res = await res.json() r = await res.json()
return [Tab(i) for i in res] return [Tab(i) for i in r]
else: else:
raise Exception(f"/json did not return 200. {await res.text()}") raise Exception(f"/json did not return 200. {await r.text()}")
async def get_tab(tab_name): async def get_tab(tab_name):
tabs = await get_tabs() tabs = await get_tabs()
+23 -3
View File
@@ -1,4 +1,4 @@
from logging import getLogger, basicConfig, INFO, DEBUG from logging import getLogger, basicConfig, INFO, DEBUG, Filter, root
from os import getenv from os import getenv
CONFIG = { CONFIG = {
@@ -7,10 +7,19 @@ CONFIG = {
"server_port": int(getenv("SERVER_PORT", "1337")), "server_port": int(getenv("SERVER_PORT", "1337")),
"live_reload": getenv("LIVE_RELOAD", "1") == "1", "live_reload": getenv("LIVE_RELOAD", "1") == "1",
"log_level": {"CRITICAL": 50, "ERROR": 40, "WARNING":30, "INFO": 20, "DEBUG": 10}[getenv("LOG_LEVEL", "INFO")], "log_level": {"CRITICAL": 50, "ERROR": 40, "WARNING":30, "INFO": 20, "DEBUG": 10}[getenv("LOG_LEVEL", "INFO")],
"store_url": getenv("STORE_URL", "https://sdh.tzatzi.me/browse") "store_url": getenv("STORE_URL", "https://plugins.deckbrew.xyz"),
"log_base_events": getenv("LOG_BASE_EVENTS", "0")=="1"
} }
class NoBaseEvents(Filter):
def filter(self, record):
return not "asyncio" in record.name
basicConfig(level=CONFIG["log_level"], format="[%(module)s][%(levelname)s]: %(message)s") basicConfig(level=CONFIG["log_level"], format="[%(module)s][%(levelname)s]: %(message)s")
for handler in root.handlers:
if not CONFIG["log_base_events"]:
print("adding filter")
handler.addFilter(NoBaseEvents())
from aiohttp.web import Application, run_app, static from aiohttp.web import Application, run_app, static
from aiohttp_jinja2 import setup as jinja_setup from aiohttp_jinja2 import setup as jinja_setup
@@ -77,11 +86,17 @@ class PluginManager:
pass pass
async def resolve_method_call(self, tab, call_id, response): async def resolve_method_call(self, tab, call_id, response):
try:
r = dumps(response)
except Exception as e:
logger.error(e)
response["result"] = str(response)
r = response
await tab._send_devtools_cmd({ await tab._send_devtools_cmd({
"id": 1, "id": 1,
"method": "Runtime.evaluate", "method": "Runtime.evaluate",
"params": { "params": {
"expression": f"resolveMethodCall({call_id}, {dumps(response)})", "expression": f"resolveMethodCall({call_id}, {r})",
"userGesture": True "userGesture": True
} }
}, receive=False) }, receive=False)
@@ -107,7 +122,12 @@ class PluginManager:
await self.resolve_method_call(tab, method["id"], res) await self.resolve_method_call(tab, method["id"], res)
async def method_call_listener(self): async def method_call_listener(self):
while True:
try:
tab = await get_tab("QuickAccess") tab = await get_tab("QuickAccess")
break
except:
await sleep(1)
await tab.open_websocket() await tab.open_websocket()
await tab._send_devtools_cmd({"id": 1, "method": "Runtime.discardConsoleEntries"}) await tab._send_devtools_cmd({"id": 1, "method": "Runtime.discardConsoleEntries"})
await tab._send_devtools_cmd({"id": 1, "method": "Runtime.enable"}) await tab._send_devtools_cmd({"id": 1, "method": "Runtime.enable"})
+1 -1
View File
@@ -19,7 +19,7 @@ class Utilities:
async def http_request(self, method="", url="", **kwargs): async def http_request(self, method="", url="", **kwargs):
async with ClientSession() as web: async with ClientSession() as web:
res = await web.request(method, url, **kwargs) async with web.request(method, url, **kwargs) as res:
return { return {
"status": res.status, "status": res.status,
"headers": dict(res.headers), "headers": dict(res.headers),