Skip to content

Commit

Permalink
Detach Elasticsearch on startup (#859)
Browse files Browse the repository at this point in the history
With this commit we detach Elasticsearch on startup from its controlling
process via the
[setpgrp](http://man7.org/linux/man-pages/man3/setpgrp.3p.html) system
call. Although we start Elasticsearch with the `-d` flag, this is
necessary to ensure it does not receive signals like `SIGHUP` from its
original parent process (Rally).
  • Loading branch information
danielmitterdorfer authored Jan 13, 2020
1 parent 93c024a commit bebf9a0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 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
11 changes: 9 additions & 2 deletions esrally/utils/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,29 @@ def exit_status_as_bool(runnable, quiet=False):
return False


def run_subprocess_with_logging(command_line, header=None, level=logging.INFO, env=None):
def run_subprocess_with_logging(command_line, header=None, level=logging.INFO, env=None, detach=False):
"""
Runs the provided command line in a subprocess. All output will be captured by a logger.
:param command_line: The command line of the subprocess to launch.
:param header: An optional header line that should be logged (this will be logged on info level, regardless of the defined log level).
:param level: The log level to use for output (default: logging.INFO).
: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) as command_line_process:
# pylint: disable=subprocess-popen-preexec-fn
with subprocess.Popen(command_line_args,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
env=env,
preexec_fn=pre_exec) as command_line_process:
has_output = True
while has_output:
line = command_line_process.stdout.readline()
Expand Down

0 comments on commit bebf9a0

Please sign in to comment.