-
Notifications
You must be signed in to change notification settings - Fork 313
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixes #1595: Add method to process.py to return both return code and logs #1828
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f9a8a29
Change run_subprocess to return `CompletedProcess` object
favilo 21caafa
Avoid the double call to `run_subprocess*` in `is_branch()`
favilo a2ee76e
Adding returncode, stdout fields to `run_subprocess_with_logging`
favilo 9b77892
Change `with ... Popen()` to `run()` instead.
favilo 375d8ba
Revert all changes, except typing.
favilo 965fd28
Add `run_subprocess_with_logging_and_output` verbose method.
favilo fa9d59d
Taking care of some final nits.
favilo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,15 +20,22 @@ | |
import shlex | ||
import subprocess | ||
import time | ||
from typing import Callable, Dict, List | ||
|
||
import psutil | ||
|
||
|
||
def run_subprocess(command_line): | ||
def run_subprocess(command_line: str) -> int: | ||
""" | ||
Runs the provided command line in a subprocess. All output will be returned in the `CompletedProcess.stdout` field. | ||
|
||
:param command_line: The command line of the subprocess to launch. | ||
:return: The process' return code | ||
""" | ||
return subprocess.call(command_line, shell=True) | ||
|
||
|
||
def run_subprocess_with_output(command_line, env=None): | ||
def run_subprocess_with_output(command_line: str, env: Dict[str, str] = None) -> List[str]: | ||
logger = logging.getLogger(__name__) | ||
logger.debug("Running subprocess [%s] with output.", command_line) | ||
command_line_args = shlex.split(command_line) | ||
|
@@ -44,7 +51,7 @@ def run_subprocess_with_output(command_line, env=None): | |
return lines | ||
|
||
|
||
def exit_status_as_bool(runnable, quiet=False): | ||
def exit_status_as_bool(runnable: Callable[[], int], quiet: bool = False) -> bool: | ||
""" | ||
|
||
:param runnable: A runnable returning an int as exit status assuming ``0`` is meaning success. | ||
|
@@ -60,7 +67,18 @@ 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, detach=False): | ||
LogLevel = int | ||
FileId = int | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Should type definitions be moved to the top of the file? |
||
|
||
|
||
def run_subprocess_with_logging( | ||
command_line: str, | ||
header: str = None, | ||
level: LogLevel = logging.INFO, | ||
stdin: FileId = None, | ||
env: Dict[str, str] = None, | ||
detach: bool = False, | ||
) -> int: | ||
""" | ||
Runs the provided command line in a subprocess. All output will be captured by a logger. | ||
|
||
|
@@ -98,7 +116,53 @@ def run_subprocess_with_logging(command_line, header=None, level=logging.INFO, s | |
return command_line_process.returncode | ||
|
||
|
||
def is_rally_process(p): | ||
def run_subprocess_with_logging_and_output( | ||
command_line: str, | ||
header: str = None, | ||
level: LogLevel = logging.INFO, | ||
stdin: FileId = None, | ||
env: Dict[str, str] = None, | ||
detach: bool = False, | ||
) -> subprocess.CompletedProcess: | ||
""" | ||
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 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) | ||
|
||
# pylint: disable=subprocess-popen-preexec-fn | ||
completed = subprocess.run( | ||
command_line_args, | ||
stdout=subprocess.PIPE, | ||
stderr=subprocess.STDOUT, | ||
text=True, | ||
env=env, | ||
check=False, | ||
stdin=stdin if stdin else None, | ||
preexec_fn=pre_exec, | ||
) | ||
|
||
for stdout in completed.stdout.splitlines(): | ||
logger.log(level=level, msg=stdout) | ||
|
||
logger.debug("Subprocess [%s] finished with return code [%s].", command_line, str(completed.returncode)) | ||
return completed | ||
|
||
|
||
def is_rally_process(p: psutil.Process) -> bool: | ||
return ( | ||
p.name() == "esrally" | ||
or p.name() == "rally" | ||
|
@@ -110,14 +174,14 @@ def is_rally_process(p): | |
) | ||
|
||
|
||
def find_all_other_rally_processes(): | ||
def find_all_other_rally_processes() -> List[psutil.Process]: | ||
others = [] | ||
for_all_other_processes(is_rally_process, others.append) | ||
return others | ||
|
||
|
||
def kill_all(predicate): | ||
def kill(p): | ||
def kill_all(predicate: Callable[[psutil.Process], bool]) -> None: | ||
def kill(p: psutil.Process): | ||
logging.getLogger(__name__).info("Killing lingering process with PID [%s] and command line [%s].", p.pid, p.cmdline()) | ||
p.kill() | ||
# wait until process has terminated, at most 3 seconds. Otherwise we might run into race conditions with actor system | ||
|
@@ -132,7 +196,7 @@ def kill(p): | |
for_all_other_processes(predicate, kill) | ||
|
||
|
||
def for_all_other_processes(predicate, action): | ||
def for_all_other_processes(predicate: Callable[[psutil.Process], bool], action: Callable[[psutil.Process], None]) -> None: | ||
# no harakiri please | ||
my_pid = os.getpid() | ||
for p in psutil.process_iter(): | ||
|
@@ -143,8 +207,8 @@ def for_all_other_processes(predicate, action): | |
pass | ||
|
||
|
||
def kill_running_rally_instances(): | ||
def rally_process(p): | ||
def kill_running_rally_instances() -> None: | ||
def rally_process(p: psutil.Process) -> bool: | ||
return ( | ||
p.name() == "esrally" | ||
or p.name() == "rally" | ||
|
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.