process

package
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 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 Adapter added in v0.1.4

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

Adapter wraps subprocess execution as a provider.RequestResponse.

func NewAdapter added in v0.1.4

func NewAdapter(cfg Config) *Adapter

NewAdapter creates a new process adapter.

func (*Adapter) Execute added in v0.1.4

func (a *Adapter) Execute(ctx context.Context, cmd Command) (*Result, error)

Execute runs a command (implements provider.RequestResponse[Command, *Result]).

func (*Adapter) IsAvailable added in v0.1.4

func (a *Adapter) IsAvailable(_ context.Context) bool

IsAvailable always returns true for process adapters (implements provider.Provider).

func (*Adapter) Name added in v0.1.4

func (a *Adapter) Name() string

Name returns the adapter name (implements provider.Provider).

func (*Adapter) Run added in v0.1.4

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

Run executes a command, applying adapter-level defaults.

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 Config added in v0.1.4

type Config struct {
	// Name identifies this adapter instance (used by provider.Provider interface).
	Name string `yaml:"name,omitempty" mapstructure:"name"`
	// GracePeriod is the default grace period for SIGTERM→SIGKILL.
	GracePeriod time.Duration `yaml:"grace_period,omitempty" mapstructure:"grace_period"`
	// Timeout is the default execution timeout. Zero means no timeout.
	Timeout time.Duration `yaml:"timeout,omitempty" mapstructure:"timeout"`
}

Config configures a process adapter.

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