更新项目配置,修改 Go 版本至 1.24,添加依赖项,重构主程序以加载配置和初始化服务,删除不再使用的远程目标配置文件。
This commit is contained in:
@@ -1,121 +1,39 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"cmii-uav-watchdog/config"
|
||||
"cmii-uav-watchdog/routes"
|
||||
"cmii-uav-watchdog/services"
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"os/signal"
|
||||
"sync"
|
||||
"syscall"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
businessProgramType = flag.String("business-program-type", "", "Type of business program (java or python)")
|
||||
businessProgramPath = flag.String("business-program-path", "", "Path to the business program file")
|
||||
stopRequested bool
|
||||
currentCmd *exec.Cmd
|
||||
mu sync.Mutex
|
||||
)
|
||||
|
||||
func startBusinessProcess(programType, programPath string) *exec.Cmd {
|
||||
var cmd *exec.Cmd
|
||||
switch programType {
|
||||
case "java":
|
||||
cmd = exec.Command("java", "-jar", programPath)
|
||||
case "python":
|
||||
cmd = exec.Command("python", programPath)
|
||||
default:
|
||||
log.Fatalf("Unsupported business program type: %s", programType)
|
||||
}
|
||||
return cmd
|
||||
}
|
||||
|
||||
// C:\Users\wdd\go\bin\gox.exe -osarch="linux/amd64" -output "build/watchdog"
|
||||
func main() {
|
||||
// 解析命令行参数
|
||||
flag.Parse()
|
||||
if *businessProgramType == "" || *businessProgramPath == "" {
|
||||
log.Fatal("Missing required flags: -business-program-type and -business-program-path must be specified")
|
||||
}
|
||||
|
||||
// 信号处理
|
||||
signalChan := make(chan os.Signal, 1)
|
||||
signal.Notify(signalChan, os.Interrupt, syscall.SIGTERM)
|
||||
|
||||
// 初始化授权服务
|
||||
authService := services.NewAuthService()
|
||||
|
||||
// 启动授权码检测定时任务
|
||||
go func() {
|
||||
for sig := range signalChan {
|
||||
log.Printf("Received signal: %v", sig)
|
||||
mu.Lock()
|
||||
stopRequested = true
|
||||
if currentCmd != nil && currentCmd.Process != nil {
|
||||
// 发送 SIGTERM 给业务进程
|
||||
if err := currentCmd.Process.Signal(syscall.SIGTERM); err != nil {
|
||||
log.Printf("Failed to send SIGTERM to process: %v", err)
|
||||
}
|
||||
// 等待 10 秒后强制杀死进程
|
||||
time.AfterFunc(10*time.Second, func() {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
if currentCmd != nil && currentCmd.Process != nil {
|
||||
log.Println("Graceful shutdown timeout, sending SIGKILL")
|
||||
currentCmd.Process.Kill()
|
||||
}
|
||||
})
|
||||
}
|
||||
mu.Unlock()
|
||||
ticker := time.NewTicker(1 * time.Hour)
|
||||
defer ticker.Stop()
|
||||
|
||||
for range ticker.C {
|
||||
authService.VerifyAuthorizationTime()
|
||||
}
|
||||
}()
|
||||
|
||||
// 主循环
|
||||
for {
|
||||
mu.Lock()
|
||||
if stopRequested {
|
||||
mu.Unlock()
|
||||
log.Println("Shutting down due to stop request")
|
||||
os.Exit(0)
|
||||
}
|
||||
mu.Unlock()
|
||||
|
||||
cmd := startBusinessProcess(*businessProgramType, *businessProgramPath)
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
|
||||
// 启动业务进程
|
||||
if err := cmd.Start(); err != nil {
|
||||
log.Printf("Failed to start business process: %v", err)
|
||||
time.Sleep(5 * time.Second)
|
||||
continue
|
||||
}
|
||||
|
||||
mu.Lock()
|
||||
currentCmd = cmd
|
||||
mu.Unlock()
|
||||
|
||||
// 等待业务进程退出
|
||||
err := cmd.Wait()
|
||||
mu.Lock()
|
||||
currentCmd = nil
|
||||
mu.Unlock()
|
||||
|
||||
// 程序异常退出,或者重启
|
||||
// 定位分析问题
|
||||
if err != nil {
|
||||
log.Printf("Business process exited with error: %v", err)
|
||||
} else {
|
||||
log.Println("Business process exited normally")
|
||||
}
|
||||
|
||||
mu.Lock()
|
||||
if stopRequested {
|
||||
mu.Unlock()
|
||||
log.Println("Shutting down due to stop request")
|
||||
os.Exit(0)
|
||||
}
|
||||
mu.Unlock()
|
||||
|
||||
// 等待 5 秒后重启
|
||||
log.Println("Restarting business process in 5 seconds...")
|
||||
time.Sleep(5 * time.Second)
|
||||
|
||||
// 初始化路由
|
||||
r := routes.SetupRouter()
|
||||
|
||||
// 启动服务
|
||||
port := config.GetConfig().Server.Port
|
||||
if port == "" {
|
||||
port = "8080"
|
||||
}
|
||||
|
||||
log.Printf("服务启动在 :%s", port)
|
||||
if err := r.Run(":" + port); err != nil {
|
||||
log.Fatalf("服务启动失败: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user