44import sys
55
66import backoff
7+ from backoff import expo # Import the 'expo' for exponential backoff
8+ from backoff .types import Details
79
810
911# Configure logging
1517logger = logging .getLogger (__name__ )
1618
1719
20+ def _log_backoff (details : Details ) -> None :
21+ """Logs backoff attempts."""
22+ logger .warning (
23+ f"Backing off { details ['wait' ]:0.1f} s after { details ['tries' ]} tries "
24+ f"for function: { details ['target' ].__qualname__ } "
25+ f"with args: { details ['args' ]} and kwargs: { details ['kwargs' ]} "
26+ )
27+
28+
1829@backoff .on_exception (
19- backoff . expo ,
30+ expo , # Use expo for exponential backoff
2031 (ConnectionRefusedError , socket .timeout ), # Handle timeout explicitly
2132 max_time = float (os .environ .get ("INPUT_MAXTIME" , "60" )), # Default to "60" and ensure numeric
33+ on_backoff = _log_backoff ,
2234 logger = logger , # Integrate backoff with the configured logger
2335)
2436def netcat (host : str , port : int , content : str | None = None ) -> None :
@@ -34,7 +46,7 @@ def netcat(host: str, port: int, content: str | None = None) -> None:
3446 try :
3547 with socket .socket (socket .AF_INET , socket .SOCK_STREAM ) as s :
3648 s .settimeout (10 ) # Add a timeout to the socket operations
37- s .connect ((host , port ))
49+ s .connect ((host , int ( port ) ))
3850 if content :
3951 s .sendall (content .encode ())
4052 s .shutdown (socket .SHUT_WR ) # Inform server no more data will be sent
@@ -62,12 +74,12 @@ def main() -> None:
6274 Main function to execute the netcat operation and set output.
6375 """
6476 my_host = os .environ ["INPUT_REMOTEHOST" ]
65- my_port = int ( os .environ ["INPUT_REMOTEPORT" ]) # Type conversion and error handling
77+ my_port = os .environ ["INPUT_REMOTEPORT" ] # keep as string
6678 try :
67- netcat (my_host , my_port )
68- except backoff .exceptions .MaxRetriesError : # Specific exception from backoff
69- logger .error ("Max retries reached. Failed to connect. " )
70- print (f"::error::Timed out waiting for connection after multiple retries" ) # Github Action error annotation
79+ netcat (my_host , int ( my_port )) # Convert to int here
80+ except backoff .exceptions .PermanentError as e : # Correct Exception type
81+ logger .error (f" Failed to connect after retries: { e } " )
82+ print (f"::error::Failed to connect after retries: { e } " ) # Github Action error annotation
7183 sys .exit (1 ) # Indicate failure to github actions
7284 except Exception as e :
7385 logger .error (f"An unexpected error occurred: { e } " )
0 commit comments