moninj,corelog: fix/cleanup exception handling (#1897)

This commit is contained in:
Sebastien Bourdeauducq 2022-07-02 17:48:18 +08:00
parent 02b086c9e5
commit 388b81af19
2 changed files with 46 additions and 40 deletions

View File

@ -91,6 +91,10 @@ class CommMonInj:
self.injection_status_cb(channel, override, value) self.injection_status_cb(channel, override, value)
else: else:
raise ValueError("Unknown packet type", ty) raise ValueError("Unknown packet type", ty)
except asyncio.CancelledError:
raise
except:
logger.error("Moninj connection terminating with exception", exc_info=True)
finally: finally:
if self.disconnect_cb is not None: if self.disconnect_cb is not None:
self.disconnect_cb() self.disconnect_cb()

View File

@ -40,44 +40,49 @@ async def get_logs_sim(host):
async def get_logs(host): async def get_logs(host):
reader, writer = await async_open_connection( try:
host, reader, writer = await async_open_connection(
1380, host,
after_idle=1, 1380,
interval=1, after_idle=1,
max_fails=3, interval=1,
) max_fails=3,
writer.write(b"ARTIQ management\n") )
endian = await reader.readexactly(1) writer.write(b"ARTIQ management\n")
if endian == b"e": endian = await reader.readexactly(1)
endian = "<" if endian == b"e":
elif endian == b"E": endian = "<"
endian = ">" elif endian == b"E":
else: endian = ">"
raise IOError("Incorrect reply from device: expected e/E.") else:
writer.write(struct.pack("B", Request.PullLog.value)) raise IOError("Incorrect reply from device: expected e/E.")
await writer.drain() writer.write(struct.pack("B", Request.PullLog.value))
await writer.drain()
while True: while True:
length, = struct.unpack(endian + "l", await reader.readexactly(4)) length, = struct.unpack(endian + "l", await reader.readexactly(4))
log = await reader.readexactly(length) log = await reader.readexactly(length)
for line in log.decode("utf-8").splitlines(): for line in log.decode("utf-8").splitlines():
m = re.match(r"^\[.+?\] (TRACE|DEBUG| INFO| WARN|ERROR)\((.+?)\): (.+)$", line) m = re.match(r"^\[.+?\] (TRACE|DEBUG| INFO| WARN|ERROR)\((.+?)\): (.+)$", line)
levelname = m.group(1) levelname = m.group(1)
if levelname == 'TRACE': if levelname == 'TRACE':
level = logging.TRACE level = logging.TRACE
elif levelname == 'DEBUG': elif levelname == 'DEBUG':
level = logging.DEBUG level = logging.DEBUG
elif levelname == ' INFO': elif levelname == ' INFO':
level = logging.INFO level = logging.INFO
elif levelname == ' WARN': elif levelname == ' WARN':
level = logging.WARN level = logging.WARN
elif levelname == 'ERROR': elif levelname == 'ERROR':
level = logging.ERROR level = logging.ERROR
name = 'firmware.' + m.group(2).replace('::', '.') name = 'firmware.' + m.group(2).replace('::', '.')
text = m.group(3) text = m.group(3)
log_with_name(name, level, text) log_with_name(name, level, text)
except asyncio.CancelledError:
raise
except:
logger.error("Logging connection terminating with exception", exc_info=True)
def main(): def main():
@ -99,8 +104,7 @@ def main():
_, pending = loop.run_until_complete(asyncio.wait( _, pending = loop.run_until_complete(asyncio.wait(
[signal_handler.wait_terminate(), [signal_handler.wait_terminate(),
server.wait_terminate(), server.wait_terminate(),
get_logs_task get_logs_task],
],
return_when=asyncio.FIRST_COMPLETED)) return_when=asyncio.FIRST_COMPLETED))
for task in pending: for task in pending:
task.cancel() task.cancel()
@ -108,8 +112,6 @@ def main():
loop.run_until_complete(server.stop()) loop.run_until_complete(server.stop())
finally: finally:
pass pass
except Exception:
logger.error("Termination due to exception", exc_info=True)
finally: finally:
signal_handler.teardown() signal_handler.teardown()
finally: finally: