Documentation
¶
Index ¶
- func ExecTimeout(d time.Duration, name string, args ...string) error
- func PadString(str string, totalSize int) string
- func SafeReadTomlFile(filename string, v any) error
- func SafeWriteTomlFile(v any, filename string) error
- type Cli
- func (cli *Cli) DeleteProcess(procName string) error
- func (cli *Cli) RestartProcess(procName string) error
- func (cli *Cli) Resurrect() error
- func (cli *Cli) Save() error
- func (cli *Cli) StartGoBin(sourcePath string, name string, keepAlive bool, args []string) error
- func (cli *Cli) StartProcess(procName string) error
- func (cli *Cli) Status() error
- func (cli *Cli) StopProcess(procName string) error
- type DecodableMaster
- type GoBin
- type Master
- func (master *Master) DeleteProcess(name string) error
- func (master *Master) ListProcs() []ProcContainer
- func (master *Master) Prepare(sourcePath string, name string, language string, keepAlive bool, args []string) (ProcPreparable, []byte, error)
- func (master *Master) RestartProcess(name string) error
- func (master *Master) Revive() error
- func (master *Master) RunPreparable(procPreparable ProcPreparable) error
- func (master *Master) SaveProcs() error
- func (master *Master) SaveProcsLoop()
- func (master *Master) StartProcess(name string) error
- func (master *Master) Stop() error
- func (master *Master) StopProcess(name string) error
- func (master *Master) UpdateStatus()
- func (master *Master) WatchProcs()
- type Preparable
- type Proc
- func (proc *Proc) AddRestart()
- func (proc *Proc) Delete() error
- func (proc *Proc) ForceStop() error
- func (proc *Proc) GetPid() int
- func (proc *Proc) GetStatus() *ProcStatus
- func (proc *Proc) GracefullyStop() error
- func (proc *Proc) Identifier() string
- func (proc *Proc) IsAlive() bool
- func (proc *Proc) NotifyStopped()
- func (proc *Proc) Restart() error
- func (proc *Proc) SetStatus(status string)
- func (proc *Proc) ShouldKeepAlive() bool
- func (proc *Proc) Start() error
- func (proc *Proc) Watch() (*os.ProcessState, error)
- type ProcContainer
- type ProcDataResponse
- type ProcPreparable
- type ProcResponse
- type ProcState
- type ProcStatus
- type ProcWatcher
- type RemoteClient
- func (client *RemoteClient) DeleteProcess(procName string) error
- func (client *RemoteClient) MonitStatus() (ProcResponse, error)
- func (client *RemoteClient) RestartProcess(procName string) error
- func (client *RemoteClient) Resurrect() error
- func (client *RemoteClient) Save() error
- func (client *RemoteClient) StartGoBin(sourcePath string, name string, keepAlive bool, args []string) error
- func (client *RemoteClient) StartProcess(procName string) error
- func (client *RemoteClient) StopProcess(procName string) error
- type RemoteMaster
- func (m *RemoteMaster) DeleteProcess(procName string, ack *bool) error
- func (m *RemoteMaster) MonitStatus(req string, response *ProcResponse) error
- func (m *RemoteMaster) RestartProcess(procName string, ack *bool) error
- func (m *RemoteMaster) Resurrect(req string, ack *bool) error
- func (m *RemoteMaster) Save(req string, ack *bool) error
- func (m *RemoteMaster) StartGoBin(goBin *GoBin, ack *bool) error
- func (m *RemoteMaster) StartProcess(procName string, ack *bool) error
- func (m *RemoteMaster) Stop() error
- func (m *RemoteMaster) StopProcess(procName string, ack *bool) error
- type Watcher
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ExecTimeout ¶ added in v1.1.3
exec another process if wait d Duration, it will kill the process d is <= 0, wait forever
func PadString ¶
PadString will add totalSize spaces evenly to the right and left side of str. Returns str after applying the pad.
func SafeReadTomlFile ¶
SafeReadTomlFile will try to acquire a lock on the file and then read its content afterwards. Returns an error in case there's any.
func SafeWriteTomlFile ¶
SafeWriteTomlFile will try to acquire a lock on the file and then write to it. Returns an error in case there's any.
Types ¶
type Cli ¶
type Cli struct {
// contains filtered or unexported fields
}
Cli is the command line client.
func (*Cli) DeleteProcess ¶
DeleteProcess will stop and delete all dependencies from process procName forever.
func (*Cli) RestartProcess ¶
RestartProcess will try to restart a process with procName. Note that this process must have been already started through StartGoBin.
func (*Cli) Resurrect ¶
Resurrect will restore all previously save processes. Display an error in case there's any.
func (*Cli) Save ¶
Save will save all previously saved processes onto a list. Display an error in case there's any.
func (*Cli) StartGoBin ¶
StartGoBin will try to start a go binary process. Returns a fatal error in case there's any.
func (*Cli) StartProcess ¶
StartProcess will try to start a process with procName. Note that this process must have been already started through StartGoBin.
func (*Cli) StopProcess ¶
StopProcess will try to stop a process named procName.
type DecodableMaster ¶
type DecodableMaster struct {
SysFolder string
PidFile string
OutFile string
ErrFile string
Watcher *Watcher
Procs map[string]*Proc
}
DecodableMaster is a struct that the config toml file will decode to. It is needed because toml decoder doesn't decode to interfaces, so the Procs map can't be decoded as long as we use the ProcContainer interface
type GoBin ¶
type GoBin struct {
SourcePath string // SourcePath is the package path. (Ex: github.com/topfreegames/apm)
Name string // Name is the process name that will be given to the process.
KeepAlive bool // KeepAlive will determine whether APM should keep the proc live or not.
Args []string // Args is an array containing all the extra args that will be passed to the binary after compilation.
}
GoBin is a struct that represents the necessary arguments for a go binary to be built.
type Master ¶
type Master struct {
sync.Mutex
SysFolder string // SysFolder is the main APM folder where the necessary config files will be stored.
PidFile string // PidFille is the APM pid file path.
OutFile string // OutFile is the APM output log file path.
ErrFile string // ErrFile is the APM err log file path.
Watcher *Watcher // Watcher is a watcher instance.
Procs map[string]ProcContainer // Procs is a map containing all procs started on APM.
}
Master is the main module that keeps everything in place and execute the necessary actions to keep the process running as they should be.
func NewMaster ¶
NewMaster will start a master instance with configFile. It returns a Master instance.
func (*Master) DeleteProcess ¶
DeleteProcess will delete a process and all its files and childs forever.
func (*Master) ListProcs ¶
func (master *Master) ListProcs() []ProcContainer
ListProcs will return a list of all procs.
func (*Master) Prepare ¶
func (master *Master) Prepare(sourcePath string, name string, language string, keepAlive bool, args []string) (ProcPreparable, []byte, error)
Prepare will compile the source code into a binary and return a preparable ready to be executed.
func (*Master) RestartProcess ¶
RestartProcess will restart a process.
func (*Master) Revive ¶
Revive will revive all procs listed on ListProcs. This should ONLY be called during Master startup.
func (*Master) RunPreparable ¶
func (master *Master) RunPreparable(procPreparable ProcPreparable) error
RunPreparable will run procPreparable and add it to the watch list in case everything goes well.
func (*Master) SaveProcs ¶
SaveProcs will save a list of procs onto a file inside configPath. Returns an error in case there's any.
func (*Master) SaveProcsLoop ¶
func (master *Master) SaveProcsLoop()
SaveProcsLoop will loop forever to save the list of procs onto the proc file.
func (*Master) StartProcess ¶
StartProcess will a start a process.
func (*Master) StopProcess ¶
StopProcess will stop a process with the given name.
func (*Master) UpdateStatus ¶
func (master *Master) UpdateStatus()
UpdateStatus will update a process status every 30s.
func (*Master) WatchProcs ¶
func (master *Master) WatchProcs()
WatchProcs will keep the procs running forever.
type Preparable ¶
type Preparable struct {
Name string // The name of the process
SourcePath string // The path to the source code of the process
Cmd string // The command to be executed
SysFolder string // The folder where all the process files will be stored
Language string // The language of the process, ie: go, python, nodejs, etc.
KeepAlive bool // If true, the process will be kept alive after it exits
Args []string // The arguments to be passed to the process
Env []string // The environment variables to be passed to the process, ie: PORT=8080, DEBUG=true
}
ProcPreparable is a preparable with all the necessary informations to run a process. To actually run a process, call the Start() method.
func (*Preparable) Identifier ¶
func (preparable *Preparable) Identifier() string
func (*Preparable) PrepareBin ¶
func (preparable *Preparable) PrepareBin() ([]byte, error)
PrepareBin will compile the Golang project from SourcePath and populate Cmd with the proper command for the process to be executed. Returns the compile command output.
func (*Preparable) Start ¶
func (preparable *Preparable) Start() (ProcContainer, error)
Start will execute the process based on the information presented on the preparable. This function should be called from inside the master to make sure all the watchers and process handling are done correctly. Returns a tuple with the process and an error in case there's any.
type Proc ¶
type Proc struct {
Name string // process name
Cmd string // process command
Args []string // process arguments
Env []string // process environment variables
Path string // process path
Pidfile string // process pid file
Outfile string // process out file
Errfile string // process err file
KeepAlive bool // should the process be kept alive after stopping
Pid int // process pid
Status *ProcStatus // process status
// contains filtered or unexported fields
}
Proc is a os.Process wrapper with Status and more info that will be used on Master to maintain the process health.
func (*Proc) Delete ¶
Delete will delete everything created by this process, including the out, err and pid file. Returns an error in case there's any.
func (*Proc) ForceStop ¶
ForceStop will forcefully send a SIGKILL signal to process killing it instantly. Returns an error in case there's any.
func (*Proc) GracefullyStop ¶
GracefullyStop will send a SIGTERM signal asking the process to terminate. The process may choose to die gracefully or ignore this signal completely. In that case the process will keep running unless you call ForceStop() Returns an error in case there's any.
func (*Proc) Identifier ¶
Proc identifier that will be used by watcher to keep track of its processes
func (*Proc) IsAlive ¶
IsAlive will check if the process is alive or not. Returns true if the process is alive or false otherwise.
func (*Proc) NotifyStopped ¶
func (proc *Proc) NotifyStopped()
Notify that process was stopped so we can set its PID to -1
func (*Proc) Restart ¶
Restart will try to gracefully stop the process and then Start it again. Returns an error in case there's any.
func (*Proc) ShouldKeepAlive ¶
Returns true if the process should be kept alive or not
type ProcContainer ¶
type ProcContainer interface {
Start() error
ForceStop() error
GracefullyStop() error
Restart() error
Delete() error
IsAlive() bool
Identifier() string
ShouldKeepAlive() bool
AddRestart()
NotifyStopped()
SetStatus(status string)
GetPid() int
GetStatus() *ProcStatus
Watch() (*os.ProcessState, error)
}
type ProcDataResponse ¶
type ProcDataResponse struct {
Name string
Pid int
Status *ProcStatus
KeepAlive bool
}
type ProcPreparable ¶
type ProcPreparable interface {
PrepareBin() ([]byte, error)
Start() (ProcContainer, error)
Identifier() string
}
type ProcResponse ¶
type ProcResponse struct {
Procs []*ProcDataResponse
}
type ProcState ¶
type ProcState struct {
// contains filtered or unexported fields
}
ProcState is a wrapper with the process state and an error in case there's any.
type ProcStatus ¶
type ProcStatus struct {
Status string // "running", "stopped", "exited"
Restarts int // number of restarts
}
ProcStatus is a wrapper with the process current status.
func (*ProcStatus) AddRestart ¶
func (p *ProcStatus) AddRestart()
AddRestart will add one restart to the process status.
func (*ProcStatus) SetStatus ¶
func (p *ProcStatus) SetStatus(status string)
SetStatus will set the process string status.
type ProcWatcher ¶
type ProcWatcher struct {
// contains filtered or unexported fields
}
ProcWatcher is a wrapper that act as a object that watches a process.
type RemoteClient ¶
type RemoteClient struct {
// contains filtered or unexported fields
}
RemoteClient is a struct that holds the remote client instance.
func StartRemoteClient ¶
func StartRemoteClient(dsn string, timeout time.Duration) (*RemoteClient, error)
StartRemoteClient will start a remote client that can talk to a remote server that is already running on dsn address. It returns an error in case there's any or it could not connect within the timeout.
func (*RemoteClient) DeleteProcess ¶
func (client *RemoteClient) DeleteProcess(procName string) error
DeleteProcess is a wrapper that calls the remote DeleteProcess. It returns an error in case there's any.
func (*RemoteClient) MonitStatus ¶
func (client *RemoteClient) MonitStatus() (ProcResponse, error)
MonitStatus is a wrapper that calls the remote MonitStatus. It returns a tuple with a list of process and an error in case there's any.
func (*RemoteClient) RestartProcess ¶
func (client *RemoteClient) RestartProcess(procName string) error
RestartProcess is a wrapper that calls the remote RestartProcess. It returns an error in case there's any.
func (*RemoteClient) Resurrect ¶
func (client *RemoteClient) Resurrect() error
Resurrect will restore all previously save processes. Returns an error in case there's any.
func (*RemoteClient) Save ¶
func (client *RemoteClient) Save() error
Save will save a list of procs onto a file. Returns an error in case there's any.
func (*RemoteClient) StartGoBin ¶
func (client *RemoteClient) StartGoBin(sourcePath string, name string, keepAlive bool, args []string) error
StartGoBin is a wrapper that calls the remote StartsGoBin. It returns an error in case there's any.
func (*RemoteClient) StartProcess ¶
func (client *RemoteClient) StartProcess(procName string) error
StartProcess is a wrapper that calls the remote StartProcess. It returns an error in case there's any.
func (*RemoteClient) StopProcess ¶
func (client *RemoteClient) StopProcess(procName string) error
StopProcess is a wrapper that calls the remote StopProcess. It returns an error in case there's any.
type RemoteMaster ¶
type RemoteMaster struct {
// contains filtered or unexported fields
}
RemoteMaster is a struct that holds the master instance.
func StartRemoteMasterServer ¶
func StartRemoteMasterServer(dsn string, configFile string) (*RemoteMaster, error)
StartRemoteMasterServer starts a remote APM server listening on dsn address and binding to configFile. It returns a RemoteMaster instance.
func (*RemoteMaster) DeleteProcess ¶
func (m *RemoteMaster) DeleteProcess(procName string, ack *bool) error
DeleteProcess will delete a process with name procName. It returns an error in case there's any.
func (*RemoteMaster) MonitStatus ¶
func (m *RemoteMaster) MonitStatus(req string, response *ProcResponse) error
MonitStatus will query for the status of each process and bind it to procs pointer list. It returns an error in case there's any.
func (*RemoteMaster) RestartProcess ¶
func (m *RemoteMaster) RestartProcess(procName string, ack *bool) error
RestartProcess will restart a process that was previously built using GoBin. It returns an error in case there's any.
func (*RemoteMaster) Resurrect ¶
func (m *RemoteMaster) Resurrect(req string, ack *bool) error
Resurrect will restore all previously save processes. Returns an error in case there's any.
func (*RemoteMaster) Save ¶
func (m *RemoteMaster) Save(req string, ack *bool) error
Save will save the current running and stopped processes onto a file. Returns an error in case there's any.
func (*RemoteMaster) StartGoBin ¶
func (m *RemoteMaster) StartGoBin(goBin *GoBin, ack *bool) error
StartGoBin will build a binary based on the arguments passed on goBin, then it will start the process and keep it alive if KeepAlive is set to true. It returns an error and binds true to ack pointer.
func (*RemoteMaster) StartProcess ¶
func (m *RemoteMaster) StartProcess(procName string, ack *bool) error
StartProcess will start a process that was previously built using GoBin. It returns an error in case there's any.
func (*RemoteMaster) Stop ¶
func (m *RemoteMaster) Stop() error
Stop will stop APM remote server. It returns an error in case there's any.
func (*RemoteMaster) StopProcess ¶
func (m *RemoteMaster) StopProcess(procName string, ack *bool) error
StopProcess will stop a process that is currently running. It returns an error in case there's any.
type Watcher ¶
Watcher is responsible for watching a list of processes and report to Master in case the process dies at some point.
func NewWatcher ¶
func NewWatcher() *Watcher
NewWatcher will create a Watcher instance. Returns a Watcher instance.
func (*Watcher) AddProcWatcher ¶
func (watcher *Watcher) AddProcWatcher(proc ProcContainer)
AddProcWatcher will add a watcher on proc.
func (*Watcher) RestartProc ¶
func (watcher *Watcher) RestartProc() chan ProcContainer
RestartProc is a wrapper to export the channel restartProc. It basically keeps track of all the processes that died and need to be restarted. Returns a channel with the dead processes that need to be restarted.
func (*Watcher) StopWatcher ¶
StopWatcher will stop a running watcher on a process with identifier 'identifier' Returns a channel that will be populated when the watcher is finally done.