pmuxlib

package
v0.0.0-...-f55aad3 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2025 License: AGPL-3.0 Imports: 12 Imported by: 0

Documentation

Overview

Package pmuxlib implements the process management aspects of the pmux process.

Index

Constants

View Source
const (
	LogSepStdout = '›'
	LogSepStderr = '»'
	LogSepSys    = '~'
)

Characters used to denote different kinds of logs in the default Pmux configuration.

View Source
const CanSendSIGHUP = true

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	TimeFormat string `yaml:"timeFormat"`

	// Set of processes to run, keyed by their name.
	Processes map[string]ProcessConfig `yaml:"processes"`
}

type Logger

type Logger interface {
	Println(string)
	Printf(string, ...interface{})
}

Logger is used by RunProcess to log process details in realtime. You can use a new(NullLogger) if you don't care.

type NullLogger

type NullLogger struct{}

NullLogger is an implementation of Logger which doesn't do anything.

func (NullLogger) Printf

func (NullLogger) Printf(string, ...interface{})

func (NullLogger) Println

func (NullLogger) Println(string)

type PlainLogger

type PlainLogger struct {
	io.WriteCloser
}

PlainLogger implements Logger by writing each line directly to the given io.Writer as-is.

func (PlainLogger) Printf

func (l PlainLogger) Printf(str string, args ...interface{})

func (PlainLogger) Println

func (l PlainLogger) Println(line string)

type Pmux

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

Pmux manages multiple child Processes. Methods on a Pmux instance are _not_ thread-safe.

Stop must be called on a Pmux before the program has exited, or there may be a leftover zombie child process.

func NewPmux

func NewPmux(cfg Config, stdout, stderr io.Writer) *Pmux

NewPmux starts a Pmux with the given configuration.

func (*Pmux) Restart

func (p *Pmux) Restart(name string)

Restart will block until the child process of the given name has been killed and a new one has been spawned. If there is no child of the given name then Restart panics.

func (*Pmux) Stop

func (p *Pmux) Stop()

Stop will block until all child processes have been killed. The Pmux should not be used again after this.

type PmuxLogger

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

PmuxLogger is used by the pmux process itself for logging. It can prefix log lines with a timestamp, the name of the process being logged, and a custom separator in front of the log line to help delineate one kind of log from another.

func NewPmuxLogger

func NewPmuxLogger(out io.Writer, sep rune, timeFmt string) *PmuxLogger

NewPmuxLogger returns a PmuxLogger which will write lines to the given io.Writer, separating metadata from the line itself using the given separator.

If timeFmt is not empty string then the timestamp of each line will be output using that format.

func (*PmuxLogger) Close

func (l *PmuxLogger) Close() error

Close flushes all buffered log data, and sets the logger to discard all future print calls.

PmuxLoggers created via With* methods should be each closed individually.

func (*PmuxLogger) Printf

func (l *PmuxLogger) Printf(msg string, args ...interface{})

func (*PmuxLogger) Println

func (l *PmuxLogger) Println(line string)

func (*PmuxLogger) WithProcessName

func (l *PmuxLogger) WithProcessName(pname string) *PmuxLogger

WithProcessName returns a copy of the PmuxLogger which will prefix log lines with the given process name.

All PmuxLoggers which are spawned using a With* method, including the original, will left pad their process name to the length of the longest process name in the group.

func (*PmuxLogger) WithSep

func (l *PmuxLogger) WithSep(sep rune) *PmuxLogger

WithSep returns a copy of the PmuxLogger which uses the given separator. The original PmuxLogger will continue to function normally.

type Process

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

Process is used to manage a running process. Methods on a Process are _not_ thread-safe.

Stop must be called on a Process before the program has exited, or there may be a leftover zombie child process.

The process will be restarted if it exits of its own accord. There will be a brief wait time between each restart, with an exponential backoff mechanism so that the wait time increases upon repeated restarts.

The stdout and stderr of the process will be written to the corresponding Loggers. Various runtime events will be written to the sysLogger.

func NewProcess

func NewProcess(cfg ProcessConfig) *Process

NewProcess returns a new Process instance based on the given config.

func (*Process) Restart

func (p *Process) Restart()

Restart will block until the currently running child process has been killed and a new one has been spawned.

func (*Process) SendSIGHUP

func (p *Process) SendSIGHUP(sig syscall.Signal) error

SendSIGHUP sends the HUP process signal to the running process. An error is returned if:

- CanSendSIGHUP is false - there is no process running because it hasn't been started yet - there is no process running because it exited and hasn't been restarted yet

func (*Process) Stop

func (p *Process) Stop()

Stop will block until the child process has been killed. The Process should not be used again after this.

type ProcessConfig

type ProcessConfig struct {
	// Cmd and Args describe the actual process to run.
	Cmd  string   `yaml:"cmd"`
	Args []string `yaml:"args"`

	// Env describes the environment variables to set on the process.
	Env map[string]string `yaml:"env"`

	// Dir is the directory the process will be run in. If not set then the
	// process is run in the same directory as this parent process.
	Dir string `yaml:"dir"`

	// MinWait and MaxWait are the minimum and maximum amount of time between
	// restarts that Process will wait.
	//
	// MinWait defaults to 1 second.
	// MaxWait defaults to 64 seconds.
	MinWait time.Duration `yaml:"minWait"`
	MaxWait time.Duration `yaml:"maxWait"`

	// SigKillWait is the amount of time after the process is sent a SIGINT
	// before a SIGKILL is sent.
	//
	// Defalts to 10 seconds.
	SigKillWait time.Duration `yaml:"sigKillWait"`

	// NoRestartOn indicates which exit codes should result in the process not
	// being restarted any further.
	NoRestartOn []int `yaml:"noRestartOn"`

	// StartAfterFunc will cause the starting of the process to be blocked until
	// this function returns.
	StartAfterFunc func(context.Context) error

	// Group can be used to control the order and grouping of processes as they
	// shut down.
	//
	// Processes will not be shut down until all processes with a higher group
	// number are already shut down. Processes with the same group number will
	// be shut down simultaneously.
	Group int `yaml:"group"`

	// Where to send stdout, stderr log lines, and lines from pmux itself.
	StdoutLogger Logger `yaml:"-"`
	StderrLogger Logger `yaml:"-"`
	SysLogger    Logger `yaml:"-"`
}

ProcessConfig is used to configure a process.

Jump to

Keyboard shortcuts

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