From 1f3872007244df93c82238354b913cba7c71bc89 Mon Sep 17 00:00:00 2001 From: Daniel Langbein Date: Thu, 12 Jan 2023 20:37:42 +0100 Subject: [PATCH] improved active receive --- src/p1st/btrfs_receive_chunks_active.py | 10 ++++++++-- src/p1st/repeat.py | 12 +++++++++++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/p1st/btrfs_receive_chunks_active.py b/src/p1st/btrfs_receive_chunks_active.py index 0420b3d..1d89904 100644 --- a/src/p1st/btrfs_receive_chunks_active.py +++ b/src/p1st/btrfs_receive_chunks_active.py @@ -35,6 +35,7 @@ def main(): inform_cmd = ['ssh', ssh_source, f'cat {shlex.quote(str(inform_path))} && rm {shlex.quote(str(inform_path))}'] + messages = [Message1.OK.value, Message1.EOF.value] rsync_cmd = ['rsync', f'{ssh_source}:{source_chunk_path}', f'{target_chunk_path}'] @@ -43,8 +44,13 @@ def main(): f"printf '{Message2.OK_STR.value}' | nc -U {shlex.quote(str(in_socket))}"] # Wait until next chunk can be transferred from sending side. - messages = [Message1.OK.value, Message1.EOF.value] - msg = repeat_until_successful(inform_cmd, user_socket) + # + # It can happen,that the sending side has not yet saved the next chunk. + # In that case `cat ...` will fail. + # We will retry 3 times, each time after 5 seconds sleep. + # + # If all 4 `cat ...` attempts failed, we wait until the user has inspected the problem. + msg = repeat_until_successful(inform_cmd, user_socket, retries=3, retry_delay_seconds=5) if msg not in messages: raise ValueError(f'Invalid message: {msg}') last_chunk = msg == Message1.EOF.value diff --git a/src/p1st/repeat.py b/src/p1st/repeat.py index b2e2a0c..bd51a7f 100644 --- a/src/p1st/repeat.py +++ b/src/p1st/repeat.py @@ -1,3 +1,4 @@ +import time from pathlib import Path from p1st.exec_print_capture import execute_print_capture @@ -5,7 +6,9 @@ from p1st.unix_sock_input import wait_for_message def repeat_until_successful(command: list[str], - socket_file: Path) -> str: + socket_file: Path, + retries: int = 0, + retry_delay_seconds: float = 5) -> str: """ Executes the given `command`. @@ -17,6 +20,7 @@ def repeat_until_successful(command: list[str], out: str returncode, out, _err = execute_print_capture(command) if returncode == 0: + time.sleep(retry_delay_seconds) return out print(f'\n' @@ -24,6 +28,12 @@ def repeat_until_successful(command: list[str], f'\t{command}\n' f'\tFor details, see above output.') + if retries > 0: + print('\tRetrying the failed command.' + '\n') + retries = retries - 1 + continue + print(f'Info:\n' f'\tPlease fix the above error first. Then continue here:\n' f'\tsudo pacman -S --needed openbsd-netcat\n'