improved active receive

This commit is contained in:
Daniel Langbein 2023-01-12 20:37:42 +01:00
parent 0e7ee3c6ca
commit 1f38720072
2 changed files with 19 additions and 3 deletions

View File

@ -35,6 +35,7 @@ def main():
inform_cmd = ['ssh', inform_cmd = ['ssh',
ssh_source, ssh_source,
f'cat {shlex.quote(str(inform_path))} && rm {shlex.quote(str(inform_path))}'] f'cat {shlex.quote(str(inform_path))} && rm {shlex.quote(str(inform_path))}']
messages = [Message1.OK.value, Message1.EOF.value]
rsync_cmd = ['rsync', rsync_cmd = ['rsync',
f'{ssh_source}:{source_chunk_path}', f'{ssh_source}:{source_chunk_path}',
f'{target_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))}"] f"printf '{Message2.OK_STR.value}' | nc -U {shlex.quote(str(in_socket))}"]
# Wait until next chunk can be transferred from sending side. # 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: if msg not in messages:
raise ValueError(f'Invalid message: {msg}') raise ValueError(f'Invalid message: {msg}')
last_chunk = msg == Message1.EOF.value last_chunk = msg == Message1.EOF.value

View File

@ -1,3 +1,4 @@
import time
from pathlib import Path from pathlib import Path
from p1st.exec_print_capture import execute_print_capture 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], 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`. Executes the given `command`.
@ -17,6 +20,7 @@ def repeat_until_successful(command: list[str],
out: str out: str
returncode, out, _err = execute_print_capture(command) returncode, out, _err = execute_print_capture(command)
if returncode == 0: if returncode == 0:
time.sleep(retry_delay_seconds)
return out return out
print(f'\n' print(f'\n'
@ -24,6 +28,12 @@ def repeat_until_successful(command: list[str],
f'\t{command}\n' f'\t{command}\n'
f'\tFor details, see above output.') f'\tFor details, see above output.')
if retries > 0:
print('\tRetrying the failed command.'
'\n')
retries = retries - 1
continue
print(f'Info:\n' print(f'Info:\n'
f'\tPlease fix the above error first. Then continue here:\n' f'\tPlease fix the above error first. Then continue here:\n'
f'\tsudo pacman -S --needed openbsd-netcat\n' f'\tsudo pacman -S --needed openbsd-netcat\n'