From 3e70631471c34966eb2bf46b6155710156c363dd Mon Sep 17 00:00:00 2001 From: Justin Paul Date: Wed, 8 Mar 2023 08:45:08 -0500 Subject: [PATCH] Update python-node-exporter.py Testing ChatGPT generated optimizations which will make sure that the various stats gathering threads will be restarted if they crash --- app/python-node-exporter.py | 44 ++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/app/python-node-exporter.py b/app/python-node-exporter.py index 1dda243..fc9b30a 100644 --- a/app/python-node-exporter.py +++ b/app/python-node-exporter.py @@ -359,23 +359,6 @@ def GetDataFunc(): log.debug("Waiting 1 second for Auth Token") time.sleep(1) - -#-------Start function to maintain ZVM Authentication--------- -# run ZvmAuthHandler func in the background -background_thread = Thread(target = ZvmAuthHandler) -background_thread.start() - - -#-----------------Start Data collector Thread----------------- -# run GetDataFunc func in the background -background_thread = Thread(target = GetDataFunc) -background_thread.start() - -#-----------------Start Data collector Thread----------------- -# run GetDataFunc func in the background -background_thread = Thread(target = GetStatsFunc) -background_thread.start() - #----------------run http server on port 9999----------------- def WebServer(): PORT = 9999 @@ -386,6 +369,27 @@ def WebServer(): print("serving at port", PORT) httpd.serve_forever() -# run WebServer func in the background -background_thread = Thread(target = WebServer) -background_thread.start() +def start_thread(target_func): + # start a new thread + thread = threading.Thread(target=target_func) + thread.start() + # return the thread object + return thread + +# start the threads +auth_thread = start_thread(ZvmAuthHandler) +data_thread = start_thread(GetDataFunc) +stats_thread = start_thread(GetStatsFunc) + +# loop indefinitely +while True: + # check if any thread has crashed + if not auth_thread.is_alive(): + # restart the thread + auth_thread = start_thread(ZvmAuthHandler) + if not data_thread.is_alive(): + # restart the thread + data_thread = start_thread(GetDataFunc) + if not stats_thread.is_alive(): + # restart the thread + stats_thread = start_thread(GetStatsFunc)