process

package
v1.1.3 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2025 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExecTimeout added in v1.1.3

func ExecTimeout(d time.Duration, name string, args ...string) error

exec another process if wait d Duration, it will kill the process d is <= 0, wait forever

func PadString

func PadString(str string, totalSize int) string

PadString will add totalSize spaces evenly to the right and left side of str. Returns str after applying the pad.

func SafeReadTomlFile

func SafeReadTomlFile(filename string, v any) error

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

func SafeWriteTomlFile(v any, filename string) error

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 NewCli

func NewCli(dsn string, timeout time.Duration, l logging.Logger) (*Cli, error)

NewCli initiates a remote client connecting to dsn. Returns a Cli instance.

func (*Cli) DeleteProcess

func (cli *Cli) DeleteProcess(procName string) error

DeleteProcess will stop and delete all dependencies from process procName forever.

func (*Cli) RestartProcess

func (cli *Cli) RestartProcess(procName string) error

RestartProcess will try to restart a process with procName. Note that this process must have been already started through StartGoBin.

func (*Cli) Resurrect

func (cli *Cli) Resurrect() error

Resurrect will restore all previously save processes. Display an error in case there's any.

func (*Cli) Save

func (cli *Cli) Save() error

Save will save all previously saved processes onto a list. Display an error in case there's any.

func (*Cli) StartGoBin

func (cli *Cli) StartGoBin(sourcePath string, name string, keepAlive bool, args []string) error

StartGoBin will try to start a go binary process. Returns a fatal error in case there's any.

func (*Cli) StartProcess

func (cli *Cli) StartProcess(procName string) error

StartProcess will try to start a process with procName. Note that this process must have been already started through StartGoBin.

func (*Cli) Status

func (cli *Cli) Status() error

Status will display the status of all procs started through StartGoBin.

func (*Cli) StopProcess

func (cli *Cli) StopProcess(procName string) error

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

func NewMaster(configFile string) *Master

NewMaster will start a master instance with configFile. It returns a Master instance.

func (*Master) DeleteProcess

func (master *Master) DeleteProcess(name string) error

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

func (master *Master) RestartProcess(name string) error

RestartProcess will restart a process.

func (*Master) Revive

func (master *Master) Revive() error

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

func (master *Master) SaveProcs() error

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

func (master *Master) StartProcess(name string) error

StartProcess will a start a process.

func (*Master) Stop

func (master *Master) Stop() error

Stop will stop APM and all of its running procs.

func (*Master) StopProcess

func (master *Master) StopProcess(name string) error

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) AddRestart

func (proc *Proc) AddRestart()

Add one restart to proc status

func (*Proc) Delete

func (proc *Proc) Delete() error

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

func (proc *Proc) ForceStop() error

ForceStop will forcefully send a SIGKILL signal to process killing it instantly. Returns an error in case there's any.

func (*Proc) GetPid

func (proc *Proc) GetPid() int

Return proc current PID

func (*Proc) GetStatus

func (proc *Proc) GetStatus() *ProcStatus

Return proc current status

func (*Proc) GracefullyStop

func (proc *Proc) GracefullyStop() error

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

func (proc *Proc) Identifier() string

Proc identifier that will be used by watcher to keep track of its processes

func (*Proc) IsAlive

func (proc *Proc) IsAlive() bool

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

func (proc *Proc) Restart() error

Restart will try to gracefully stop the process and then Start it again. Returns an error in case there's any.

func (*Proc) SetStatus

func (proc *Proc) SetStatus(status string)

Set proc status

func (*Proc) ShouldKeepAlive

func (proc *Proc) ShouldKeepAlive() bool

Returns true if the process should be kept alive or not

func (*Proc) Start

func (proc *Proc) Start() error

Start will execute the command Cmd that should run the process. It will also create an out, err and pidfile in case they do not exist yet. Returns an error in case there's any.

func (*Proc) Watch

func (proc *Proc) Watch() (*os.ProcessState, error)

Watch will stop execution and wait until the process change its state. Usually changing state, means that the process died. Returns a tuple with the new process state and an error in case there's any.

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

type Watcher struct {
	sync.Mutex
	// contains filtered or unexported fields
}

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

func (watcher *Watcher) StopWatcher(identifier string) chan bool

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.

Jump to

Keyboard shortcuts

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