|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "os/exec" |
| 8 | + "path/filepath" |
| 9 | + "runtime" |
| 10 | + "strconv" |
| 11 | + "strings" |
| 12 | + "time" |
| 13 | +) |
| 14 | + |
| 15 | +// pidFileRecord identifies the supervisor process that owns the daemon PID file. |
| 16 | +type pidFileRecord struct { |
| 17 | + PID int `json:"pid"` // PID 是 supervisor 进程号。 |
| 18 | + Executable string `json:"executable"` // Executable 是启动 supervisor 的可执行文件路径。 |
| 19 | + StartedAt time.Time `json:"startedAt"` // StartedAt 是 supervisor 启动时间。 |
| 20 | +} |
| 21 | + |
| 22 | +// writePIDRecord atomically writes a structured daemon PID record. |
| 23 | +func writePIDRecord(path string, record pidFileRecord) error { |
| 24 | + data, err := json.Marshal(record) |
| 25 | + if err != nil { |
| 26 | + return err |
| 27 | + } |
| 28 | + temp, err := os.CreateTemp(filepath.Dir(path), ".anssl-pid-*") |
| 29 | + if err != nil { |
| 30 | + return err |
| 31 | + } |
| 32 | + tempPath := temp.Name() |
| 33 | + defer func() { |
| 34 | + _ = temp.Close() |
| 35 | + _ = os.Remove(tempPath) |
| 36 | + }() |
| 37 | + if err := temp.Chmod(0600); err != nil { |
| 38 | + return err |
| 39 | + } |
| 40 | + if _, err := temp.Write(append(data, '\n')); err != nil { |
| 41 | + return err |
| 42 | + } |
| 43 | + if err := temp.Sync(); err != nil { |
| 44 | + return err |
| 45 | + } |
| 46 | + if err := temp.Close(); err != nil { |
| 47 | + return err |
| 48 | + } |
| 49 | + return os.Rename(tempPath, path) |
| 50 | +} |
| 51 | + |
| 52 | +// readPIDRecord reads structured PID files and accepts legacy numeric files during upgrade. |
| 53 | +func readPIDRecord(path string) (pidFileRecord, error) { |
| 54 | + data, err := os.ReadFile(path) |
| 55 | + if err != nil { |
| 56 | + return pidFileRecord{}, err |
| 57 | + } |
| 58 | + var record pidFileRecord |
| 59 | + if json.Unmarshal(data, &record) == nil && record.PID > 0 { |
| 60 | + return record, nil |
| 61 | + } |
| 62 | + pid, err := strconv.Atoi(strings.TrimSpace(string(data))) |
| 63 | + if err != nil || pid <= 0 { |
| 64 | + return pidFileRecord{}, fmt.Errorf("无效的 PID 文件") |
| 65 | + } |
| 66 | + return pidFileRecord{PID: pid}, nil |
| 67 | +} |
| 68 | + |
| 69 | +// supervisorProcessMatches verifies that a PID belongs to an anssl supervisor process. |
| 70 | +func supervisorProcessMatches(record pidFileRecord) bool { |
| 71 | + if record.PID <= 0 { |
| 72 | + return false |
| 73 | + } |
| 74 | + commandLine, err := processCommandLine(record.PID) |
| 75 | + if err != nil || !strings.Contains(commandLine, "_supervisor") { |
| 76 | + return false |
| 77 | + } |
| 78 | + if record.Executable == "" { |
| 79 | + return true |
| 80 | + } |
| 81 | + return strings.Contains(commandLine, filepath.Base(record.Executable)) |
| 82 | +} |
| 83 | + |
| 84 | +// processCommandLine returns a process command line using the native inspection tool. |
| 85 | +func processCommandLine(pid int) (string, error) { |
| 86 | + if runtime.GOOS == "windows" { |
| 87 | + filter := fmt.Sprintf("(Get-CimInstance Win32_Process -Filter \"ProcessId = %d\").CommandLine", pid) |
| 88 | + output, err := exec.Command("powershell", "-NoProfile", "-NonInteractive", "-Command", filter).CombinedOutput() |
| 89 | + return strings.TrimSpace(string(output)), err |
| 90 | + } |
| 91 | + psPath := "/bin/ps" |
| 92 | + if _, err := os.Stat(psPath); err != nil { |
| 93 | + psPath = "/usr/bin/ps" |
| 94 | + } |
| 95 | + output, err := exec.Command(psPath, "-p", strconv.Itoa(pid), "-o", "command=").CombinedOutput() |
| 96 | + return strings.TrimSpace(string(output)), err |
| 97 | +} |
| 98 | + |
| 99 | +// acquireDaemonStartLock prevents concurrent daemon and restart commands from spawning supervisors. |
| 100 | +func acquireDaemonStartLock() (func(), error) { |
| 101 | + path := GetPIDFile() + ".lock" |
| 102 | + for attempt := 0; attempt < 2; attempt++ { |
| 103 | + file, err := os.OpenFile(path, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0600) |
| 104 | + if err == nil { |
| 105 | + if _, err := fmt.Fprintf(file, "%d\n", os.Getpid()); err != nil { |
| 106 | + _ = file.Close() |
| 107 | + _ = os.Remove(path) |
| 108 | + return nil, fmt.Errorf("写入守护进程启动锁失败: %w", err) |
| 109 | + } |
| 110 | + if err := file.Close(); err != nil { |
| 111 | + _ = os.Remove(path) |
| 112 | + return nil, fmt.Errorf("关闭守护进程启动锁失败: %w", err) |
| 113 | + } |
| 114 | + return func() { _ = os.Remove(path) }, nil |
| 115 | + } |
| 116 | + if !os.IsExist(err) { |
| 117 | + return nil, fmt.Errorf("创建守护进程启动锁失败: %w", err) |
| 118 | + } |
| 119 | + data, readErr := os.ReadFile(path) |
| 120 | + ownerPID, parseErr := strconv.Atoi(strings.TrimSpace(string(data))) |
| 121 | + if readErr == nil && parseErr == nil { |
| 122 | + if commandLine, inspectErr := processCommandLine(ownerPID); inspectErr == nil && commandLine != "" { |
| 123 | + return nil, fmt.Errorf("已有另一个启动或重启操作正在进行") |
| 124 | + } |
| 125 | + } |
| 126 | + if removeErr := os.Remove(path); removeErr != nil && !os.IsNotExist(removeErr) { |
| 127 | + return nil, fmt.Errorf("清理失效启动锁失败: %w", removeErr) |
| 128 | + } |
| 129 | + } |
| 130 | + return nil, fmt.Errorf("创建守护进程启动锁失败") |
| 131 | +} |
0 commit comments