process

package
v0.0.1-dev.137 Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2026 License: MIT Imports: 23 Imported by: 0

Documentation

Overview

Package process — RUNE-121 init-step execution for the process runtime.

Package process implements a Runner interface for local processes

Example
package main

import (
	"context"
	"fmt"
	"io"
	"os"
	"time"

	"github.com/runestack/rune/pkg/log"
	"github.com/runestack/rune/pkg/runner"
	"github.com/runestack/rune/pkg/runner/process"
	"github.com/runestack/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) Dial

func (r *ProcessRunner) Dial(ctx context.Context, instance *types.Instance, port uint32) (net.Conn, error)

Dial opens a TCP connection to the given port on the local machine. Process-runner instances share the host network namespace so the target address is 127.0.0.1:port. Used by `rune port-forward` (RUNE-122).

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

func (r *ProcessRunner) RunDebug(ctx context.Context, instance *types.Instance, options runner.ExecOptions) (runner.ExecStream, error)

RunDebug is unsupported by the process runner — there's no image to clone and no entrypoint to override, so spawning an inspection sidecar has no meaningful equivalent. The exec service surfaces this as FailedPrecondition.

func (*ProcessRunner) RunInit

func (r *ProcessRunner) RunInit(ctx context.Context, instance *types.Instance, step types.InitStep) (int, error)

RunInit implements runner.Runner.RunInit for the process runtime.

Process init steps run as a synchronous child of the runner. Unlike the docker runner, there is no image to pull and no bind-mount setup: the parent service's volumes are already part of the host filesystem (the process runtime treats type=hostPath as the only supported volume kind), and secret/configmap mounts have already been materialised onto disk by Create(). The step.Volumes / step.SecretMounts / step.ConfigmapMounts filters are therefore advisory in this runtime — the controller honours them at evaluation time but RunInit does not re-validate them.

On normal termination the container's exit code is returned (zero on success). On runtime errors (fork/exec failure, security context failure, etc.) the error is non-nil and the exit code is undefined.

Cancellation of ctx kills the step process group.

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