pexec

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 23, 2021 License: MIT Imports: 13 Imported by: 8

Documentation

Overview

Package pexec defines process management utilities to be used as a library within a go process wishing to own sub-processes.

It helps manage the lifecycle of processes by keeping them up as long as possible when configured.

Index

Constants

This section is empty.

Variables

View Source
var NoopProcessManager = &noopProcessManager{}

NoopProcessManager does nothing and is useful for places that need to return some ProcessManager.

Functions

This section is empty.

Types

type ManagedProcess

type ManagedProcess interface {
	// ID returns the unique ID of the process.
	ID() string

	// Start starts the process. The given context is only used for one shot processes.
	Start(ctx context.Context) error

	// Stop signals and waits for the process to stop. An error is returned if
	// there's any system level issue stopping the process.
	Stop() error
}

A ManagedProcess controls the lifecycle of a single system process. Based on its configuration, it will ensure the process is revived if it every unexpectedly perishes.

func MergeAddProcessManagers

func MergeAddProcessManagers(dst, src ProcessManager) ([]ManagedProcess, error)

MergeAddProcessManagers merges in another process manager and takes ownership of its processes. This may replace existing processes and it's the callers responsibility to stop what has been replaced.

func MergeRemoveProcessManagers

func MergeRemoveProcessManagers(dst, src ProcessManager) []ManagedProcess

MergeRemoveProcessManagers merges in another process manager and removes ownership of its own processes. It does not stop the processes.

func NewManagedProcess

func NewManagedProcess(config ProcessConfig, logger golog.Logger) ManagedProcess

NewManagedProcess returns a new, unstarted, from the given configuration.

type ProcessConfig

type ProcessConfig struct {
	ID      string   `json:"id"`
	Name    string   `json:"name"`
	Args    []string `json:"args"`
	CWD     string   `json:"cwd"`
	OneShot bool     `json:"one_shot"`
	Log     bool     `json:"log"`
}

A ProcessConfig describes how to manage a system process.

func (*ProcessConfig) Validate

func (config *ProcessConfig) Validate(path string) error

Validate ensures all parts of the config are valid.

type ProcessManager

type ProcessManager interface {
	// ProcessIDs returns the IDs of all managed processes.
	ProcessIDs() []string

	// ProcessByID fetches the process by the given ID if it exists.
	ProcessByID(id string) (ManagedProcess, bool)

	// RemoveProcessByID removes a managed process by the given ID if it exists.
	// It does not stop it it.
	RemoveProcessByID(id string) (ManagedProcess, bool)

	// Start starts all added processes and errors if any fail to start. The
	// given context is only used for one shot processes.
	Start(ctx context.Context) error

	// AddProcess manages the given process and potentially starts it depending
	// on the state of the ProcessManager and if it's requested. The same context
	// semantics in Start apply here. If the process is replaced by its ID, the
	// replaced process will be returned.
	AddProcess(ctx context.Context, proc ManagedProcess, start bool) (ManagedProcess, error)

	// AddProcess manages a new process from the given configuration and
	// potentially starts it depending on the state of the ProcessManager.
	// The same context semantics in Start apply here. If the process is
	// replaced by its ID, the replaced process will be returned.
	AddProcessFromConfig(ctx context.Context, config ProcessConfig) (ManagedProcess, error)

	// Stop signals and waits for all managed processes to stop and returns
	// any errors from stopping them.
	Stop() error

	// Clone gives a copy of the processes being managed but provides
	// no guarantee of the current state of the processes.
	Clone() ProcessManager
}

A ProcessManager is responsible for controlling the lifecycle of processes added to it.

func NewProcessManager

func NewProcessManager(logger golog.Logger) ProcessManager

NewProcessManager returns a new ProcessManager.

Jump to

Keyboard shortcuts

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