processmanager

package
v2.5.0 Latest Latest
Warning

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

Go to latest
Published: Jan 15, 2020 License: Apache-2.0 Imports: 13 Imported by: 5

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultPDeathSignal = syscall.SIGKILL

DefaultPDeathSignal is default signal used for parent death process attribute

View Source
var DefaultPlugin = *NewPlugin()

DefaultPlugin is a default instance of Plugin.

Functions

This section is empty.

Types

type Config

type Config struct {
	TemplatePath string `json:"template-path"`
}

Config contains information about the path where process templates are stored

type Deps

type Deps struct {
	infra.PluginDeps
}

Deps define process dependencies

type Option

type Option func(*Plugin)

Option is a function that can be used in NewPlugin to customize Plugin.

func UseDeps

func UseDeps(cb func(*Deps)) Option

UseDeps returns Option that can inject custom dependencies.

type POption

type POption func(*POptions)

POption is helper function to set process options

func Args

func Args(args ...string) POption

Args if process should start with arguments

func AutoTerminate

func AutoTerminate() POption

AutoTerminate causes that zombie processes are automatically terminated

func CPUAffinityMask

func CPUAffinityMask(affinity string, delay time.Duration) POption

CPUAffinityMask allows to set CPU affinity to given process

func Detach

func Detach() POption

Detach process from parent after start, so it can survive after parent process is terminated

func EnvVar

func EnvVar(env []string) POption

EnvVar allows to set custom environment variables. If not set, os.Environ is used instead

func Notify

func Notify(notifyChan chan status.ProcessStatus) POption

Notify will send process status change notifications to the provided channel Note: caller should not close the channel, since plugin is a sender, it handles the close

func Restarts

func Restarts(restart int32) POption

Restarts defines number of automatic restarts of given process

func Template

func Template(runOnStartup bool) POption

Template will be created for given process. Process template also requires a flag whether the process should be started automatically with plugin

func Writer

func Writer(outW, errW io.Writer) POption

Writer allows to use custom writer instance. Can be defined with nil parameters, in such a case standard output will be used

type POptions

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

POptions is common object which holds all selected options

type Plugin

type Plugin struct {
	Deps
	// contains filtered or unexported fields
}

Plugin implements API to manage processes. There are two options to add a process to manage, start it as a new one or attach to an existing process. In both cases, the process is stored internally as known to the plugin.

func NewPlugin

func NewPlugin(opts ...Option) *Plugin

NewPlugin creates a new Plugin with the provided Options.

func (*Plugin) AttachProcess

func (p *Plugin) AttachProcess(name string, cmd string, pid int, options ...POption) (ProcessInstance, error)

AttachProcess attaches to existing process, reads its status and starts process status watcher

func (*Plugin) Close

func (p *Plugin) Close() error

Close stops all process watcher. Processes are either kept running (if detached) or terminated automatically if thay are child processes of the application

func (*Plugin) Delete

func (p *Plugin) Delete(name string) error

Delete releases the process resources and removes it from the plugin cache

func (*Plugin) GetAllProcesses

func (p *Plugin) GetAllProcesses() []ProcessInstance

GetAllProcesses returns all processes known to plugin

func (*Plugin) GetAllTemplates

func (p *Plugin) GetAllTemplates() ([]*process.Template, error)

GetAllTemplates returns all templates

func (*Plugin) GetProcessByName

func (p *Plugin) GetProcessByName(name string) ProcessInstance

GetProcessByName uses process name to find a desired instance

func (*Plugin) GetProcessByPID

func (p *Plugin) GetProcessByPID(pid int) ProcessInstance

GetProcessByPID uses process ID to find a desired instance

func (*Plugin) GetTemplate

func (p *Plugin) GetTemplate(name string) (*process.Template, error)

GetTemplate returns template with given name

func (*Plugin) Init

func (p *Plugin) Init() error

Init reads plugin config file for process template path. If exists, plugin initializes template reader, reads all existing templates and initializes them. Those marked as 'run on startup' are immediately started

func (*Plugin) NewProcess

func (p *Plugin) NewProcess(name, cmd string, options ...POption) ProcessInstance

NewProcess creates a new process and saves its template if required

func (*Plugin) NewProcessFromTemplate

func (p *Plugin) NewProcessFromTemplate(tmp *process.Template) ProcessInstance

NewProcessFromTemplate creates a new process from template file

func (*Plugin) String

func (p *Plugin) String() string

String returns string representation of the plugin

type Process

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

Process is wrapper around the os.Process

func (*Process) GetArguments

func (p *Process) GetArguments() []string

GetArguments returns arguments process was started with, if any. May be empty also for attached processes

func (*Process) GetCommand

func (p *Process) GetCommand() string

GetCommand returns command used to start process. May be empty for attached processes

func (*Process) GetInstanceName

func (p *Process) GetInstanceName() string

GetInstanceName returns process name of the instance

func (*Process) GetName

func (p *Process) GetName() string

