process

package
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 23, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package process provides subprocess execution with context cancellation, signal handling, and structured output capture.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Command

type Command struct {
	// Binary is the executable path or name (resolved via PATH).
	Binary string
	// Args are the command-line arguments.
	Args []string
	// Dir is the working directory. If empty, uses the current directory.
	Dir string
	// Env is additional environment variables (key=value). Merged with os.Environ.
	Env []string
	// Stdin provides input to the process. May be nil.
	Stdin io.Reader
	// GracePeriod is how long to wait after SIGTERM before SIGKILL.
	// Defaults to 5 seconds if zero.
	GracePeriod time.Duration
}

Command configures a subprocess to execute.

type Result

type Result struct {
	// Stdout is the captured standard output.
	Stdout []byte
	// Stderr is the captured standard error.
	Stderr []byte
	// ExitCode is the process exit code. -1 if the process was killed.
	ExitCode int
	// Duration is how long the process ran.
	Duration time.Duration
}

Result holds the output and status of a completed subprocess.

func Run

func Run(ctx context.Context, cmd Command) (*Result, error)

Run executes a subprocess and waits for it to complete. If the context is canceled, SIGTERM is sent first, then SIGKILL after GracePeriod.

func RunWithResilience

func RunWithResilience(ctx context.Context, cmd Command, runner *Runner) (*Result, error)

RunWithResilience is a convenience for one-shot subprocess execution with resilience. For repeated calls where circuit breaker state should persist, use NewRunner instead.

type Runner

type Runner struct {
	// contains filtered or unexported fields
}

Runner wraps subprocess execution with persistent resilience state. Use NewRunner to create one, then call Run repeatedly. The circuit breaker state persists across calls — repeated crashes trip the breaker.

func NewRunner

func NewRunner(cfg provider.ResilienceConfig) *Runner

NewRunner creates a Runner with the given resilience config. Nil config fields are skipped. Empty config means Run() calls process.Run directly.

func (*Runner) Run

func (r *Runner) Run(ctx context.Context, cmd Command) (*Result, error)

Run executes a subprocess through the resilience chain.

type SubprocessProvider

type SubprocessProvider[I, O any] struct {
	// contains filtered or unexported fields
}

SubprocessProvider wraps a Command as a provider.RequestResponse. The input function builds a Command from the input, and the output function parses the Result into the desired output type.

func NewSubprocessProvider

func NewSubprocessProvider[I, O any](
	name string,
	buildCmd func(I) Command,
	parseOut func(*Result) (O, error),
) *SubprocessProvider[I, O]

NewSubprocessProvider creates a RequestResponse provider backed by subprocess execution.

func (*SubprocessProvider[I, O]) Execute

func (p *SubprocessProvider[I, O]) Execute(ctx context.Context, input I) (O, error)

func (*SubprocessProvider[I, O]) IsAvailable

func (p *SubprocessProvider[I, O]) IsAvailable(ctx context.Context) bool

func (*SubprocessProvider[I, O]) Name

func (p *SubprocessProvider[I, O]) Name() string

func (*SubprocessProvider[I, O]) WithAvailabilityCheck

func (p *SubprocessProvider[I, O]) WithAvailabilityCheck(fn func(context.Context) bool) *SubprocessProvider[I, O]

WithAvailabilityCheck sets a custom availability check for the provider.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL