Skip to content

Commit

Permalink
fix always create connection
Browse files Browse the repository at this point in the history
  • Loading branch information
MehmedGIT committed Mar 6, 2024
1 parent 0210ebf commit be8c4c3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
7 changes: 7 additions & 0 deletions src/utils/operandi_utils/hpc/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,16 @@ def create_sftp_client(self) -> SFTPClient:
def is_transport_responsive(transport: Transport) -> bool:
if not transport:
return False
if not transport.is_authenticated():
return False
if not transport.is_active():
return False
if not transport.is_alive():
return False
try:
# Sometimes is_active() returns false-positives, hence the extra check
transport.send_ignore()
# Nevertheless this still returns false-positives...!!!
return True
except EOFError:
return False
Expand Down Expand Up @@ -207,6 +212,8 @@ def reconnect_if_required(
hpc_host = self.last_used_hpc_host
if not proxy_host:
proxy_host = self.last_used_proxy_host

# Always reconnect.
# if not self.is_ssh_connection_still_responsive(self.ssh_proxy_client):
self.log.warning("The connection to proxy server is not responsive, trying to open a new connection")
self.ssh_proxy_client = self.connect_to_proxy_server(host=proxy_host, port=proxy_port)
Expand Down
8 changes: 3 additions & 5 deletions src/utils/operandi_utils/hpc/transfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def __init__(
)

def put_batch_script(self, batch_script_id: str) -> str:
self.recreate_sftp_if_required()
local_batch_script_path = join(dirname(__file__), "batch_scripts", batch_script_id)
hpc_batch_script_path = join(self.batch_scripts_dir, batch_script_id)
self.put_file(
Expand Down Expand Up @@ -87,6 +88,7 @@ def create_slurm_workspace_zip(
return dst_zip_path

def put_slurm_workspace(self, local_src_slurm_zip: str, workflow_job_id: str) -> str:
self.recreate_sftp_if_required()
self.log.info(f"Workflow job id to be used: {workflow_job_id}")
hpc_dst_slurm_zip = join(self.slurm_workspaces_dir, f"{workflow_job_id}.zip")
self.put_file(local_src=local_src_slurm_zip, remote_dst=hpc_dst_slurm_zip)
Expand Down Expand Up @@ -125,6 +127,7 @@ def pack_and_put_slurm_workspace(
return local_src_slurm_zip, hpc_dst

def get_and_unpack_slurm_workspace(self, ocrd_workspace_dir: str, workflow_job_dir: str):
self.recreate_sftp_if_required()
self.log.info(f"Entering get_and_unpack_slurm_workspace")
self.log.info(f"ocrd_workspace_dir: {ocrd_workspace_dir}")
self.log.info(f"workflow_job_dir: {workflow_job_dir}")
Expand Down Expand Up @@ -209,7 +212,6 @@ def get_and_unpack_slurm_workspace(self, ocrd_workspace_dir: str, workflow_job_d
self.log.info(f"Leaving get_and_unpack_slurm_workspace")

def mkdir_p(self, remotepath, mode=0o766):
self.recreate_sftp_if_required()
if remotepath == '/':
self.sftp_client.chdir('/') # absolute path so change directory to root
return False
Expand All @@ -225,7 +227,6 @@ def mkdir_p(self, remotepath, mode=0o766):
return True

def get_file(self, remote_src, local_dst):
self.recreate_sftp_if_required()
makedirs(name=Path(local_dst).parent.absolute(), exist_ok=True)
self.sftp_client.get(remotepath=remote_src, localpath=local_dst)

Expand All @@ -235,7 +236,6 @@ def get_dir(self, remote_src, local_dst, mode=0o766):
The remote source directory needs to exist.
All subdirectories in source are created under destination.
"""
self.recreate_sftp_if_required()
makedirs(name=local_dst, mode=mode, exist_ok=True)
for item in self.sftp_client.listdir(remote_src):
item_src = join(remote_src, item)
Expand All @@ -246,7 +246,6 @@ def get_dir(self, remote_src, local_dst, mode=0o766):
self.get_file(remote_src=item_src, local_dst=item_dst)

def put_file(self, local_src, remote_dst):
self.recreate_sftp_if_required()
self.mkdir_p(remotepath=str(Path(remote_dst).parent.absolute()))
self.sftp_client.put(localpath=local_src, remotepath=remote_dst)

Expand All @@ -256,7 +255,6 @@ def put_dir(self, local_src, remote_dst, mode=0o766):
The remote destination directory needs to exist.
All subdirectories in source are created under destination.
"""
self.recreate_sftp_if_required()
self.mkdir_p(remotepath=remote_dst, mode=mode)
for item in listdir(local_src):
item_src = join(local_src, item)
Expand Down

0 comments on commit be8c4c3

Please sign in to comment.