Documentation
¶
Index ¶
- Variables
- func IsFatal(err error) bool
- func IsFatalErr(err error) bool
- func IsNoRestartErr(err error) bool
- func NoRestartErr(err error) error
- type ExitStatus
- type FatalErr
- type Manager
- func (m *Manager) Add(srv Service, opts ...Option) error
- func (m *Manager) AddWithConfig(srv Service, config ServiceConfig) error
- func (m *Manager) Delete(name string) error
- func (m *Manager) GetServiceInfo(name string) (*ServiceInfo, error)
- func (m *Manager) GetServicesInfo() []*ServiceInfo
- func (m *Manager) Has(name string) bool
- func (m *Manager) OnClose(fn func())
- func (m *Manager) RemoveServices() error
- func (m *Manager) ResetService(name string) error
- func (m *Manager) RestartService(name string) error
- func (m *Manager) RestartServices() error
- func (m *Manager) Run(ctx context.Context) error
- func (m *Manager) Serve(ctx context.Context) error
- func (m *Manager) ServeBackground(ctx context.Context) <-chan error
- func (m *Manager) Services() []Service
- func (m *Manager) StartService(name string) error
- func (m *Manager) StopService(name string) error
- type Metric
- type Option
- func WithAutoStart(autoStart bool) Option
- func WithBackoff(maxDelay time.Duration, multiplier float64) Option
- func WithMaxRestarts(n int) Option
- func WithRestartDelay(d time.Duration) Option
- func WithRestartPolicy(policy RestartPolicy) Option
- func WithRestartWindow(window time.Duration, maxRestarts int) Option
- type RestartPolicy
- type Service
- type ServiceConfig
- type ServiceInfo
- type ServiceStatus
Constants ¶
This section is empty.
Variables ¶
View Source
var ( // ErrServiceNotFound 服务未找到 ErrServiceNotFound = errors.New("service not found") // ErrServiceAlreadyExists 服务已存在 ErrServiceAlreadyExists = errors.New("service already exists") // ErrServiceStopped 服务已停止 ErrServiceStopped = errors.New("service is stopped") // ErrServiceRunning 服务正在运行 ErrServiceRunning = errors.New("service is running") // ErrDoNotRestart 不要重启服务 ErrDoNotRestart = errors.New("do not restart") // ErrTerminateSupervisor 终止 supervisor ErrTerminateSupervisor = errors.New("terminate supervisor") )
预定义错误
Functions ¶
Types ¶
type ExitStatus ¶
type ExitStatus int
ExitStatus 退出状态码
const ( ExitSuccess ExitStatus = 0 ExitError ExitStatus = 1 ExitNoUpgradeAvailable ExitStatus = 2 ExitRestart ExitStatus = 3 ExitUpgrade ExitStatus = 4 )
func (ExitStatus) AsInt ¶
func (s ExitStatus) AsInt() int
type FatalErr ¶
type FatalErr struct {
Err error
Status ExitStatus
}
FatalErr 致命错误,将导致 supervisor 终止
func AsFatalErr ¶
func AsFatalErr(err error, status ExitStatus) *FatalErr
AsFatalErr 将错误包装为 FatalErr
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
func (*Manager) AddWithConfig ¶
func (m *Manager) AddWithConfig(srv Service, config ServiceConfig) error
AddWithConfig 添加服务并指定配置
func (*Manager) GetServiceInfo ¶
func (m *Manager) GetServiceInfo(name string) (*ServiceInfo, error)
GetServiceInfo returns the status info of a specific service
func (*Manager) GetServicesInfo ¶
func (m *Manager) GetServicesInfo() []*ServiceInfo
GetServicesInfo returns the status info of all services
func (*Manager) RemoveServices ¶
func (*Manager) ResetService ¶
ResetService 重置失败的服务,清除所有重启计数
func (*Manager) RestartService ¶
func (*Manager) RestartServices ¶
func (*Manager) ServeBackground ¶
func (*Manager) StartService ¶
StartService 启动已暂停的服务
func (*Manager) StopService ¶
StopService 暂停服务,但保留在 services map 中
type Metric ¶
type Metric struct {
Name string `json:"name"` // 服务名称
Status ServiceStatus `json:"status"` // 当前状态
StartCount uint32 `json:"start_count"` // 启动次数
ErrorCount uint32 `json:"error_count"` // 错误次数
SuccessCount uint32 `json:"success_count"` // 成功退出次数
ConsecFailures uint32 `json:"consec_failures"` // 连续失败次数
LastError string `json:"last_error"` // 最后一次错误信息
LastErrorTime time.Time `json:"last_error_time"` // 最后一次错误时间
LastStartTime time.Time `json:"last_start_time"` // 最后一次启动时间
LastStopTime time.Time `json:"last_stop_time"` // 最后一次停止时间
CurrentUptime time.Duration `json:"current_uptime"` // 当前运行时长
TotalUptime time.Duration `json:"total_uptime"` // 总运行时长
AverageUptime time.Duration `json:"average_uptime"` // 平均运行时长
CreatedAt time.Time `json:"created_at"` // 服务创建时间
CurrentDelay time.Duration `json:"current_delay"` // 当前重启延迟
RestartsInWindow uint32 `json:"restarts_in_window"` // 窗口期内重启次数
}
Metric 服务指标
type Option ¶
type Option func(*ServiceConfig)
Option 配置选项
func WithBackoff ¶
WithBackoff 设置退避策略
func WithRestartPolicy ¶
func WithRestartPolicy(policy RestartPolicy) Option
WithRestartPolicy 设置重启策略
type RestartPolicy ¶
type RestartPolicy int
RestartPolicy 重启策略
const ( // RestartAlways 总是重启(除非手动停止) RestartAlways RestartPolicy = iota // RestartOnFailure 仅在失败时重启 RestartOnFailure // RestartNever 从不自动重启 RestartNever )
type Service ¶
type Service interface {
Name() string
Error() error
String() string
Serve(ctx context.Context) error
Metric() *Metric
}
Service 服务接口
type ServiceConfig ¶
type ServiceConfig struct {
// RestartPolicy 重启策略
RestartPolicy RestartPolicy
// MaxRestarts 最大重启次数,0 表示无限制
MaxRestarts int
// RestartDelay 初始重启延迟
RestartDelay time.Duration
// MaxRestartDelay 最大重启延迟(用于指数退避)
MaxRestartDelay time.Duration
// RestartWindow 重启计数窗口期,在此期间内的重启会被计数
// 如果服务运行超过此时间后崩溃,重启计数会重置
RestartWindow time.Duration
// MaxRestartsInWindow 窗口期内最大重启次数,超过则标记为 failed
MaxRestartsInWindow int
// BackoffMultiplier 退避乘数,默认 2.0
BackoffMultiplier float64
// AutoStart 是否自动启动,默认 true
AutoStart bool
}
ServiceConfig 服务配置
type ServiceInfo ¶
type ServiceInfo struct {
*Metric
Stopped bool `json:"stopped"`
Failed bool `json:"failed"`
RestartCount int `json:"restart_count"`
ConsecFailures int `json:"consec_failures"`
WindowRestarts int `json:"window_restarts"`
CurrentDelay time.Duration `json:"current_delay_ns"`
CurrentDelayStr string `json:"current_delay"`
WindowStart time.Time `json:"window_start"`
LastServiceStart time.Time `json:"last_service_start"`
}
ServiceInfo 包含服务指标和运行时状态
type ServiceStatus ¶
type ServiceStatus string
ServiceStatus 服务状态
const ( StatusIdle ServiceStatus = "idle" // 空闲,未启动 StatusRunning ServiceStatus = "running" // 运行中 StatusStopped ServiceStatus = "stopped" // 已停止(手动) StatusError ServiceStatus = "error" // 错误状态 StatusCrashing ServiceStatus = "crashing" // 崩溃循环中 StatusFailed ServiceStatus = "failed" // 已失败(达到重启上限) )
Click to show internal directories.
Click to hide internal directories.