process

package
v1.2.2 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2024 License: Apache-2.0 Imports: 25 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// Stopped 已停止
	Stopped State = iota

	// Starting 启动中
	Starting = 10

	// Running 运行中
	Running = 20

	// Backoff 已挂起
	Backoff = 30

	// Stopping 停止中
	Stopping = 40

	// Exited 已退出
	Exited = 100

	// Fatal 启动失败
	Fatal = 200

	// Unknown 未知状态
	Unknown = 1000
)

Variables

This section is empty.

Functions

func DisableDebug

func DisableDebug()

func EnableDebug

func EnableDebug()

func Reap

func Reap()

Reap Normal entry point for the reaper code. Start reaping children in the

background inside a goroutine.

func ReapZombie

func ReapZombie()

ReapZombie 回收僵尸进程

func Start

func Start(config Config)

Start Entry point for invoking the reaper code with a specific configuration.

The config allows you to bypass the pid 1 checks, so handle with care.
The child processes are reaped in the background inside a goroutine.

Types

type AutoReStart added in v1.0.0

type AutoReStart string
const (
	AutoReStartUnexpected AutoReStart = "unexpected"
	AutoReStartTrue       AutoReStart = "true"
	AutoReStartFalse      AutoReStart = "false"
)

程序退出后自动重启

type Config

type Config struct {
	Pid              int
	Options          int
	DisablePid1Check bool
}

type Info

type Info struct {
	Name          string `json:"name"`
	Description   string `json:"description"`
	Start         int    `json:"start"`
	Stop          int    `json:"stop"`
	Now           int    `json:"now"`
	State         int    `json:"state"`
	StateName     string `json:"statename"`
	SpawnErr      string `json:"spawnerr"`
	ExitStatus    int    `json:"exitstatus"`
	Logfile       string `json:"logfile"`
	StdoutLogfile string `json:"stdout_logfile"`
	StderrLogfile string `json:"stderr_logfile"`
	Pid           int    `json:"pid"`
}

Info 进程的运行状态

type Manager

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

func NewManager

func NewManager() *Manager

NewManager 创建进程管理器

func (*Manager) Add

func (that *Manager) Add(name string, proc *Process)

Add 添加进程到Manager

func (*Manager) Clear

func (that *Manager) Clear()

Clear 清除进程

func (*Manager) Find

func (that *Manager) Find(name string) *Process

Find 根据进程名查询进程

func (*Manager) ForEachProcess

func (that *Manager) ForEachProcess(procFunc func(p *Process))

ForEachProcess 迭代进程列表

func (*Manager) GetAllProcessInfo

func (that *Manager) GetAllProcessInfo() ([]*Info, error)

GetAllProcessInfo 获取所有进程信息

func (*Manager) GetProcessInfo

func (that *Manager) GetProcessInfo(name string) (*Info, error)

GetProcessInfo 获取指定进程名的进程信息

func (*Manager) GracefulReload added in v1.0.0

func (that *Manager) GracefulReload(name string, wait bool) (bool, error)

GracefulReload 停止指定进程

func (*Manager) NewProcess

func (that *Manager) NewProcess(path string, args []string, environment map[string]string, opts ...ProcOption) (*Process, error)

NewProcess 创建进程 path: 可执行文件路径 args: 参数 environment: 环境变量

func (*Manager) NewProcessByOptions added in v1.0.0

func (that *Manager) NewProcessByOptions(opts ProcOptions) (*Process, error)

NewProcessByOptions 创建进程 entry: 配置对象

func (*Manager) NewProcessByProcess added in v1.0.0

func (that *Manager) NewProcessByProcess(proc *Process) (*Process, error)

NewProcessByProcess 创建进程 proc: Process对象

func (*Manager) NewProcessCmd

func (that *Manager) NewProcessCmd(cmd string, environment map[string]string) (*Process, error)

NewProcessCmd 创建进程 path: shell命令 environment: 环境变量

func (*Manager) Remove

func (that *Manager) Remove(name string) *Process

Remove 从Manager移除进程

func (*Manager) StartProcess

func (that *Manager) StartProcess(name string, wait bool) (bool, error)

StartProcess 启动指定进程

func (*Manager) StopAllProcesses

func (that *Manager) StopAllProcesses()

StopAllProcesses 关闭所有进程

func (*Manager) StopProcess

func (that *Manager) StopProcess(name string, wait bool) (bool, error)

StopProcess 停止指定进程

type ProcOption added in v1.0.0

type ProcOption func(*ProcOptions)

func ProcArgs added in v1.0.0

func ProcArgs(opt ...string) ProcOption

ProcArgs 启动参数

func ProcAutoReStart added in v1.0.0

func ProcAutoReStart(opt AutoReStart) ProcOption

ProcAutoReStart 程序退出后自动重启,可选值:[unexpected,true,false],默认为unexpected,表示进程意外杀死后才重启

func ProcAutoStart added in v1.0.0

func ProcAutoStart(opt bool) ProcOption

ProcAutoStart 启动的时候自动该进程启动

func ProcCommand added in v1.0.0

func ProcCommand(opt string) ProcOption

ProcCommand 启动命令

func ProcDirectory added in v1.0.0

func ProcDirectory(opt string) ProcOption

ProcDirectory 进程运行目录

func ProcEnvironment added in v1.0.0

func ProcEnvironment(opt map[string]string) ProcOption

func ProcExitCodes added in v1.0.0

func ProcExitCodes(opt ...int) ProcOption

ProcExitCodes 进程退出的code值列表,该列表中的值表示已知

func ProcExtraFiles added in v1.0.0

func ProcExtraFiles(opt []*os.File) ProcOption

ProcExtraFiles 设置打开的文件句柄列表

func ProcKillAsGroup added in v1.0.0

func ProcKillAsGroup(opt bool) ProcOption

ProcKillAsGroup 默认为false,向进程组发送kill信号,包括子进程

func ProcKillWaitSecs added in v1.0.0

func ProcKillWaitSecs(opt int) ProcOption

ProcKillWaitSecs 强杀进程等待秒数

func ProcName added in v1.0.0

func ProcName(opt string) ProcOption

ProcName 设置进程名称

func ProcPriority added in v1.0.0

func ProcPriority(opt int) ProcOption

ProcPriority 进程启动优先级,默认999,值小的优先启动

func ProcRedirectStderr added in v1.0.0

func ProcRedirectStderr(opt bool) ProcOption

ProcRedirectStderr 错误输出是否与标准输入一起

func ProcRestartPause added in v1.0.0

func ProcRestartPause(opt int) ProcOption

ProcRestartPause 进程重启间隔秒数,默认是0,表示不间隔

func ProcRestartWhenBinaryChanged added in v1.0.0

func ProcRestartWhenBinaryChanged(opt bool) ProcOption

ProcRestartWhenBinaryChanged 当进程的二进制文件有修改,是否需要重启

func ProcSetEnvironment added in v1.0.0

func ProcSetEnvironment(key, val string) ProcOption

ProcSetEnvironment 环境变量

func ProcSetExtend added in v1.0.0

func ProcSetExtend(key, val interface{}) ProcOption

ProcSetExtend 扩展参数

func ProcStartRetries added in v1.0.0

func ProcStartRetries(opt int) ProcOption

ProcStartRetries 启动失败自动重试次数,默认是3

func ProcStartSecs added in v1.0.0

func ProcStartSecs(opt int) ProcOption

ProcStartSecs 指定启动多少秒后没有异常退出,则表示启动成功 // 未设置该值,则表示cmd.Start方法调用为出错,则表示启动成功, // 设置了该值,则表示程序启动后需稳定运行指定的秒数后才算启动成功

func ProcStderrLog added in v1.0.0

func ProcStderrLog(file string, maxBytes string, backups ...int) ProcOption

ProcStderrLog 设置stderrlog的存放配置

func ProcStdoutLog added in v1.0.0

func ProcStdoutLog(file string, maxBytes string, backups ...int) ProcOption

ProcStdoutLog 设置stdoutlog的存放配置

func ProcStopAsGroup added in v1.0.0

func ProcStopAsGroup(opt bool) ProcOption

ProcStopAsGroup 默认为false,进程被杀死时,是否向这个进程组发送stop信号,包括子进程

func ProcStopSignal added in v1.0.0

func ProcStopSignal(opt ...string) ProcOption

ProcStopSignal 结束进程发送的信号列表

func ProcStopWaitSecs added in v1.0.0

func ProcStopWaitSecs(opt int) ProcOption

ProcStopWaitSecs 发送结束进程的信号后等待的秒数

func ProcUser added in v1.0.0

func ProcUser(opt string) ProcOption

ProcUser 用哪个用户启动进程,默认是父进程的所属用户

type ProcOptions added in v1.0.0

