diff --git a/speasy/core/http.py b/speasy/core/http.py index f593b623..9b71143f 100644 --- a/speasy/core/http.py +++ b/speasy/core/http.py @@ -2,6 +2,7 @@ import logging import platform import re +import time from functools import partial from typing import Optional @@ -109,7 +110,7 @@ def urlopen(url, timeout: int = DEFAULT_TIMEOUT, headers: dict = None): def is_server_up(url: Optional[str] = None, host: Optional[str] = None, port: Optional[int] = None, - timeout: int = 5) -> bool: + timeout: int = 5, retries=5) -> bool: """Checks if a server is up and running. Parameters @@ -140,10 +141,16 @@ def is_server_up(url: Optional[str] = None, host: Optional[str] = None, port: Op raise ValueError("Either url or host and port must be provided") import socket try: - sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - sock.settimeout(timeout) - result = sock.connect_ex((host, int(port))) - return result == 0 + for _ in range(retries): + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + sock.settimeout(timeout) + result = sock.connect_ex((host, int(port))) + sock.close() + if result == 0: + return True + log.debug(f"Server {host}:{port} not up yet, retrying in 1 second") + time.sleep(1.) + return False except Exception as e: log.error(f"Error while checking server status: {e}") return False