Skip to content

Commit

Permalink
Merge branch 'pr/859' into allow-pipes-with-logging-subprocess
Browse files Browse the repository at this point in the history
And also only log piped output if it exists
# Conflicts:
#	esrally/utils/process.py
  • Loading branch information
dliappis committed Jan 10, 2020
1 parent 43d5362 commit 2f263cb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
2 changes: 1 addition & 1 deletion esrally/mechanic/launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ def _start_process(binary_path, env):
os.chdir(binary_path)
cmd = [io.escape_path(os.path.join(".", "bin", "elasticsearch"))]
cmd.extend(["-d", "-p", "pid"])
ret = process.run_subprocess_with_logging(command_line=" ".join(cmd), env=env)
ret = process.run_subprocess_with_logging(command_line=" ".join(cmd), env=env, detach=True)
if ret != 0:
msg = "Daemon startup failed with exit code [{}]".format(ret)
logging.error(msg)
Expand Down
16 changes: 12 additions & 4 deletions esrally/utils/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def exit_status_as_bool(runnable, quiet=False):
return False


def run_subprocess_with_logging(command_line, header=None, level=logging.INFO, stdin=None, env=None):
def run_subprocess_with_logging(command_line, header=None, level=logging.INFO, stdin=None, env=None, detach=False):
"""
Runs the provided command line in a subprocess. All output will be captured by a logger.
Expand All @@ -70,19 +70,27 @@ def run_subprocess_with_logging(command_line, header=None, level=logging.INFO, s
:param stdin: The stdout object returned by subprocess.Popen(stdout=PIPE) allowing chaining of shell operations with pipes
(default: None).
:param env: Use specific environment variables (default: None).
:param detach: Whether to detach this process from its parent process (default: False).
:return: The process exit code as an int.
"""
logger = logging.getLogger(__name__)
logger.debug("Running subprocess [%s] with logging.", command_line)
command_line_args = shlex.split(command_line)
pre_exec = os.setpgrp if detach else None
if header is not None:
logger.info(header)

with subprocess.Popen(command_line_args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env=env,
stdin=stdin if stdin else None) as command_line_process:
# pylint: disable=subprocess-popen-preexec-fn
with subprocess.Popen(command_line_args,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
env=env,
stdin=stdin if stdin else None,
preexec_fn=pre_exec) as command_line_process:
if stdin:
output = command_line_process.communicate()
logger.log(level=level, msg=output[0])
if output[0]:
logger.log(level=level, msg=output[0])
else:
has_output = True
while has_output:
Expand Down

0 comments on commit 2f263cb

Please sign in to comment.