type ProcOptions struct {
	//进程名称
	Name string
	// 启动命令
	Command string
	// 启动参数
	Args []string

	//进程运行目录
	Directory string
	//启动的时候自动该进程启动
	AutoStart bool
	//启动10秒后没有异常退出,就表示进程正常启动了,默认为1秒
	StartSecs int
	//程序退出后自动重启,可选值:[unexpected,true,false],默认为unexpected,表示进程意外杀死后才重启
	AutoReStart AutoReStart
	// 进程退出的code值
	ExitCodes []int
	//启动失败自动重试次数,默认是3
	StartRetries int
	//进程重启间隔秒数,默认是0,表示不间隔
	RestartPause int
	//用哪个用户启动进程,默认是父进程的所属用户
	User string
	//进程启动优先级,默认999,值小的优先启动
	Priority int

	//日志文件,需要注意当指定目录不存在时无法正常启动,所以需要手动创建目录(supervisord 会自动创建日志文件)
	StdoutLogfile string
	//stdout 日志文件大小,默认50MB
	StdoutLogFileMaxBytes int
	//stdout 日志文件备份数,默认是10
	StdoutLogFileBackups int
	// 把stderr重定向到stdout,默认false
	RedirectStderr bool
	// 日志文件,进程启动后的标准错误写入该文件
	StderrLogfile string
	//stderr 日志文件大小,默认50MB
	StderrLogFileMaxBytes int
	//stderr 日志文件备份数,默认是10
	StderrLogFileBackups int

	//默认为false,进程被杀死时,是否向这个进程组发送stop信号,包括子进程
	StopAsGroup bool
	//默认为false,向进程组发送kill信号,包括子进程
	KillAsGroup bool
	//结束进程发送的信号
	StopSignal []string
	// 发送结束进程的信号后等待的秒数
	StopWaitSecs int
	// 强杀进程等待秒数
	KillWaitSecs int
	// 环境变量
	Environment *gmap.StrStrMap
	//当进程的二进制文件有修改,是否需要重启,默认false
	RestartWhenBinaryChanged bool

	// 继承主进程已经打开的文件列表
	ExtraFiles []*os.File

	// 扩展参数
	Extend *gmap.AnyAnyMap
}

func NewProcOptions added in v1.0.0

func NewProcOptions(opts ...ProcOption) ProcOptions

NewProcOptions 创建进程启动配置

func (ProcOptions) CreateCommand added in v1.0.0

func (that ProcOptions) CreateCommand() (*exec.Cmd, error)

CreateCommand 根据就配置生成cmd对象

type Process

type Process struct {
	// 进程管理对象
	Manager *Manager

	StdoutLog proclog.Logger
	StderrLog proclog.Logger
	// contains filtered or unexported fields
}

func NewProcess

func NewProcess(opts ...ProcOption) *Process

NewProcess 创建进程对象

func NewProcessByOptions added in v1.0.0

func NewProcessByOptions(options ProcOptions) *Process

NewProcessByOptions 通过详细配置,创建进程对象

func NewProcessCmd

func NewProcessCmd(cmd string, environment map[string]string) *Process

NewProcessCmd 按命令启动

func (*Process) Clone added in v1.0.0

func (that *Process) Clone() (*Process, error)

Clone 进程

func (*Process) GetDescription

func (that *Process) GetDescription() string

GetDescription 获取进程描述

func (*Process) GetExitStatus

func (that *Process) GetExitStatus() int

GetExitStatus 获取进程退出状态

func (*Process) GetName

func (that *Process) GetName() string

GetName 获取进程名

func (*Process) GetProcessInfo

func (that *Process) GetProcessInfo() *Info

GetProcessInfo 获取进程的详情

func (*Process) GetStartTime

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

GetStartTime 获取进程启动时间

func (*Process) GetState

func (that *Process) GetState() State

GetState 获取进程状态

func (*Process) GetStatus

func (that *Process) GetStatus() string

GetStatus 获取进程当前状态

func (*Process) GetStderrLogfile

func (that *Process) GetStderrLogfile() string

GetStderrLogfile 获取标准错误将要写入的日志文件

func (*Process) GetStdoutLogfile

func (that *Process) GetStdoutLogfile() string

GetStdoutLogfile 获取标准输出将要写入的日志文件

func (*Process) GetStopTime

func (that *Process) GetStopTime() time.Time

GetStopTime 获取进程结束时间

func (*Process) Pid

func (that *Process) Pid() int

Pid 获取进程pid,返回0表示进程未启动

func (*Process) Signal

func (that *Process) Signal(sig os.Signal, sigChildren bool) error

Signal 向进程发送信号 sig: 要发送的信号 sigChildren: 如果为true,则信号会发送到该进程的子进程

func (*Process) Start

func (that *Process) Start(wait bool)

Start 启动进程,wait表示阻塞等待进程启动成功

func (*Process) Stop

func (that *Process) Stop(wait bool)

Stop 主动停止进程

type State

type State int

func (State) String

func (p State) String() string

String 把进程状态转换成可识别的字符串

Jump to

Keyboard shortcuts

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