Documentation ¶
Index ¶
- Variables
- type Config
- type Deps
- type Option
- type POption
- func Args(args ...string) POption
- func AutoTerminate() POption
- func CPUAffinityMask(affinity string, delay time.Duration) POption
- func Detach() POption
- func EnvVar(env []string) POption
- func Notify(notifyChan chan status.ProcessStatus) POption
- func Restarts(restart int32) POption
- func Template(runOnStartup bool) POption
- func Writer(outW, errW io.Writer) POption
- type POptions
- type Plugin
- func (p *Plugin) AttachProcess(name string, cmd string, pid int, options ...POption) (ProcessInstance, error)
- func (p *Plugin) Close() error
- func (p *Plugin) Delete(name string) error
- func (p *Plugin) GetAllProcesses() []ProcessInstance
- func (p *Plugin) GetAllTemplates() ([]*process.Template, error)
- func (p *Plugin) GetProcessByName(name string) ProcessInstance
- func (p *Plugin) GetProcessByPID(pid int) ProcessInstance
- func (p *Plugin) GetTemplate(name string) (*process.Template, error)
- func (p *Plugin) Init() error
- func (p *Plugin) NewProcess(name, cmd string, options ...POption) ProcessInstance
- func (p *Plugin) NewProcessFromTemplate(tmp *process.Template) ProcessInstance
- func (p *Plugin) String() string
- type Process
- func (p *Process) GetArguments() []string
- func (p *Process) GetCommand() string
- func (p *Process) GetInstanceName() string
- func (p *Process) GetName() string
- func (p *Process) GetNotificationChan() <-chan status.ProcessStatus
- func (p *Process) GetPid() int
- func (p *Process) GetStartTime() time.Time
- func (p *Process) GetStatus(pid int) (statusFile *status.File, err error)
- func (p *Process) GetUptime() time.Duration
- func (p *Process) IsAlive() bool
- func (p *Process) Kill() error
- func (p *Process) Restart() (err error)
- func (p *Process) Signal(signal os.Signal) error
- func (p *Process) Start() (err error)
- func (p *Process) Stop() error
- func (p *Process) StopAndWait() (*os.ProcessState, error)
- func (p *Process) Wait() (*os.ProcessState, error)
- type ProcessInstance
- type ProcessManager
Constants ¶
This section is empty.
Variables ¶
var DefaultPDeathSignal = syscall.SIGKILL
DefaultPDeathSignal is default signal used for parent death process attribute
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 Option ¶
type Option func(*Plugin)
Option is a function that can be used in NewPlugin to customize Plugin.
type POption ¶
type POption func(*POptions)
POption is helper function to set process options
func AutoTerminate ¶
func AutoTerminate() POption
AutoTerminate causes that zombie processes are automatically terminated
func CPUAffinityMask ¶
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 ¶
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
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 (*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 ¶
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) GetAllProcesses ¶
func (p *Plugin) GetAllProcesses() []ProcessInstance
GetAllProcesses returns all processes known to plugin
func (*Plugin) GetAllTemplates ¶
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 ¶
GetTemplate returns template with given name
func (*Plugin) Init ¶
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
type Process ¶
type Process struct {
// contains filtered or unexported fields
}
Process is wrapper around the os.Process
func (*Process) GetArguments ¶
GetArguments returns arguments process was started with, if any. May be empty also for attached processes
func (*Process) GetCommand ¶
GetCommand returns command used to start process. May be empty for attached processes
func (*Process) GetInstanceName ¶
GetInstanceName returns process name of the instance
func (*Process) GetNotificationChan ¶
func (p *Process) GetNotificationChan() <-chan status.ProcessStatus
GetNotificationChan returns channel listening on notifications about process status changes
func (*Process) GetStartTime ¶
GetStartTime returns process start timestamp
func (*Process) IsAlive ¶
IsAlive checks whether the process is running sending zero signal. Only a simple check, does not return error
func (*Process) Start ¶
Start a process with defined arguments. Every process is watched for liveness and status changes
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
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