Skip to content

Commit

Permalink
Merge pull request red-hat-storage#3900 from psathyan/fixLMdataRead
Browse files Browse the repository at this point in the history
Further Improvements to long_running method
  • Loading branch information
mergify[bot] authored Jul 22, 2024
2 parents 9f59e2c + c83af7c commit 8a7f009
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions ceph/ceph.py
Original file line number Diff line number Diff line change
Expand Up @@ -1145,6 +1145,18 @@ class CommandFailed(Exception):
pass


class TimeoutException(Exception):
"""Operation timeout exception."""

pass


def check_timeout(end_time, timeout):
"""Raises an exception when current time is greater"""
if timeout and datetime.datetime.now() >= end_time:
raise TimeoutException("Command exceed the allocated execution time.")


class RolesContainer(object):
"""
Container for single or multiple node roles.
Expand Down Expand Up @@ -1521,7 +1533,7 @@ def long_running(self, **kw):
channel.settimeout(timeout)

# A mismatch between stdout and stderr streams have been observed hence
# combining the streams and logging is set to debug level only.
# combining the streams and logging is set to debug level.
channel.set_combine_stderr(True)
channel.exec_command(cmd)

Expand All @@ -1532,29 +1544,28 @@ def long_running(self, **kw):

while not channel.exit_status_ready():
# Prevent high resource consumption
sleep(2)

sleep(1)
if channel.recv_ready():
data = channel.recv(1024)
while data:
for line in data.splitlines():
logger.debug(line)

check_timeout(_end_time, timeout)
data = channel.recv(1024)

# time check - raise exception when exceeded.
if timeout and datetime.datetime.now() > _end_time:
channel.close()
raise SocketTimeoutException(
f"{cmd} failed to complete within {timeout}s"
)
check_timeout(_end_time, timeout)

logger.info(f"Command completed on {datetime.datetime.now()}")
return channel.recv_exit_status()

except socket.timeout as terr:
logger.error(f"Command failed to execute within {timeout} seconds.")
raise SocketTimeoutException(terr)
except TimeoutException as tex:
channel.close()
logger.error(f"{cmd} failed to execute within {timeout}s.")
raise CommandFailed(tex)
except BaseException as be: # noqa
logger.exception(be)
raise CommandFailed(be)
Expand Down

0 comments on commit 8a7f009

Please sign in to comment.