first commit
This commit is contained in:
27
cmii-uav-watchdog/build_and_run_watchdog.ps1
Normal file
27
cmii-uav-watchdog/build_and_run_watchdog.ps1
Normal file
@@ -0,0 +1,27 @@
|
||||
|
||||
|
||||
try {
|
||||
$ErrorActionPreference = "Stop"
|
||||
|
||||
Write-Host "1. Building binary exec file..." -ForegroundColor Cyan
|
||||
& "C:\Users\wdd\go\bin\gox.exe" -osarch="linux/amd64" -output "build/cmii-watchdog_linux_amd64"
|
||||
|
||||
# 执行远程ssh命令 ssh root@192.168.35.70 "rm /root/cmii-watchdog"
|
||||
Write-Host "2. Cleaning old binary file..." -ForegroundColor Yellow
|
||||
ssh root@192.168.35.70 "rm -f /root/wdd/cmii-watchdog/*"
|
||||
|
||||
Write-Host "3. Uploading binary exec..." -ForegroundColor Green
|
||||
scp build/cmii-watchdog_linux_amd64 root@192.168.35.70:/root/wdd/cmii-watchdog/
|
||||
scp java/* root@192.168.35.70:/root/wdd/cmii-watchdog/
|
||||
|
||||
Write-Host "4. Exec the command ..." -ForegroundColor Blue
|
||||
Write-Host ""
|
||||
Write-Host ""
|
||||
ssh root@192.168.35.70 "cd /root/wdd/cmii-watchdog/ && chmod +x cmii-watchdog_linux_amd64 && sudo bash build_cmii_watchdog.sh"
|
||||
Write-Host ""
|
||||
Write-Host ""
|
||||
|
||||
} catch {
|
||||
Write-Host "操作失败: $_" -ForegroundColor Red
|
||||
exit 1
|
||||
}
|
||||
9
cmii-uav-watchdog/cmii-uav-watchdog.iml
Normal file
9
cmii-uav-watchdog/cmii-uav-watchdog.iml
Normal file
@@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="WEB_MODULE" version="4">
|
||||
<component name="Go" enabled="true" />
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$/cmii-uav-watchdog" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
||||
3
cmii-uav-watchdog/go.mod
Normal file
3
cmii-uav-watchdog/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module cmii-uav-watchdog
|
||||
|
||||
go 1.23
|
||||
121
cmii-uav-watchdog/main.go
Normal file
121
cmii-uav-watchdog/main.go
Normal file
@@ -0,0 +1,121 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"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)
|
||||
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()
|
||||
}
|
||||
}()
|
||||
|
||||
// 主循环
|
||||
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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user