Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add retries to SCPMover.copy() #153

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 38 additions & 18 deletions trollmoves/movers.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,43 @@ def move(self):

def copy(self):
"""Upload the file."""
from scp import SCPError

retries = 3
success = False
while retries > 0:
retries -= 1

try:
scp = self._get_scp_client()
scp.put(self.origin, self.destination.path)
success = True
break
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need the break here if you break on success further down?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was already pretty tired when writing that, so probably not 😅

except OSError as osex:
if osex.errno == 2:
LOGGER.error("No such file or directory. File not transfered: "
"%s. Original error message: %s",
self.origin, str(osex))
else:
LOGGER.error("OSError in scp.put: %s", str(osex))
raise
except SCPError as err:
LOGGER.error("SCP failed: %s", str(err))
except Exception as err:
LOGGER.error("Something went wrong with scp: %s", str(err))
LOGGER.error("Exception name %s", type(err).__name__)
LOGGER.error("Exception args %s", str(err.args))
raise
finally:
scp.close()

if success:
break

LOGGER.error("Retrying transfer in 2 seconds")
time.sleep(2)

def _get_scp_client(self):
from scp import SCPClient

ssh_connection = self.get_connection(self.destination.hostname,
Expand All @@ -369,24 +406,7 @@ def copy(self):
LOGGER.error("Failed to initiate SCPClient: %s", str(err))
ssh_connection.close()
raise

try:
scp.put(self.origin, self.destination.path)
except OSError as osex:
if osex.errno == 2:
LOGGER.error("No such file or directory. File not transfered: "
"%s. Original error message: %s",
self.origin, str(osex))
else:
LOGGER.error("OSError in scp.put: %s", str(osex))
raise
except Exception as err:
LOGGER.error("Something went wrong with scp: %s", str(err))
LOGGER.error("Exception name %s", type(err).__name__)
LOGGER.error("Exception args %s", str(err.args))
raise
finally:
scp.close()
return scp


class SftpMover(Mover):
Expand Down