From 6b82684d5adb1b15a76fce0cf12750ce6205f7ce Mon Sep 17 00:00:00 2001 From: Luna Mendes Date: Mon, 15 Oct 2018 19:34:54 -0300 Subject: [PATCH] adapters: handle 204's on HttpAdapter - adapters: add debug info on ping errors --- elstat/adapters.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/elstat/adapters.py b/elstat/adapters.py index 812aa7b..e011b69 100644 --- a/elstat/adapters.py +++ b/elstat/adapters.py @@ -70,12 +70,13 @@ class PingAdapter(Adapter): # we dont need to keep sending err_msg on alive scenarios return cls._construct(alive, latency) - # only log on errors - worker.log.info(f'alive={alive} latency={latency}ms') - err = PING_ERROR_RGX.search(out) err_msg = err.group(2) if not alive and err else 'packet lost' + # only log on errors + worker.log.info(f'alive={alive} latency={latency}ms ' + f'err={err_msg!r} out={out}') + return cls._construct(alive, latency, err_msg) @@ -87,9 +88,11 @@ class HttpAdapter(Adapter): } @classmethod - def get_phrase(cls, status: int): - with open('./status.json', 'r') as fd: - statuses = json.load(fd) + def get_phrase(cls, status: int) -> str: + """Get a string representing the error + given by the http status code.""" + with open('./status.json', 'r') as status_file: + statuses = json.load(status_file) try: return statuses[str(status)] @@ -105,7 +108,8 @@ class HttpAdapter(Adapter): resp = await session.get(f'{adp_args["url"]}') t_end = time.monotonic() - succ = resp.status == 200 + # handle both 200 and 204 + succ = resp.status in (200, 204) latency = round((t_end - t_start) * 1000) # drop latency to 0 to signal a non-success