refactor: use nginx -s reload instead of systemd
- doc: add comment
This commit is contained in:
@@ -12,19 +12,25 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
loadStateProperty = "LoadState"
|
||||
// loadStateProperty systemctl show 输出的加载状态属性名。
|
||||
loadStateProperty = "LoadState"
|
||||
// activeStateProperty systemctl show 输出的活动状态属性名。
|
||||
activeStateProperty = "ActiveState"
|
||||
subStateProperty = "SubState"
|
||||
loadedState = "loaded"
|
||||
notFoundState = "not-found"
|
||||
// subStateProperty systemctl show 输出的子状态属性名。
|
||||
subStateProperty = "SubState"
|
||||
// loadedState 表示单元已加载。
|
||||
loadedState = "loaded"
|
||||
// notFoundState 表示单元未找到。
|
||||
notFoundState = "not-found"
|
||||
)
|
||||
|
||||
// Systemctl invokes one exact systemctl executable directly, never through a shell.
|
||||
// Systemctl 直接调用一个精确的 systemctl 可执行文件,绝不经过 shell。
|
||||
type Systemctl struct {
|
||||
// executable systemctl 的绝对路径。
|
||||
executable string
|
||||
}
|
||||
|
||||
// NewSystemctl requires the absolute executable path supplied by local daemon configuration.
|
||||
// NewSystemctl 要求传入本地守护进程配置提供的可执行文件绝对路径。
|
||||
func NewSystemctl(executable string) (*Systemctl, error) {
|
||||
if !filepath.IsAbs(executable) {
|
||||
return nil, errors.New("systemctl executable path must be absolute")
|
||||
@@ -32,6 +38,8 @@ func NewSystemctl(executable string) (*Systemctl, error) {
|
||||
return &Systemctl{executable: executable}, nil
|
||||
}
|
||||
|
||||
// Inspect 查询指定单元的关键状态属性并返回其状态。
|
||||
// 未找到单元时错误包装 ErrUnitNotFound;加载状态异常时返回错误。
|
||||
func (s *Systemctl) Inspect(ctx context.Context, unitName string) (Unit, error) {
|
||||
if err := validateUnitName(unitName); err != nil {
|
||||
return Unit{}, err
|
||||
@@ -62,14 +70,17 @@ func (s *Systemctl) Inspect(ctx context.Context, unitName string) (Unit, error)
|
||||
return unit, nil
|
||||
}
|
||||
|
||||
// Start 启动指定单元。
|
||||
func (s *Systemctl) Start(ctx context.Context, unitName string) error {
|
||||
return s.changeState(ctx, "start", unitName)
|
||||
}
|
||||
|
||||
// Stop 停止指定单元。
|
||||
func (s *Systemctl) Stop(ctx context.Context, unitName string) error {
|
||||
return s.changeState(ctx, "stop", unitName)
|
||||
}
|
||||
|
||||
// changeState 通过 systemctl 执行给定动作(start 或 stop)。
|
||||
func (s *Systemctl) changeState(ctx context.Context, action, unitName string) error {
|
||||
if err := validateUnitName(unitName); err != nil {
|
||||
return err
|
||||
@@ -81,6 +92,8 @@ func (s *Systemctl) changeState(ctx context.Context, action, unitName string) er
|
||||
return nil
|
||||
}
|
||||
|
||||
// parseUnitProperties 解析 systemctl show 的输出,
|
||||
// 严格要求恰好包含三个预期属性且不重复、不多余。
|
||||
func parseUnitProperties(unitName string, output []byte) (Unit, error) {
|
||||
values := make(map[string]string, 3)
|
||||
scanner := bufio.NewScanner(bytes.NewReader(output))
|
||||
@@ -116,6 +129,7 @@ func parseUnitProperties(unitName string, output []byte) (Unit, error) {
|
||||
}, nil
|
||||
}
|
||||
|
||||
// validateUnitName 校验单元名称必须非空且不含首尾空白。
|
||||
func validateUnitName(unitName string) error {
|
||||
if unitName == "" || strings.TrimSpace(unitName) != unitName {
|
||||
return errors.New("exact systemd unit name is required")
|
||||
@@ -123,6 +137,7 @@ func validateUnitName(unitName string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// commandError 包装命令执行错误,并附带去除首尾空白的命令输出作为诊断细节。
|
||||
func commandError(action string, output []byte, err error) error {
|
||||
detail := strings.TrimSpace(string(output))
|
||||
if detail == "" {
|
||||
|
||||
Reference in New Issue
Block a user