This is an automated email from the ASF dual-hosted git repository.

potiuk pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git


The following commit(s) were added to refs/heads/main by this push:
     new 6b2a0cb3c8 Refactor attempting loops in breeze (#34054)
6b2a0cb3c8 is described below

commit 6b2a0cb3c84eeeaec013c96153c6b9538c6e74c4
Author: Miroslav Šedivý <[email protected]>
AuthorDate: Wed Sep 6 21:08:45 2023 +0000

    Refactor attempting loops in breeze (#34054)
---
 .../src/airflow_breeze/commands/developer_commands.py      | 14 ++++++--------
 dev/breeze/src/airflow_breeze/utils/kubernetes_utils.py    | 10 +++++-----
 2 files changed, 11 insertions(+), 13 deletions(-)

diff --git a/dev/breeze/src/airflow_breeze/commands/developer_commands.py 
b/dev/breeze/src/airflow_breeze/commands/developer_commands.py
index 6a7e6ac00d..fded9415c0 100644
--- a/dev/breeze/src/airflow_breeze/commands/developer_commands.py
+++ b/dev/breeze/src/airflow_breeze/commands/developer_commands.py
@@ -533,9 +533,8 @@ def static_checks(
             f"[info]Trying to install the environments up to 
{max_initialization_attempts} "
             f"times in case of flakiness[/]"
         )
-        i = 0
-        while True:
-            get_console().print(f"[info]Attempt number {i + 1} to install 
pre-commit environments")
+        for attempt in range(1, 1 + max_initialization_attempts):
+            get_console().print(f"[info]Attempt number {attempt} to install 
pre-commit environments")
             initialization_result = run_command(
                 [sys.executable, "-m", "pre_commit", "install", 
"--install-hooks"],
                 check=False,
@@ -544,11 +543,10 @@ def static_checks(
             )
             if initialization_result.returncode == 0:
                 break
-            get_console().print(f"[warning]Attempt number {i + 1} failed - 
retrying[/]")
-            if i == max_initialization_attempts - 1:
-                get_console().print("[error]Could not install pre-commit 
environments[/]")
-                sys.exit(initialization_result.returncode)
-            i += 1
+            get_console().print(f"[warning]Attempt number {attempt} failed - 
retrying[/]")
+        else:
+            get_console().print("[error]Could not install pre-commit 
environments[/]")
+            sys.exit(initialization_result.returncode)
 
     command_to_execute = [sys.executable, "-m", "pre_commit", "run"]
     if not one_or_none_set([last_commit, commit_ref, only_my_changes, 
all_files]):
diff --git a/dev/breeze/src/airflow_breeze/utils/kubernetes_utils.py 
b/dev/breeze/src/airflow_breeze/utils/kubernetes_utils.py
index fe6ac68e75..3bed798c22 100644
--- a/dev/breeze/src/airflow_breeze/utils/kubernetes_utils.py
+++ b/dev/breeze/src/airflow_breeze/utils/kubernetes_utils.py
@@ -16,6 +16,7 @@
 # under the License.
 from __future__ import annotations
 
+import itertools
 import os
 import random
 import re
@@ -473,9 +474,8 @@ def _attempt_to_connect(port_number: int, output: Output | 
None, wait_seconds: i
 
     start_time = datetime.now(timezone.utc)
     sleep_seconds = 5
-    num_try = 1
-    while True:
-        get_console(output=output).print(f"[info]Connecting to 
localhost:{port_number}. Num try: {num_try}")
+    for attempt in itertools.count(1):
+        get_console(output=output).print(f"[info]Connecting to 
localhost:{port_number}. Num try: {attempt}")
         try:
             response = requests.head(f"http://localhost:{port_number}/health";)
         except ConnectionError:
@@ -500,10 +500,10 @@ def _attempt_to_connect(port_number: int, output: Output 
| None, wait_seconds: i
         if current_time - start_time > timedelta(seconds=wait_seconds):
             if wait_seconds > 0:
                 get_console(output=output).print(f"[error]More than 
{wait_seconds} passed. Exiting.")
-            return False
+            break
         get_console(output=output).print(f"Sleeping for {sleep_seconds} 
seconds.")
         sleep(sleep_seconds)
-        num_try += 1
+    return False
 
 
 def print_cluster_urls(

Reply via email to