process

package
v0.0.1-dev.12 Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2025 License: MIT Imports: 20 Imported by: 0

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

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.

func (*ProcessExecStream) Write

func (s *ProcessExecStream) Write(p []byte) (n int, err error)

Write sends data to the standard input 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) Create

func (r *ProcessRunner) Create(ctx context.Context, instance *types.Instance) error

Create creates a new process but does not start it

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) List

func (r *ProcessRunner) List(ctx context.Context, namespace string) ([]*types.Instance, error)

List lists all process instances

func (*ProcessRunner) Remove

func (r *ProcessRunner) Remove(ctx context.Context, instance *types.Instance, force bool) error

Remove removes a process instance

func (*ProcessRunner) Start

func (r *ProcessRunner) Start(ctx context.Context, instance *types.Instance) error

Start starts an existing 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

Directories

Path Synopsis
Package security provides security implementations for process runners
Package security provides security implementations for process runners

Jump to

Keyboard shortcuts

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