Documentation
¶
Overview ¶
Package process implements a Runner interface for local processes
Example ¶
package main
import (
"context"
"fmt"
"io"
"os"
"time"
"github.com/rzbill/rune/pkg/log"
"github.com/rzbill/rune/pkg/runner"
"github.com/rzbill/rune/pkg/runner/process"
"github.com/rzbill/rune/pkg/types"
)
func main() {
// Create a context with timeout
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
// Create a logger
logger := log.NewLogger()
// Create a process runner with options
processRunner, err := process.NewProcessRunner(
process.WithBaseDir(os.TempDir()),
process.WithLogger(logger),
)
if err != nil {
fmt.Printf("Failed to create process runner: %v\n", err)
return
}
// Create a unique instance ID
instanceID := "example-process-123"
// Create a process instance that will list files
instance := &types.Instance{
ID: instanceID,
Name: "example-process",
NodeID: "local",
Process: &types.ProcessSpec{
Command: "ls",
Args: []string{"-la"},
},
}
// Create the instance
fmt.Println("Creating process instance...")
if err := processRunner.Create(ctx, instance); err != nil {
fmt.Printf("Failed to create instance: %v\n", err)
return
}
// Start the instance
fmt.Println("Starting process instance...")
if err := processRunner.Start(ctx, instance); err != nil {
fmt.Printf("Failed to start instance: %v\n", err)
return
}
// Give the process time to complete
time.Sleep(1 * time.Second)
// Get the status
status, err := processRunner.Status(ctx, instance)
if err != nil {
fmt.Printf("Failed to get status: %v\n", err)
return
}
fmt.Printf("Instance status: %s\n", status)
// Get logs
fmt.Println("Getting logs...")
logs, err := processRunner.GetLogs(ctx, instance, runner.LogOptions{
Tail: 10,
})
if err != nil {
fmt.Printf("Failed to get logs: %v\n", err)
return
}
defer logs.Close()
// Print logs content to stdout
logBytes, err := io.ReadAll(logs)
if err != nil {
fmt.Printf("Failed to read logs: %v\n", err)
return
}
fmt.Printf("Log output: %s\n", string(logBytes))
// List all instances
instances, err := processRunner.List(ctx, "example")
if err != nil {
fmt.Printf("Failed to list instances: %v\n", err)
return
}
fmt.Printf("Found %d instances\n", len(instances))
// Remove the instance
fmt.Println("Removing process instance...")
if err := processRunner.Remove(ctx, instance, true); err != nil {
fmt.Printf("Failed to remove instance: %v\n", err)
return
}
fmt.Println("Process runner example completed successfully")
}
Index ¶
- func ValidateProcessSpec(spec *types.ProcessSpec) error
- type ProcessExecStream
- func (s *ProcessExecStream) Close() error
- func (s *ProcessExecStream) ExitCode() (int, error)
- func (s *ProcessExecStream) Read(p []byte) (n int, err error)
- func (s *ProcessExecStream) ResizeTerminal(width, height uint32) error
- func (s *ProcessExecStream) Signal(sigName string) error
- func (s *ProcessExecStream) Stderr() io.Reader
- func (s *ProcessExecStream) Write(p []byte) (n int, err error)
- type ProcessOption
- type ProcessRunner
- func (r *ProcessRunner) Create(ctx context.Context, instance *types.Instance) error
- func (r *ProcessRunner) Exec(ctx context.Context, instance *types.Instance, options runner.ExecOptions) (runner.ExecStream, error)
- func (r *ProcessRunner) GetLogs(ctx context.Context, instance *types.Instance, options runner.LogOptions) (io.ReadCloser, error)
- func (r *ProcessRunner) List(ctx context.Context, namespace string) ([]*types.Instance, error)
- func (r *ProcessRunner) Remove(ctx context.Context, instance *types.Instance, force bool) error
- func (r *ProcessRunner) Start(ctx context.Context, instance *types.Instance) error
- func (r *ProcessRunner) Status(ctx context.Context, instance *types.Instance) (types.InstanceStatus, error)
- func (r *ProcessRunner) Stop(ctx context.Context, instance *types.Instance, timeout time.Duration) error
- func (r *ProcessRunner) Type() types.RunnerType
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ValidateProcessSpec ¶
func ValidateProcessSpec(spec *types.ProcessSpec) error
ValidateProcessSpec validates the process specification, including checking if the command exists and is executable on the system
Types ¶
type ProcessExecStream ¶
type ProcessExecStream struct {
// contains filtered or unexported fields
}
ProcessExecStream implements the runner.ExecStream interface for processes.
func NewProcessExecStream ¶
func NewProcessExecStream( ctx context.Context, instanceID string, options runner.ExecOptions, logger log.Logger, ) (*ProcessExecStream, error)
NewProcessExecStream creates a new ProcessExecStream.
func (*ProcessExecStream) Close ¶
func (s *ProcessExecStream) Close() error
Close terminates the exec session and releases resources.
func (*ProcessExecStream) ExitCode ¶
func (s *ProcessExecStream) ExitCode() (int, error)
ExitCode returns the exit code after the process has completed.
func (*ProcessExecStream) Read ¶
func (s *ProcessExecStream) Read(p []byte) (n int, err error)
Read reads data from the standard output of the process.
func (*ProcessExecStream) ResizeTerminal ¶
func (s *ProcessExecStream) ResizeTerminal(width, height uint32) error
ResizeTerminal resizes the terminal (if TTY was enabled). For processes, terminal resizing isn't typically supported through the Go API.
func (*ProcessExecStream) Signal ¶
func (s *ProcessExecStream) Signal(sigName string) error
Signal sends a signal to the process.
func (*ProcessExecStream) Stderr ¶
func (s *ProcessExecStream) Stderr() io.Reader
Stderr provides access to the standard error stream of the process.
type ProcessOption ¶
type ProcessOption func(*ProcessRunner)
ProcessOption is a function that configures a ProcessRunner
func WithBaseDir ¶
func WithBaseDir(dir string) ProcessOption
WithBaseDir sets the base directory for process workspaces and logs
func WithLogger ¶
func WithLogger(logger log.Logger) ProcessOption
WithLogger sets the logger for the runner
type ProcessRunner ¶
type ProcessRunner struct {
// contains filtered or unexported fields
}
ProcessRunner implements the runner.Runner interface for local processes
func NewProcessRunner ¶
func NewProcessRunner(options ...ProcessOption) (*ProcessRunner, error)
NewProcessRunner creates a new ProcessRunner with the given options
func (*ProcessRunner) Exec ¶
func (r *ProcessRunner) Exec(ctx context.Context, instance *types.Instance, options runner.ExecOptions) (runner.ExecStream, error)
Exec creates an interactive exec session within a running process.
func (*ProcessRunner) GetLogs ¶
func (r *ProcessRunner) GetLogs(ctx context.Context, instance *types.Instance, options runner.LogOptions) (io.ReadCloser, error)
GetLogs retrieves logs from a process instance
func (*ProcessRunner) Status ¶
func (r *ProcessRunner) Status(ctx context.Context, instance *types.Instance) (types.InstanceStatus, error)
Status retrieves the current status of a process instance
func (*ProcessRunner) Stop ¶
func (r *ProcessRunner) Stop(ctx context.Context, instance *types.Instance, timeout time.Duration) error
Stop stops a running process instance
func (*ProcessRunner) Type ¶
func (r *ProcessRunner) Type() types.RunnerType