|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "os/exec" |
| 8 | + "path/filepath" |
| 9 | + "strconv" |
| 10 | + "strings" |
| 11 | + "syscall" |
| 12 | + "time" |
| 13 | + |
| 14 | + "github.com/orange-juzipi/cert-deploy/internal/updater" |
| 15 | + "github.com/spf13/cobra" |
| 16 | +) |
| 17 | + |
| 18 | +// CreateDaemonCmd 创建守护进程命令 |
| 19 | +func CreateDaemonCmd() *cobra.Command { |
| 20 | + return &cobra.Command{ |
| 21 | + Use: "daemon", |
| 22 | + Short: "启动守护进程(后台运行)", |
| 23 | + Long: "在后台启动证书部署守护进程,进程崩溃或更新后将自动重启", |
| 24 | + Run: func(cmd *cobra.Command, args []string) { |
| 25 | + // 检查是否已经在运行,如果是则先停止 |
| 26 | + if IsRunning() { |
| 27 | + fmt.Println("守护进程已在运行,正在重启...") |
| 28 | + if err := StopDaemon(); err != nil { |
| 29 | + fmt.Printf("停止失败: %v\n", err) |
| 30 | + os.Exit(1) |
| 31 | + } |
| 32 | + time.Sleep(2 * time.Second) |
| 33 | + } |
| 34 | + |
| 35 | + execPath, err := os.Executable() |
| 36 | + if err != nil { |
| 37 | + fmt.Printf("获取可执行文件路径失败: %v\n", err) |
| 38 | + os.Exit(1) |
| 39 | + } |
| 40 | + |
| 41 | + supervisorCmd := exec.Command(execPath, "_supervisor", "-c", ConfigFile) |
| 42 | + if err := supervisorCmd.Start(); err != nil { |
| 43 | + fmt.Printf("启动失败: %v\n", err) |
| 44 | + os.Exit(1) |
| 45 | + } |
| 46 | + |
| 47 | + time.Sleep(500 * time.Millisecond) |
| 48 | + |
| 49 | + if !IsRunning() { |
| 50 | + fmt.Println("启动失败") |
| 51 | + os.Exit(1) |
| 52 | + } |
| 53 | + |
| 54 | + fmt.Println("守护进程已启动") |
| 55 | + |
| 56 | + // 异步检查更新 |
| 57 | + go func() { |
| 58 | + time.Sleep(1 * time.Second) |
| 59 | + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 60 | + defer cancel() |
| 61 | + |
| 62 | + info, err := updater.CheckUpdate(ctx) |
| 63 | + if err == nil && info.HasUpdate { |
| 64 | + fmt.Printf("\n发现新版本: %s -> %s\n", info.CurrentVersion, info.LatestVersion) |
| 65 | + fmt.Println("执行 './cert-deploy update' 进行更新") |
| 66 | + } |
| 67 | + }() |
| 68 | + |
| 69 | + time.Sleep(100 * time.Millisecond) |
| 70 | + }, |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +// createSupervisorCmd 创建 supervisor 命令(内部使用) |
| 75 | +func createSupervisorCmd() *cobra.Command { |
| 76 | + return &cobra.Command{ |
| 77 | + Use: "_supervisor", |
| 78 | + Hidden: true, |
| 79 | + Short: "运行守护进程监控器(内部命令)", |
| 80 | + Run: func(cmd *cobra.Command, args []string) { |
| 81 | + runSupervisor() |
| 82 | + }, |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +// runSupervisor 运行守护进程监控器 |
| 87 | +func runSupervisor() { |
| 88 | + execPath, err := os.Executable() |
| 89 | + if err != nil { |
| 90 | + fmt.Printf("获取可执行文件路径失败: %v\n", err) |
| 91 | + return |
| 92 | + } |
| 93 | + |
| 94 | + supervisorLogFile, err := os.OpenFile(GetLogFile(), os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644) |
| 95 | + if err != nil { |
| 96 | + fmt.Printf("打开日志文件失败: %v\n", err) |
| 97 | + return |
| 98 | + } |
| 99 | + defer supervisorLogFile.Close() |
| 100 | + |
| 101 | + logSupervisor := func(format string, args ...interface{}) { |
| 102 | + msg := fmt.Sprintf("[Supervisor %s] ", time.Now().Format("15:04:05")) |
| 103 | + msg += fmt.Sprintf(format, args...) |
| 104 | + msg += "\n" |
| 105 | + supervisorLogFile.WriteString(msg) |
| 106 | + supervisorLogFile.Sync() |
| 107 | + } |
| 108 | + |
| 109 | + logSupervisor("已启动") |
| 110 | + |
| 111 | + restartDelay := 1 * time.Second |
| 112 | + maxRestartDelay := 30 * time.Second |
| 113 | + consecutiveFailures := 0 |
| 114 | + |
| 115 | + for { |
| 116 | + if shouldStopSupervisor() { |
| 117 | + logSupervisor("停止") |
| 118 | + return |
| 119 | + } |
| 120 | + |
| 121 | + cmd := exec.Command(execPath, "start", "-c", ConfigFile) |
| 122 | + |
| 123 | + logFile, err := os.OpenFile(GetLogFile(), os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644) |
| 124 | + if err != nil { |
| 125 | + time.Sleep(restartDelay) |
| 126 | + continue |
| 127 | + } |
| 128 | + |
| 129 | + cmd.Stdout = logFile |
| 130 | + cmd.Stderr = logFile |
| 131 | + |
| 132 | + startTime := time.Now() |
| 133 | + if err := cmd.Start(); err != nil { |
| 134 | + logFile.Close() |
| 135 | + consecutiveFailures++ |
| 136 | + time.Sleep(restartDelay) |
| 137 | + continue |
| 138 | + } |
| 139 | + |
| 140 | + pidFile := GetPIDFile() |
| 141 | + if err := os.WriteFile(pidFile, []byte(strconv.Itoa(cmd.Process.Pid)), 0644); err != nil { |
| 142 | + cmd.Process.Kill() |
| 143 | + logFile.Close() |
| 144 | + time.Sleep(restartDelay) |
| 145 | + continue |
| 146 | + } |
| 147 | + |
| 148 | + err = cmd.Wait() |
| 149 | + logFile.Close() |
| 150 | + uptime := time.Since(startTime) |
| 151 | + |
| 152 | + if err != nil { |
| 153 | + consecutiveFailures++ |
| 154 | + logSupervisor("异常退出: %v", err) |
| 155 | + |
| 156 | + if uptime < 10*time.Second { |
| 157 | + restartDelay = time.Duration(consecutiveFailures) * time.Second |
| 158 | + if restartDelay > maxRestartDelay { |
| 159 | + restartDelay = maxRestartDelay |
| 160 | + } |
| 161 | + logSupervisor("等待 %v 后重启", restartDelay) |
| 162 | + time.Sleep(restartDelay) |
| 163 | + } else { |
| 164 | + consecutiveFailures = 0 |
| 165 | + } |
| 166 | + } else { |
| 167 | + // 检查更新标记(程序同级目录) |
| 168 | + execDir := filepath.Dir(execPath) |
| 169 | + updateMarker := filepath.Join(execDir, ".cert-deploy-updated") |
| 170 | + if _, err := os.Stat(updateMarker); err == nil { |
| 171 | + logSupervisor("应用更新,重启中...") |
| 172 | + os.Remove(updateMarker) |
| 173 | + consecutiveFailures = 0 |
| 174 | + time.Sleep(1 * time.Second) |
| 175 | + } else { |
| 176 | + logSupervisor("正常退出") |
| 177 | + os.Remove(pidFile) |
| 178 | + return |
| 179 | + } |
| 180 | + } |
| 181 | + } |
| 182 | +} |
| 183 | + |
| 184 | +// shouldStopSupervisor 检查是否应该停止监控器 |
| 185 | +func shouldStopSupervisor() bool { |
| 186 | + homeDir, _ := os.UserHomeDir() |
| 187 | + stopMarker := filepath.Join(homeDir, ".cert-deploy-stop") |
| 188 | + if _, err := os.Stat(stopMarker); err == nil { |
| 189 | + os.Remove(stopMarker) |
| 190 | + return true |
| 191 | + } |
| 192 | + return false |
| 193 | +} |
| 194 | + |
| 195 | +// StopDaemon 停止守护进程 |
| 196 | +func StopDaemon() error { |
| 197 | + homeDir, _ := os.UserHomeDir() |
| 198 | + stopMarker := filepath.Join(homeDir, ".cert-deploy-stop") |
| 199 | + os.WriteFile(stopMarker, []byte("stop"), 0644) |
| 200 | + |
| 201 | + pidFile := GetPIDFile() |
| 202 | + data, err := os.ReadFile(pidFile) |
| 203 | + if err != nil { |
| 204 | + return fmt.Errorf("读取PID文件失败: %w", err) |
| 205 | + } |
| 206 | + |
| 207 | + pidStr := strings.TrimSpace(string(data)) |
| 208 | + pid, err := strconv.Atoi(pidStr) |
| 209 | + if err != nil { |
| 210 | + return fmt.Errorf("无效的PID: %w", err) |
| 211 | + } |
| 212 | + |
| 213 | + process, err := os.FindProcess(pid) |
| 214 | + if err != nil { |
| 215 | + return fmt.Errorf("查找进程失败: %w", err) |
| 216 | + } |
| 217 | + |
| 218 | + if err := process.Signal(syscall.SIGTERM); err != nil { |
| 219 | + return fmt.Errorf("发送停止信号失败: %w", err) |
| 220 | + } |
| 221 | + |
| 222 | + for i := 0; i < 10; i++ { |
| 223 | + if !IsRunning() { |
| 224 | + break |
| 225 | + } |
| 226 | + time.Sleep(1 * time.Second) |
| 227 | + } |
| 228 | + |
| 229 | + if IsRunning() { |
| 230 | + if err := process.Signal(syscall.SIGKILL); err != nil { |
| 231 | + return fmt.Errorf("强制停止失败: %w", err) |
| 232 | + } |
| 233 | + } |
| 234 | + |
| 235 | + os.Remove(pidFile) |
| 236 | + os.Remove(stopMarker) |
| 237 | + |
| 238 | + return nil |
| 239 | +} |
0 commit comments