From 524b97f8d4d7174ae18e8badef118bc93cd961e5 Mon Sep 17 00:00:00 2001 From: DevTekVE Date: Wed, 19 Jun 2024 21:00:02 +0200 Subject: [PATCH] Refactor sunnylinkd.main exit conditions The checks for the sunnylink_enabled flag have been restructured to eliminate unnecessary loop iterations. The check has been moved out of the loop to do the evaluation only once, when exiting the loop. This significantly increases the efficiency of the code. --- system/athena/sunnylinkd.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/system/athena/sunnylinkd.py b/system/athena/sunnylinkd.py index 4715994e48..0bb4c28e16 100755 --- a/system/athena/sunnylinkd.py +++ b/system/athena/sunnylinkd.py @@ -202,13 +202,8 @@ def main(exit_event: threading.Event = None): ws_uri = SUNNYLINK_ATHENA_HOST conn_start = None conn_retries = 0 - while exit_event is None or not exit_event.is_set(): + while exit_event is None or not exit_event.is_set() and is_sunnylink_enabled: try: - if not is_sunnylink_enabled: - cloudlog.debug("Exiting sunnylinkd.main as SunnylinkEnabled is False but will retry in 60 seconds") - time.sleep(60) - break - if conn_start is None: conn_start = time.monotonic() @@ -238,6 +233,10 @@ def main(exit_event: threading.Event = None): time.sleep(backoff(conn_retries)) + if not is_sunnylink_enabled: + cloudlog.debug("Reached end of sunnylinkd.main while SunnylinkEnabled is False so will wait for 60 seconds before exiting") + time.sleep(60) + if __name__ == "__main__": main()