From b11e6f4978fbc47cbdc491e61a162bebe9cf7d0d Mon Sep 17 00:00:00 2001 From: sparky8512 <76499194+sparky8512@users.noreply.github.com> Date: Wed, 2 Mar 2022 14:48:51 -0800 Subject: [PATCH] Catch and ignore KeyboardInterrupt exception Per discussion in PR #40 comments. This will prevent a stack dump from littering the output when interrupting script run via Control-C. That was useful to have for a while, but probably is not anymore. Also, fix up a few corner cases where rc could be used without it having been set. --- dish_grpc_influx.py | 3 ++- dish_grpc_influx2.py | 2 +- dish_grpc_mqtt.py | 6 ++++-- dish_grpc_sqlite.py | 2 +- dish_grpc_text.py | 3 ++- 5 files changed, 10 insertions(+), 6 deletions(-) diff --git a/dish_grpc_influx.py b/dish_grpc_influx.py index 81b2766..dc76832 100644 --- a/dish_grpc_influx.py +++ b/dish_grpc_influx.py @@ -311,6 +311,7 @@ def main(): # ...unless influxdb-python package version is too old gstate.influx_client = InfluxDBClient(**opts.icargs) + rc = 0 try: next_loop = time.monotonic() while True: @@ -321,7 +322,7 @@ def main(): time.sleep(next_loop - now) else: break - except Terminated: + except (KeyboardInterrupt, Terminated): pass finally: loop_body(opts, gstate, shutdown=True) diff --git a/dish_grpc_influx2.py b/dish_grpc_influx2.py index ba6f66f..0f5f955 100644 --- a/dish_grpc_influx2.py +++ b/dish_grpc_influx2.py @@ -307,7 +307,7 @@ def main(): time.sleep(next_loop - now) else: break - except Terminated: + except (KeyboardInterrupt, Terminated): pass finally: loop_body(opts, gstate, shutdown=True) diff --git a/dish_grpc_mqtt.py b/dish_grpc_mqtt.py index 4266552..c4cb838 100644 --- a/dish_grpc_mqtt.py +++ b/dish_grpc_mqtt.py @@ -184,6 +184,7 @@ def main(): signal.signal(signal.SIGTERM, handle_sigterm) + rc = 0 try: next_loop = time.monotonic() while True: @@ -194,11 +195,12 @@ def main(): time.sleep(next_loop - now) else: break - except Terminated: + except (KeyboardInterrupt, Terminated): pass finally: gstate.shutdown() - sys.exit(rc) + + sys.exit(rc) if __name__ == '__main__': diff --git a/dish_grpc_sqlite.py b/dish_grpc_sqlite.py index 53e8499..9fe529e 100644 --- a/dish_grpc_sqlite.py +++ b/dish_grpc_sqlite.py @@ -303,7 +303,7 @@ def main(): except sqlite3.Error as e: logging.error("Database error: %s", e) rc = 1 - except Terminated: + except (KeyboardInterrupt, Terminated): pass finally: loop_body(opts, gstate, shutdown=True) diff --git a/dish_grpc_text.py b/dish_grpc_text.py index 69743be..29b844e 100644 --- a/dish_grpc_text.py +++ b/dish_grpc_text.py @@ -274,6 +274,7 @@ def main(): sys.exit(1) signal.signal(signal.SIGTERM, handle_sigterm) + rc = 0 try: next_loop = time.monotonic() while True: @@ -284,7 +285,7 @@ def main(): time.sleep(next_loop - now) else: break - except Terminated: + except (KeyboardInterrupt, Terminated): pass finally: loop_body(opts, gstate, print_file, shutdown=True)