panicmon

package
v0.8.2 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2019 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package panicmon provides a method of spawning processes and monitoring its behavior via generic pluggable handlers.

On linux we want to signal to the child process that when its parent dies it should die as well via receiving SIGKILL. This is because panicmon is set up to forward every signal, so if it dies we assume that means it was because it was sent SIGTERM, which it propagated to the child, which the child then did not process and shut down in time to prevent the supervisor (supervisord / systemd) from sending the parent a SIGKILL, which it cannot ignore and thus not pass to child, meaning that the child as well should exit as if it was passed SIGKILL.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Executor

type Executor interface {
	// Run executes a command as defined by args.
	Run(args []string) (StatusCode, error)
}

An Executor is responsible for executing a command and forwarding signals received by the parent to the spawned process. It reports process/signal events via the ProcessHandler/SignalHandler interfaces it is created with.

func NewExecutor

func NewExecutor(opts ExecutorOptions) Executor

NewExecutor returns an executor configured according to opts (after calling opts.Validate()).

type ExecutorOptions

type ExecutorOptions struct {
	// Handler for signal and process events.
	Handler Handler

	// If ListenSigs is non-empty than only the signals specified will be
	// listened to / forwarded. Default (empty) will cause all signals to be
	// listened to, and all signals except SIGCHLD will be forwarded to the
	// child.
	Signals []os.Signal

	// By default panicmon directs the child's stdout/stderr to its own in order
	// to seamlessly wrap the process. This behavior can be overridden by
	// specifying an io.Writer to send the child's stdout/stderr to. If either
	// is nil the parent's will be used. Output can be silenced by setting either
	// to ioutil.Discard.
	Stdout io.Writer
	Stderr io.Writer

	// By default panicmon uses the existing processes environment variables
	// when launching child processes. Set Env if you'd like to specify you're
	// own environment variables to spawn with, on how to use this option see
	// os/exec.Cmd.
	Env []string
}

ExecutorOptions specifies options for creating a new executor.

func NewDefaultExecutorOpts

func NewDefaultExecutorOpts() ExecutorOptions

NewDefaultExecutorOpts returns an ExecutorOpts that will listen to / forward all signals (except SIGCHLD) but will use noop handlers for signal/process events.

func (*ExecutorOptions) Validate

func (opts *ExecutorOptions) Validate()

Validate ensures that an ExecutorOpts type is valid. Specifically, it will set ProcessHandler and SignalHandler to noop handlers if either of them are nil.

type Handler

type Handler struct {
	ProcessHandler
	SignalHandler
}

Handler is composed of a ProcessHandler and SignalHandler.

type NoopProcessHandler

type NoopProcessHandler struct{}

NoopProcessHandler implements ProcessHandler and does nothing.

func (NoopProcessHandler) ProcessExited

func (NoopProcessHandler) ProcessExited(_ ProcessExitedEvent)

ProcessExited is a noop.

func (NoopProcessHandler) ProcessFailed

func (NoopProcessHandler) ProcessFailed(_ ProcessFailedEvent)

ProcessFailed is a noop.

func (NoopProcessHandler) ProcessStarted

func (NoopProcessHandler) ProcessStarted(_ ProcessStartEvent)

ProcessStarted is a noop.

type NoopSignalHandler

type NoopSignalHandler struct{}

NoopSignalHandler implements SignalHandler and does nothing.

func (NoopSignalHandler) SignalFailed

func (NoopSignalHandler) SignalFailed(_ SignalFailedEvent)

SignalFailed is a noop.

func (NoopSignalHandler) SignalPassed

func (NoopSignalHandler) SignalPassed(_ SignalPassedEvent)

SignalPassed is a noop.

func (NoopSignalHandler) SignalReceived

func (NoopSignalHandler) SignalReceived(_ SignalReceivedEvent)

SignalReceived is a noop.

type ProcessExitedEvent

type ProcessExitedEvent struct {
	Args []string
	Code StatusCode
	Err  error
}

ProcessExitedEvent is passed to a ProcessExited handler.

type ProcessFailedEvent

type ProcessFailedEvent struct {
	Args []string
	Err  error
}

ProcessFailedEvent is passed to a ProcessFailed handler.

type ProcessHandler

type ProcessHandler interface {
	// ProcessStarted is called immediately prior to a process being started.
	ProcessStarted(ProcessStartEvent)

	// ProcessFailed is called when a process cannot successfully be started.
	// It receives the error returned by os/exec.
	ProcessFailed(ProcessFailedEvent)

	// ProcessExited is called when a process has been successfully started and
	// has ran to completion (regardless of exit status). It receives the status
	// code returned by the process.
	ProcessExited(ProcessExitedEvent)
}

ProcessHandler is implemented by clients that would like to subscribe to process-related events (start, exit, etc.).

type ProcessStartEvent

type ProcessStartEvent struct {
	Args []string
}

ProcessStartEvent is passed to a ProcessStarted handler.

type SignalFailedEvent

type SignalFailedEvent struct {
	Signal   os.Signal
	ChildPid int
	Err      error
}

SignalFailedEvent is passed to a SignalFailed handler.

type SignalHandler

type SignalHandler interface {
	// SignalReceived is called immediately after a signal is intercepted by
	// panicmon.
	SignalReceived(SignalReceivedEvent)

	// SignalPassed is called when a signal has been successfully forwarded to a
	// child process.
	SignalPassed(SignalPassedEvent)

	// SignalFailed is called when panicmon encounters an error passing a signal
	// to a child.
	SignalFailed(SignalFailedEvent)
}

SignalHandler is implemented by clients that would like to subscribe to signal-related events, specifically when signals are received by panicmon and whether or not they are successfully passed to the child.

type SignalPassedEvent

type SignalPassedEvent struct {
	Signal   os.Signal
	ChildPid int
}

SignalPassedEvent is passed to a SignalPassed handler.

type SignalReceivedEvent

type SignalReceivedEvent struct {
	Signal   os.Signal
	ChildPid int
}

SignalReceivedEvent is passed to a SignalReceived handler.

type StatusCode

type StatusCode int

StatusCode represents the exit code of a process.

func (StatusCode) String

func (r StatusCode) String() string

String returns the string representation of the status code.

Jump to

Keyboard shortcuts

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