From 38eb8bb2776c3bea75d17aa1b0ea511b0f31d57c Mon Sep 17 00:00:00 2001 From: Jan Katins Date: Wed, 10 Jun 2020 22:55:33 +0200 Subject: [PATCH] Ignore any KeyboardInterrupt in system stats process no matter where --- mara_pipelines/logging/system_statistics.py | 79 ++++++++++----------- 1 file changed, 39 insertions(+), 40 deletions(-) diff --git a/mara_pipelines/logging/system_statistics.py b/mara_pipelines/logging/system_statistics.py index 5e5d2f3..c09547c 100644 --- a/mara_pipelines/logging/system_statistics.py +++ b/mara_pipelines/logging/system_statistics.py @@ -64,47 +64,46 @@ def mem_usage(): def swap_usage(): swap = psutil.swap_memory() return 100.0 * swap.used / swap.total if swap.total > 0 else None - - # immediately send event for current cpu, mem and swap usage - event_queue.put(SystemStatistics( - datetime.datetime.now(), cpu_usage=cpu_usage(), mem_usage=mem_usage(), swap_usage=swap_usage())) - period = config.system_statistics_collection_period() - - n = 0 - - # some counters on WSL1 return None because psutil thinks it's linux, - # but the linux kernel API is not implemented and fails - # This lets it always return 0 for all attributes on that counter and lets at least CPU show up - class _zero(): - def __getattr__(self, item): return 0 - zero = _zero() - - # capture current disc and net state for later diff - discs_last = psutil.disk_io_counters() or zero - nets_last = psutil.net_io_counters() or zero - mb = 1024 * 1024 try: + # immediately send event for current cpu, mem and swap usage + event_queue.put(SystemStatistics( + datetime.datetime.now(), cpu_usage=cpu_usage(), mem_usage=mem_usage(), swap_usage=swap_usage())) + period = config.system_statistics_collection_period() + + n = 0 + + # some counters on WSL1 return None because psutil thinks it's linux, + # but the linux kernel API is not implemented and fails + # This lets it always return 0 for all attributes on that counter and lets at least CPU show up + class _zero(): + def __getattr__(self, item): return 0 + zero = _zero() + + # capture current disc and net state for later diff + discs_last = psutil.disk_io_counters() or zero + nets_last = psutil.net_io_counters() or zero + mb = 1024 * 1024 time.sleep(period) + + while True: + discs_cur = psutil.disk_io_counters() or zero + nets_cur = psutil.net_io_counters() or zero + event_queue.put(SystemStatistics( + datetime.datetime.now(), + disc_read=(discs_cur.read_bytes - discs_last.read_bytes) / mb / period, + disc_write=(discs_cur.write_bytes - discs_last.write_bytes) / mb / period, + net_recv=(nets_cur.bytes_recv - nets_last.bytes_recv) / mb / period, + net_sent=(nets_cur.bytes_sent - nets_last.bytes_sent) / mb / period, + cpu_usage=cpu_usage(), mem_usage=mem_usage(), swap_usage=swap_usage())) + nets_last = nets_cur + discs_last = discs_cur + + # double period every 100 measurements in order to avoid sending too many requests to frontend + n += 1 + if n % 100 == 0: + period *= 2 + time.sleep(period) except KeyboardInterrupt: + # happens e.g. when someone shuts down the webserver... + # does not bubble up, so if this get caught here it might need a second ctr+c return - while True: - discs_cur = psutil.disk_io_counters() or zero - nets_cur = psutil.net_io_counters() or zero - event_queue.put(SystemStatistics( - datetime.datetime.now(), - disc_read=(discs_cur.read_bytes - discs_last.read_bytes) / mb / period, - disc_write=(discs_cur.write_bytes - discs_last.write_bytes) / mb / period, - net_recv=(nets_cur.bytes_recv - nets_last.bytes_recv) / mb / period, - net_sent=(nets_cur.bytes_sent - nets_last.bytes_sent) / mb / period, - cpu_usage=cpu_usage(), mem_usage=mem_usage(), swap_usage=swap_usage())) - nets_last = nets_cur - discs_last = discs_cur - - # double period every 100 measurements in order to avoid sending too many requests to frontend - n += 1 - if n % 100 == 0: - period *= 2 - try: - time.sleep(period) - except KeyboardInterrupt: - break