supervisor

package
v2.0.0-beta.14 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

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

func IsFatal

func IsFatal(err error) bool

IsFatal 判断是否为致命错误

func IsFatalErr

func IsFatalErr(err error) bool

IsFatalErr 判断是否是致命错误

func IsNoRestartErr

func IsNoRestartErr(err error) bool

IsNoRestartErr 判断是否是不需要重启的错误

func NoRestartErr

func NoRestartErr(err error) error

NoRestartErr 包装错误,使其不会触发自动重启

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

func (*FatalErr) Error

func (e *FatalErr) Error() string

func (*FatalErr) Is

func (*FatalErr) Is(target error) bool

func (*FatalErr) Unwrap

func (e *FatalErr) Unwrap() error

type Manager

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

func Default

func Default(lc lifecycle.Getter) *Manager

func NewManager

func NewManager(name string, lc lifecycle.Getter) *Manager

func (*Manager) Add

func (m *Manager) Add(srv Service, opts ...Option) error

func (*Manager) AddWithConfig

func (m *Manager) AddWithConfig(srv Service, config ServiceConfig) error

AddWithConfig 添加服务并指定配置

func (*Manager) Delete

func (m *Manager) Delete(name string) error

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

func (m *Manager) Has(name string) bool

func (*Manager) OnClose

func (m *Manager) OnClose(fn func())

func (*Manager) RemoveServices

func (m *Manager) RemoveServices() error

func (*Manager) ResetService

func (m *Manager) ResetService(name string) error

ResetService 重置失败的服务,清除所有重启计数

func (*Manager) RestartService

func (m *Manager) RestartService(name string) error

func (*Manager) RestartServices

func (m *Manager) RestartServices() error

func (*Manager) Run

func (m *Manager) Run(ctx context.Context) error

func (*Manager) Serve

func (m *Manager) Serve(ctx context.Context) error

func (*Manager) ServeBackground

func (m *Manager) ServeBackground(ctx context.Context) <-chan error

func (*Manager) Services

func (m *Manager) Services() []Service

func (*Manager) StartService

func (m *Manager) StartService(name string) error

StartService 启动已暂停的服务

func (*Manager) StopService

func (m *Manager) StopService(name string) error

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 WithAutoStart

func WithAutoStart(autoStart bool) Option

WithAutoStart 设置是否自动启动

func WithBackoff

func WithBackoff(maxDelay time.Duration, multiplier float64) Option

WithBackoff 设置退避策略

func WithMaxRestarts

func WithMaxRestarts(n int) Option

WithMaxRestarts 设置最大重启次数

func WithRestartDelay

func WithRestartDelay(d time.Duration) Option

WithRestartDelay 设置重启延迟

func WithRestartPolicy

func WithRestartPolicy(policy RestartPolicy) Option

WithRestartPolicy 设置重启策略

func WithRestartWindow

func WithRestartWindow(window time.Duration, maxRestarts int) Option

WithRestartWindow 设置重启窗口

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 服务接口

func NewService

func NewService(name string, fn func(ctx context.Context) error) 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 服务配置

func DefaultServiceConfig

func DefaultServiceConfig() ServiceConfig

DefaultServiceConfig 默认服务配置

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"   // 已失败(达到重启上限)
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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