process

package
v0.0.55 Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2026 License: MIT Imports: 5 Imported by: 0

README

sup/process

Go Reference Test License

sup/process is a small adapter between os/exec.Cmd and sup.Actor. The caller creates and configures an ordinary standard-library command; the actor runs one fresh command for each execution attempt.

Usage

package main

import (
	"context"
	"fmt"
	"os"
	"os/exec"

	"github.com/webermarci/sup"
	"github.com/webermarci/sup/process"
)

func main() {
	worker := process.NewActor("worker", func(ctx context.Context) *exec.Cmd {
		cmd := exec.CommandContext(ctx, "./worker", "--serve")
		cmd.Dir = "/srv/worker"
		cmd.Env = append(os.Environ(), "LOG_LEVEL=info")
		cmd.Stdout = os.Stdout
		cmd.Stderr = os.Stderr
		return cmd
	})

	supervisor := sup.NewSupervisor("root",
		sup.WithActors(worker),
		sup.WithPolicy(sup.Transient),
	)

	if err := supervisor.Run(context.Background()); err != nil {
		fmt.Println("supervisor failed:", err)
	}
}

Contract

  • The command function is called once per execution attempt and must return a fresh *os/exec.Cmd; commands cannot be reused.
  • Use os/exec.CommandContext with the supplied context when cancellation should stop the process.
  • All command configuration remains standard Go: stdin, output, environment, working directory, platform attributes, cancellation, and WaitDelay are configured directly on os/exec.Cmd.
  • Context cancellation is a clean actor shutdown and makes Run return nil.
  • Other command errors are returned to the supervisor.
  • A nil command returns process.ErrNilCommand.
  • Concurrent calls to Run return process.ErrActorRunning.

Process restarts, restart delays, restart limits, logging, and application state belong to the supervisor or application rather than this package.

Documentation

Overview

Package process adapts standard-library os/exec commands to sup actors.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrActorRunning is returned when Run is called while the actor is already
	// running a process.
	ErrActorRunning = errors.New("process: actor is already running")

	// ErrNilCommand is returned when a CommandFunc returns nil.
	ErrNilCommand = errors.New("process: command function returned nil")
)

Functions

This section is empty.

Types

type Actor

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

Actor creates and runs an external command for each execution attempt. Process configuration and cancellation behavior belong to CommandFunc; the actor only adapts the command lifecycle to sup.Actor.

func NewActor

func NewActor(id string, command CommandFunc) *Actor

NewActor creates a process actor with the given id and command function.

func (*Actor) ID

func (a *Actor) ID() string

ID returns the actor id.

func (*Actor) Inspect

func (a *Actor) Inspect() sup.Spec

Inspect returns the process actor spec.

func (*Actor) Run

func (a *Actor) Run(ctx context.Context) error

Run creates and runs one command. Context cancellation is a clean actor shutdown and returns nil; other command errors are returned to the supervisor.

type CommandFunc

type CommandFunc func(context.Context) *exec.Cmd

CommandFunc creates a fresh command for one execution attempt. The supplied context should normally be passed to os/exec.CommandContext so cancellation stops the process. A command cannot be reused across calls.

Jump to

Keyboard shortcuts

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