GetName returns plugin-wide process name

func (*Process) GetNotificationChan

func (p *Process) GetNotificationChan() <-chan status.ProcessStatus

GetNotificationChan returns channel listening on notifications about process status changes

func (*Process) GetPid

func (p *Process) GetPid() int

GetPid returns process ID

func (*Process) GetStartTime

func (p *Process) GetStartTime() time.Time

GetStartTime returns process start timestamp

func (*Process) GetStatus

func (p *Process) GetStatus(pid int) (statusFile *status.File, err error)

GetStatus updates actual process status and returns status file

func (*Process) GetUptime

func (p *Process) GetUptime() time.Duration

GetUptime returns process uptime since the last start

func (*Process) IsAlive

func (p *Process) IsAlive() bool

IsAlive checks whether the process is running sending zero signal. Only a simple check, does not return error

func (*Process) Kill

func (p *Process) Kill() error

Kill sends the SIGKILL signal to force stop given process

func (*Process) Restart

func (p *Process) Restart() (err error)

Restart the process, or start it if it is not running

func (*Process) Signal

func (p *Process) Signal(signal os.Signal) error

Signal sends custom signal to the process

func (*Process) Start

func (p *Process) Start() (err error)

Start a process with defined arguments. Every process is watched for liveness and status changes

func (*Process) Stop

func (p *Process) Stop() error

Stop sends the SIGTERM signal to stop given process

func (*Process) StopAndWait

func (p *Process) StopAndWait() (*os.ProcessState, error)

StopAndWait sends the SIGTERM signal to stop given process and waits until it is completed

func (*Process) Wait

func (p *Process) Wait() (*os.ProcessState, error)

Wait for the process to exit, and then returns its state.

type ProcessInstance

type ProcessInstance interface {
	// Start starts the process. Depending on the procedure result, the status is set to 'running' or 'failed'. Start
	// also stores *os.Process in the instance for future use.
	Start() error
	// Restart briefly stops and starts the process. If the process is not running, it is started.
	Restart() error
	// Stop sends the termination signal to the process. The status is set to 'stopped' (or 'failed' if not successful).
	// Attempt to stop a non-existing process instance results in error
	Stop() error
	// Stop sends the termination signal to the process. The status is set to 'stopped' (or 'failed' if not successful).
	// Attempt to stop a non-existing process instance results in error
	StopAndWait() (*os.ProcessState, error)
	// Kill immediately terminates the process and releases all resources associated with it. Attempt to kill
	// a non-existing process instance results in error
	Kill() error
	// Wait for process to exit and return its process state describing its status and error (if any)
	Wait() (*os.ProcessState, error)
	// Signal allows user to send a user-defined signal
	Signal(signal os.Signal) error
	// IsAlive returns true if process is alive, or false if not or if the inner instance does not exist.
	IsAlive() bool
	// GetNotification returns channel to watch process availability/status.
	GetNotificationChan() <-chan status.ProcessStatus
	// GetName returns process name
	GetName() string
	// GetInstanceName returns process name from status
	GetInstanceName() string
	// GetPid returns process ID, or zero if process instance does not exist
	GetPid() int
	// GetStatus reads and returns all current plugin-defined process state data
	GetStatus(pid int) (*status.File, error)
	// GetCommand returns process command
	GetCommand() string
	// GetArguments returns process arguments if set
	GetArguments() []string
	// GetStartTime returns time when the process was started
	GetStartTime() time.Time
	// GetUptime returns time elapsed since the process started
	GetUptime() time.Duration
}

ProcessInstance defines methods to manage a given process

type ProcessManager

type ProcessManager interface {
	// NewProcess creates new process instance with name, command to start and other options (arguments, policy).
	// New process is not immediately started, process instance comprises from a set of methods to manage.
	NewProcess(name, cmd string, options ...POption) ProcessInstance
	// Starts process from template file
	NewProcessFromTemplate(tmp *process.Template) ProcessInstance
	// Attach to existing process using its process ID. The process is stored under the provided name. Error
	// is returned if process does not exits
	AttachProcess(name, cmd string, pid int, options ...POption) (ProcessInstance, error)
	// GetProcessByName returns existing process instance using name
	GetProcessByName(name string) ProcessInstance
	// GetProcessByName returns existing process instance using PID
	GetProcessByPID(pid int) ProcessInstance
	// GetAll returns all processes known to plugin
	GetAllProcesses() []ProcessInstance
	// Delete removes process from the memory. Delete cancels process watcher, but does not stop the running instance
	// (possible to attach later). Note: no process-related templates are removed
	Delete(name string) error
	// GetTemplate returns process template object with given name fom provided path. Returns nil if does not exists
	// or error if the reader is not available
	GetTemplate(name string) (*process.Template, error)
	// GetAllTemplates returns all templates available from given path. Returns empty list if
	// the reader is not available
	GetAllTemplates() ([]*process.Template, error)
}

ProcessManager defines methods to create, delete or manage processes

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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