Last active
January 28, 2020 14:21
-
-
Save FilippoVajana/678ab1849998fbc74be53a1b6bf5c85b to your computer and use it in GitHub Desktop.
op.pla subprocess logging
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Optimize saving the stdout of a subprocess. | |
""" | |
import os | |
import subprocess | |
import sys | |
import io | |
def run_command(cmd, log_file_path, timeout=28800, timeout_rerun=3): | |
""" | |
Runs the input command via subprocess module with a default timeout of 8 hours. | |
""" | |
def write_log(log): | |
"""Appends the input to the log file.""" | |
with open(log_file_path, "a") as f: | |
f.write(log) | |
try: | |
process = subprocess.run( | |
cmd, | |
shell=False, | |
stdout=subprocess.PIPE, | |
stderr=subprocess.STDOUT, | |
timeout=timeout, | |
universal_newlines=True) | |
write_log(process.stdout) | |
return process | |
except subprocess.CalledProcessError as cmdError: | |
write_log(cmdError.stdout) | |
write_log(str(cmdError)) | |
return process | |
except subprocess.TimeoutExpired as cmdTimeout: | |
write_log(cmdTimeout.stdout) | |
write_log(str(cmdTimeout)) | |
# relaunch the command after timeout. | |
if timeout_rerun > 0: | |
write_log("\n\n###########\nRELAUNCH AFTER TIMEOUT\n###########\n\n") | |
# recursive call in which the "timeout_rerun" arg is used as a counter. | |
rerun_count = timeout_rerun - 1 | |
process = run_command(cmd, log_file_path, | |
timeout, timeout_rerun=rerun_count) | |
return process | |
else: | |
write_log("\n\n###########\nTOO MANY TIMEOUTS\n###########\n\n") | |
raise Exception("Too many timeouts") | |
if __name__ == "__main__": | |
log_file_path = "test_log.txt" | |
# remove old log file. | |
if os.path.isfile(log_file_path): | |
os.remove(log_file_path) | |
# run the command and check if the return code is not 0. | |
try: | |
cmd_result = run_command( | |
["python3", "cmd_writer.py"], log_file_path, timeout=10) | |
if cmd_result.returncode != 0: | |
with open(log_file_path, "a") as log_file: | |
log_file.write( | |
f"Unexpected Return Code: {cmd_result.returncode}") | |
raise Exception() | |
except Exception: | |
sys.exit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment