Skip to content

Commit

Permalink
feat(ssh): add retry loop around SSH Exceptions (#68)
Browse files Browse the repository at this point in the history
* feat(ssh): add retry loop around ssh exceptions

* refactor(ssh): remove useless stdin

* refactor(ssh-connection): use a "retry" decorator

---------

Co-authored-by: Laurent LAPORTE <[email protected]>
(cherry picked from commit 3e0d436)
  • Loading branch information
MartinBelthle authored and laurent-laporte-pro committed Apr 11, 2024
1 parent c4e647d commit 95b0164
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ npx auto-changelog -l false --hide-empty-releases -v v1.3.1 -o CHANGES.out.md

- build: add a script to bump the version

### Changed

- feat(ssh): add retry loop around SSH Exceptions [`#68`](https://github.com/AntaresSimulatorTeam/antares-launcher/pull/68)


## [1.3.1] - 2023-09-26

Expand Down
42 changes: 42 additions & 0 deletions antareslauncher/remote_environnement/ssh_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,48 @@
DIRECTORY_NOT_FOUND_ERROR = "Directory not found error"


def retry(
exception: t.Type[Exception],
*exceptions: t.Type[Exception],
delay_sec: float = 5,
max_retry: int = 5,
msg_fmt: str = "Retrying in {delay_sec} seconds...",
):
"""
Decorator to retry a function call if it raises an exception.
Args:
exception: The exception to catch.
exceptions: Additional exceptions to catch.
delay_sec: The delay (in seconds) between each retry.
max_retry: The maximum number of retries.
msg_fmt: The message to display when retrying, with the following format keys:
- delay_sec: The delay (in seconds) between each retry.
- remaining: The number of remaining retries.
Returns:
The decorated function.
"""

def decorator(func): # type: ignore
@functools.wraps(func)
def wrapper(*args, **kwargs): # type: ignore
for attempt in range(max_retry):
try:
return func(*args, **kwargs)
except (exception, *exceptions):
logger = logging.getLogger(__name__)
remaining = max_retry - attempt - 1
logger.warning(msg_fmt.format(delay_sec=delay_sec, remaining=remaining))
time.sleep(delay_sec)
# Last attempt
return func(*args, **kwargs)

return wrapper

return decorator


def retry(
exception: t.Type[Exception],
*exceptions: t.Type[Exception],
Expand Down

0 comments on commit 95b0164

Please sign in to comment.