Capture subprocess results separately from diagnostics
This commit is contained in:
@@ -3,6 +3,7 @@ package subprocess
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
@@ -20,6 +21,8 @@ type RunRequest struct {
|
||||
Timeout time.Duration
|
||||
StdoutLogPath string
|
||||
StderrLogPath string
|
||||
StdoutResult io.Writer
|
||||
StdoutResultLimit int64
|
||||
}
|
||||
|
||||
// RunResult captures subprocess execution details.
|
||||
@@ -51,7 +54,11 @@ func Run(ctx context.Context, req RunRequest) (RunResult, error) {
|
||||
defer cancel()
|
||||
|
||||
childEnv := buildChildEnvironment(os.Environ(), req.EnvOverrides)
|
||||
logs, err := openLogWriters(req.StdoutLogPath, req.StderrLogPath, req.diagnosticOwner(), sensitiveEnvironmentValues(childEnv, req.SensitiveEnvNames))
|
||||
stdoutLogPath := req.StdoutLogPath
|
||||
if req.StdoutResult != nil {
|
||||
stdoutLogPath = ""
|
||||
}
|
||||
logs, err := openLogWriters(stdoutLogPath, req.StderrLogPath, req.diagnosticOwner(), sensitiveEnvironmentValues(childEnv, req.SensitiveEnvNames))
|
||||
if err != nil {
|
||||
return RunResult{}, err
|
||||
}
|
||||
@@ -61,6 +68,11 @@ func Run(ctx context.Context, req RunRequest) (RunResult, error) {
|
||||
cmd.Dir = req.WorkingDir
|
||||
cmd.Env = childEnv
|
||||
cmd.Stdout = logs.Stdout
|
||||
var resultWriter *boundedResultWriter
|
||||
if req.StdoutResult != nil {
|
||||
resultWriter = &boundedResultWriter{target: req.StdoutResult, limit: req.StdoutResultLimit, owner: req.diagnosticOwner(), abort: logs.limits}
|
||||
cmd.Stdout = resultWriter
|
||||
}
|
||||
cmd.Stderr = logs.Stderr
|
||||
outcome := executeOwnedCommand(runCtx, cmd, logs.Limits())
|
||||
cleanupErr := joinErrors(outcome.cleanupErr, logs.Flush())
|
||||
@@ -68,18 +80,28 @@ func Run(ctx context.Context, req RunRequest) (RunResult, error) {
|
||||
if captureLimit == nil {
|
||||
captureLimit = logs.Limit()
|
||||
}
|
||||
result := RunResult{ExitCode: outcome.exitCode, StartedAt: outcome.startedAt, CompletedAt: outcome.completedAt, StdoutLogPath: req.StdoutLogPath, StderrLogPath: req.StderrLogPath}
|
||||
result := RunResult{ExitCode: outcome.exitCode, StartedAt: outcome.startedAt, CompletedAt: outcome.completedAt, StdoutLogPath: stdoutLogPath, StderrLogPath: req.StderrLogPath}
|
||||
result.Duration = result.CompletedAt.Sub(result.StartedAt)
|
||||
result.TimedOut = outcome.reason == executionDeadline
|
||||
result.Canceled = outcome.reason == executionCanceled
|
||||
|
||||
if outcome.reason == executionNaturalExit && outcome.waitErr == nil && cleanupErr == nil {
|
||||
var resultErr error
|
||||
if resultWriter != nil {
|
||||
resultErr = resultWriter.Err()
|
||||
}
|
||||
if outcome.reason == executionNaturalExit && outcome.waitErr == nil && cleanupErr == nil && resultErr == nil && captureLimit == nil {
|
||||
return result, nil
|
||||
}
|
||||
|
||||
stderrTail := logs.stderr.Tail()
|
||||
diagnostics := buildDiagnostics(req, result, stderrTail)
|
||||
|
||||
if resultErr != nil {
|
||||
if cause := joinErrors(outcome.waitErr, cleanupErr); cause != nil {
|
||||
return result, fmt.Errorf("%w (%s): %w", resultErr, diagnostics, cause)
|
||||
}
|
||||
return result, fmt.Errorf("%w (%s)", resultErr, diagnostics)
|
||||
}
|
||||
if captureLimit != nil {
|
||||
if cause := joinErrors(outcome.waitErr, cleanupErr); cause != nil {
|
||||
return result, fmt.Errorf("%w (%s): %w", captureLimit, diagnostics, cause)
|
||||
@@ -115,7 +137,7 @@ func buildDiagnostics(req RunRequest, result RunResult, stderrTail string) strin
|
||||
result.ExitCode,
|
||||
result.TimedOut,
|
||||
result.Canceled,
|
||||
req.StdoutLogPath,
|
||||
result.StdoutLogPath,
|
||||
req.StderrLogPath,
|
||||
)
|
||||
if strings.TrimSpace(stderrTail) == "" {
|
||||
|
||||
Reference in New Issue
Block a user