Skip to content
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

forward subprocess STDOUT / STDERR to main process #123

Merged
merged 1 commit into from
Oct 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 10 additions & 13 deletions ragna/_cli/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,12 @@ def api(
if start_worker is None:
start_worker = config.core.queue_url != "memory"

components = urlsplit(config.api.url)
if components.hostname is None or components.port is None:
# TODO: make this part of the config validation
rich.print(f"Unable to extract hostname and port from {config.api.url}.")
raise typer.Exit(1)

if start_worker:
process = subprocess.Popen(
[
Expand All @@ -152,19 +158,13 @@ def api(
"--config",
config.__ragna_cli_value__, # type: ignore[attr-defined]
],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
stdout=sys.stdout,
stderr=sys.stderr,
)
else:
process = None

try:
components = urlsplit(config.api.url)
if components.hostname is None or components.port is None:
# TODO: make this part of the config validation
rich.print(f"Unable to extract hostname and port from {config.api.url}.")
raise typer.Exit(1)

uvicorn.run(
api_app(config), host=components.hostname, port=components.port or 31476
)
Expand Down Expand Up @@ -206,8 +206,8 @@ def check_api_available() -> bool:
"--config",
config.__ragna_cli_value__, # type: ignore[attr-defined]
],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
stdout=sys.stdout,
stderr=sys.stderr,
)
else:
process = None
Expand All @@ -217,12 +217,9 @@ def check_api_available() -> bool:

@timeout_after(30)
def wait_for_api() -> None:
rich.print(f"Starting ragna api at {config.api.url}")
while not check_api_available():
time.sleep(0.5)

rich.print("Started ragna api")

wait_for_api()

ui_app(config).serve() # type: ignore[no-untyped-call]
Expand Down
20 changes: 5 additions & 15 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,13 @@


@contextlib.contextmanager
def background_subprocess(
*args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, **kwargs
):
def background_subprocess(*args, stdout=sys.stdout, stderr=sys.stdout, **kwargs):
process = subprocess.Popen(*args, stdout=stdout, stderr=stderr, **kwargs)
try:
yield process
finally:
process.kill()
stdout, stderr = process.communicate()
if stdout:
sys.stdout.buffer.write(stdout)
sys.stdout.flush()
if stderr:
sys.stderr.buffer.write(stderr)
sys.stderr.flush()
process.communicate()


def timeout_after(seconds=30, *, message=None):
Expand Down Expand Up @@ -130,17 +122,15 @@ def ragna_worker(config):

with background_subprocess(
[sys.executable, "-m", "ragna", "worker", "--config", str(config_path)]
) as process:
):

@timeout_after(message="Unable to start worker")
def wait_for_worker():
# This seems quite brittle, but I didn't find a better way to check
# whether the worker is ready. We are checking the logged messages until
# we see the "ready" message.
for line in process.stderr:
sys.stderr.buffer.write(line)
if b"Huey consumer started" in line:
sys.stderr.flush()
for line in sys.stderr:
if "Huey consumer started" in line:
return

yield wait_for_worker()
Expand Down
Loading