Skip to content
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
41 changes: 27 additions & 14 deletions src/cardonnay/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import pathlib as pl
import subprocess
import sys
import threading
import time
import typing as tp

Expand Down Expand Up @@ -65,13 +66,22 @@ def print_json(data: dict | list | pydantic.BaseModel) -> None:
print_json_str(data=json_str)


def _stream(pipe: tp.TextIO, target: tp.TextIO) -> None:
try:
for line in pipe:
target.write(line)
target.flush()
finally:
pipe.close()


def run_command(
command: str | list,
workdir: ttypes.FileType = "",
ignore_fail: bool = False,
shell: bool = False,
) -> int:
"""Run command and stream output live."""
"""Run command and stream output to stdout/stderr."""
if isinstance(command, str):
cmd = command if shell else command.split()
cmd_str = command
Expand All @@ -86,22 +96,25 @@ def run_command(
cwd=workdir or None,
shell=shell,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
stderr=subprocess.PIPE,
text=True,
bufsize=1, # Line-buffered
bufsize=1,
)

if p.stdout is not None:
for line in p.stdout:
sys.stdout.write(line)
sys.stdout.flush()
p.stdout.close() # Properly close the stream
else:
p.wait() # Still wait if no stdout
if not ignore_fail and p.returncode != 0:
err = f"An error occurred while running `{cmd_str}` (no output captured)"
raise RuntimeError(err)
return p.returncode
threads = []

if p.stdout:
t_out = threading.Thread(target=_stream, args=(p.stdout, sys.stdout), daemon=True)
threads.append(t_out)
t_out.start()

if p.stderr:
t_err = threading.Thread(target=_stream, args=(p.stderr, sys.stderr), daemon=True)
threads.append(t_err)
t_err.start()

for t_ in threads:
t_.join()

p.wait()

Expand Down
10 changes: 5 additions & 5 deletions src/cardonnay_scripts/scripts/common/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ get_slot_length() {
}

cardano_cli_log() {
local out retval _
local stderr retval _

if [ -z "${START_CLUSTER_LOG:-}" ]; then
START_CLUSTER_LOG="${STATE_CLUSTER}/start-cluster.log"
Expand All @@ -25,15 +25,15 @@ cardano_cli_log() {

for _ in {1..3}; do
retval=0
{ out="$(cardano-cli "$@" 2>&1)"; } || retval="$?"
# Capture stderr while also outputting both stdout and stderr to their original destinations
{ stderr="$(cardano-cli "$@" 2> >(tee /dev/stderr) 1>&3)"; } 3>&1 || retval="$?"

case "$out" in
case "$stderr" in
*"resource vanished"*)
printf "Retrying \`cardano-cli %s\`. Failure:\n%s\n" "$*" "$out" >&2
printf "Retrying \`cardano-cli %s\`. Failure:\n%s\n" "$*" "$stderr" >&2
sleep 1
;;
*)
if [ -n "$out" ]; then echo "$out"; fi
break
;;
esac
Expand Down