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.
This commit is contained in:
DevTekVE
2024-06-19 21:00:02 +02:00
parent 62b0c9f8be
commit 524b97f8d4
+5 -6
View File
@@ -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()