Skip to content

Commit

Permalink
Using 'with' statement to manage subprocess
Browse files Browse the repository at this point in the history
  • Loading branch information
nilupulmanodya committed Nov 15, 2023
1 parent 6ed0a5b commit f80b46b
Showing 1 changed file with 23 additions and 23 deletions.
46 changes: 23 additions & 23 deletions mslib/mscolab/mscolab.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,29 +275,29 @@ def handle_mscolab_metadata_init(repo_exists):
try:
command = ["python", os.path.join("mslib", "mscolab", "mscolab.py"),
"start"] if repo_exists else ["mscolab", "start"]
process = subprocess.Popen(command)

# maximum retires for connecting mscolab
max_retries = 5
# retry interval if retry failed
retry_interval = 1

for _ in range(max_retries):
try:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
sock.connect(('localhost', 8083))
cmd_curl = ["curl", "http://localhost:8083/metadata/localhost_test_idp",
"-o", os.path.join(mscolab_settings.MSCOLAB_SSO_DIR, "metadata_sp.xml")]
subprocess.run(cmd_curl, check=True)
process.terminate()
logging.info('mscolab metadata file generated succesfully')
return True
except socket.error:
time.sleep(retry_interval)

if _ == max_retries:
print(f'mscolab server failed to start after {max_retries} retries')
return False
with subprocess.Popen(command) as process:
# maximum retires for connecting mscolab
max_retries = 5
# retry interval if retry failed
retry_interval = 1

for _ in range(max_retries):
try:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
sock.connect(('localhost', 8083))
cmd_curl = ["curl", "http://localhost:8083/metadata/localhost_test_idp",
"-o", os.path.join(mscolab_settings.MSCOLAB_SSO_DIR, "metadata_sp.xml")]
subprocess.run(cmd_curl, check=True)
process.terminate()
logging.info('mscolab metadata file generated succesfully')
return True
except socket.error:
time.sleep(retry_interval)

if _ == max_retries:
print(f'error while generating metadata,'
f'mscolab server failed to start after {max_retries} retries')
return False

except subprocess.CalledProcessError as error:
print(f"Error while generating metadata file for the mscolab server: {error}")
Expand Down

0 comments on commit f80b46b

Please sign in to comment.