Documentation
¶
Overview ¶
Package process provides process management with Core IPC integration.
The process package enables spawning, monitoring, and controlling external processes with output streaming via the Core ACTION system.
Getting Started ¶
// Register with Core
core, _ := framework.New(
framework.WithName("process", process.NewService(process.Options{})),
)
// Get service and run a process
svc := framework.MustServiceFor[*process.Service](core, "process")
proc, _ := svc.Start(ctx, "go", "test", "./...")
Listening for Events ¶
Process events are broadcast via Core.ACTION:
core.RegisterAction(func(c *framework.Core, msg framework.Message) error {
switch m := msg.(type) {
case process.ActionProcessOutput:
fmt.Print(m.Line)
case process.ActionProcessExited:
fmt.Printf("Exit code: %d\n", m.ExitCode)
}
return nil
})
Index ¶
- Constants
- Variables
- func Init(c *framework.Core) error
- func Kill(id string) error
- func NewService(opts Options) func(*framework.Core) (any, error)
- func Run(ctx context.Context, command string, args ...string) (string, error)
- func RunWithOptions(ctx context.Context, opts RunOptions) (string, error)
- func SetDefault(s *Service)
- type ActionProcessExited
- type ActionProcessKilled
- type ActionProcessOutput
- type ActionProcessStarted
- type Info
- type Options
- type Process
- func (p *Process) CloseStdin() error
- func (p *Process) Done() <-chan struct{}
- func (p *Process) Info() Info
- func (p *Process) IsRunning() bool
- func (p *Process) Kill() error
- func (p *Process) Output() string
- func (p *Process) OutputBytes() []byte
- func (p *Process) SendInput(input string) error
- func (p *Process) Signal(sig interface{ ... }) error
- func (p *Process) Wait() error
- type RingBuffer
- type RunAllResult
- type RunOptions
- type RunResult
- type RunSpec
- type Runner
- type Service
- func (s *Service) Clear()
- func (s *Service) Get(id string) (*Process, error)
- func (s *Service) Kill(id string) error
- func (s *Service) List() []*Process
- func (s *Service) OnShutdown(ctx context.Context) error
- func (s *Service) OnStartup(ctx context.Context) error
- func (s *Service) Output(id string) (string, error)
- func (s *Service) Remove(id string) error
- func (s *Service) Run(ctx context.Context, command string, args ...string) (string, error)
- func (s *Service) RunWithOptions(ctx context.Context, opts RunOptions) (string, error)
- func (s *Service) Running() []*Process
- func (s *Service) Start(ctx context.Context, command string, args ...string) (*Process, error)
- func (s *Service) StartWithOptions(ctx context.Context, opts RunOptions) (*Process, error)
- type ServiceError
- type Status
- type Stream
Constants ¶
const DefaultBufferSize = 1024 * 1024
Default buffer size for process output (1MB).
Variables ¶
var ( ErrProcessNotFound = errors.New("process not found") ErrProcessNotRunning = errors.New("process is not running") ErrStdinNotAvailable = errors.New("stdin not available") )
Errors
var ErrServiceNotInitialized = &ServiceError{msg: "process: service not initialized"}
ErrServiceNotInitialized is returned when the service is not initialized.
Functions ¶
func Init ¶
Init initializes the default global service with a Core instance. This is typically called during application startup.
func NewService ¶
NewService creates a process service factory for Core registration.
core, _ := framework.New(
framework.WithName("process", process.NewService(process.Options{})),
)
func RunWithOptions ¶
func RunWithOptions(ctx context.Context, opts RunOptions) (string, error)
RunWithOptions executes a command with options and waits using the default service.
func SetDefault ¶
func SetDefault(s *Service)
SetDefault sets the global process service. Thread-safe: can be called concurrently with Default().
Types ¶
type ActionProcessExited ¶
type ActionProcessExited struct {
ID string
ExitCode int
Duration time.Duration
Error error // Non-nil if failed to start or was killed
}
ActionProcessExited is broadcast when a process completes. Check ExitCode for success (0) or failure.
type ActionProcessKilled ¶
ActionProcessKilled is broadcast when a process is terminated.
type ActionProcessOutput ¶
ActionProcessOutput is broadcast for each line of output. Subscribe to this for real-time streaming.
type ActionProcessStarted ¶
ActionProcessStarted is broadcast when a process begins execution.
type Info ¶
type Info struct {
ID string `json:"id"`
Command string `json:"command"`
Args []string `json:"args"`
Dir string `json:"dir"`
StartedAt time.Time `json:"startedAt"`
Status Status `json:"status"`
ExitCode int `json:"exitCode"`
Duration time.Duration `json:"duration"`
PID int `json:"pid"`
}
Info provides a snapshot of process state without internal fields.
type Options ¶
type Options struct {
// BufferSize is the ring buffer size for output capture.
// Default: 1MB (1024 * 1024 bytes).
BufferSize int
}
Options configures the process service.
type Process ¶
type Process struct {
ID string
Command string
Args []string
Dir string
Env []string
StartedAt time.Time
Status Status
ExitCode int
Duration time.Duration
// contains filtered or unexported fields
}
Process represents a managed external process.
func Running ¶
func Running() []*Process
Running returns all currently running processes from the default service.
func StartWithOptions ¶
func StartWithOptions(ctx context.Context, opts RunOptions) (*Process, error)
StartWithOptions spawns a process with full configuration using the default service.
func (*Process) CloseStdin ¶
CloseStdin closes the process stdin pipe.
func (*Process) Done ¶
func (p *Process) Done() <-chan struct{}
Done returns a channel that closes when the process exits.
func (*Process) OutputBytes ¶
OutputBytes returns the captured output as bytes.
type RingBuffer ¶
type RingBuffer struct {
// contains filtered or unexported fields
}
RingBuffer is a fixed-size circular buffer that overwrites old data. Thread-safe for concurrent reads and writes.
func NewRingBuffer ¶
func NewRingBuffer(size int) *RingBuffer
NewRingBuffer creates a ring buffer with the given capacity.
func (*RingBuffer) Bytes ¶
func (rb *RingBuffer) Bytes() []byte
Bytes returns a copy of the buffer contents.
func (*RingBuffer) Len ¶
func (rb *RingBuffer) Len() int
Len returns the current length of data in the buffer.
func (*RingBuffer) String ¶
func (rb *RingBuffer) String() string
String returns the buffer contents as a string.
type RunAllResult ¶
type RunAllResult struct {
Results []RunResult
Duration time.Duration
Passed int
Failed int
Skipped int
}
RunAllResult is the aggregate result of running multiple specs.
func (RunAllResult) Success ¶
func (r RunAllResult) Success() bool
Success returns true if all non-skipped specs passed.
type RunOptions ¶
type RunOptions struct {
// Command is the executable to run.
Command string
// Args are the command arguments.
Args []string
// Dir is the working directory (empty = current).
Dir string
// Env are additional environment variables (KEY=VALUE format).
Env []string
// DisableCapture disables output buffering.
// By default, output is captured to a ring buffer.
DisableCapture bool
}
RunOptions configures process execution.
type RunResult ¶
type RunResult struct {
Name string
Spec RunSpec
ExitCode int
Duration time.Duration
Output string
Error error
Skipped bool
}
RunResult captures the outcome of a single process.
type RunSpec ¶
type RunSpec struct {
// Name is a friendly identifier (e.g., "lint", "test").
Name string
// Command is the executable to run.
Command string
// Args are the command arguments.
Args []string
// Dir is the working directory.
Dir string
// Env are additional environment variables.
Env []string
// After lists spec names that must complete successfully first.
After []string
// AllowFailure if true, continues pipeline even if this spec fails.
AllowFailure bool
}
RunSpec defines a process to run with optional dependencies.
type Runner ¶
type Runner struct {
// contains filtered or unexported fields
}
Runner orchestrates multiple processes with dependencies.
func (*Runner) RunAll ¶
RunAll executes specs respecting dependencies, parallelising where possible.
func (*Runner) RunParallel ¶
RunParallel executes all specs concurrently, regardless of dependencies.
func (*Runner) RunSequential ¶
RunSequential executes specs one after another, stopping on first failure.
type Service ¶
type Service struct {
*framework.ServiceRuntime[Options]
// contains filtered or unexported fields
}
Service manages process execution with Core IPC integration.
func Default ¶
func Default() *Service
Default returns the global process service. Returns nil if not initialized.
func (*Service) OnShutdown ¶
OnShutdown implements framework.Stoppable. Kills all running processes on shutdown.
func (*Service) Run ¶
Run executes a command and waits for completion. Returns the combined output and any error.
func (*Service) RunWithOptions ¶
RunWithOptions executes a command with options and waits for completion.
func (*Service) StartWithOptions ¶
StartWithOptions spawns a process with full configuration.
type ServiceError ¶
type ServiceError struct {
// contains filtered or unexported fields
}
ServiceError represents a service-level error.
func (*ServiceError) Error ¶
func (e *ServiceError) Error() string
Error returns the service error message.
type Status ¶
type Status string
Status represents the process lifecycle state.
const ( // StatusPending indicates the process is queued but not yet started. StatusPending Status = "pending" // StatusRunning indicates the process is actively executing. StatusRunning Status = "running" // StatusExited indicates the process completed (check ExitCode). StatusExited Status = "exited" // StatusFailed indicates the process could not be started. StatusFailed Status = "failed" // StatusKilled indicates the process was terminated by signal. StatusKilled Status = "killed" )