mirror of
https://github.com/fankes/komari-agent.git
synced 2025-10-18 18:49:23 +08:00
fix: 自动更新后不会自动重新启动
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1,4 +1,5 @@
|
||||
agent.json
|
||||
.vscode/
|
||||
komari-agent.exe
|
||||
komari-agent
|
||||
komari-agent
|
||||
.komari-agent.exe.old
|
||||
|
@@ -14,6 +14,7 @@ type LocalConfig struct {
|
||||
ReconnectInterval int `json:"reconnectInterval"`
|
||||
IgnoreUnsafeCert bool `json:"ignoreUnsafeCert"`
|
||||
Interval float64 `json:"interval"`
|
||||
AutoUpdate bool `json:"autoUpdate"`
|
||||
}
|
||||
|
||||
func LoadConfig() (LocalConfig, error) {
|
||||
@@ -27,6 +28,7 @@ func LoadConfig() (LocalConfig, error) {
|
||||
reconnectInterval int
|
||||
ignoreUnsafeCert bool
|
||||
interval float64
|
||||
autoUpdate bool
|
||||
)
|
||||
|
||||
flag.StringVar(&endpoint, "e", "", "The endpoint URL")
|
||||
@@ -37,6 +39,7 @@ func LoadConfig() (LocalConfig, error) {
|
||||
flag.IntVar(&reconnectInterval, "reconnectInterval", 5, "Reconnect interval in seconds")
|
||||
flag.Float64Var(&interval, "interval", 1.1, "Interval in seconds for sending data to the server")
|
||||
flag.BoolVar(&ignoreUnsafeCert, "ignoreUnsafeCert", false, "Ignore unsafe certificate errors")
|
||||
flag.BoolVar(&autoUpdate, "autoUpdate", false, "Enable or disable auto update (default: false)")
|
||||
flag.Parse()
|
||||
|
||||
// Ensure -c cannot coexist with other flags
|
||||
@@ -68,5 +71,6 @@ func LoadConfig() (LocalConfig, error) {
|
||||
ReconnectInterval: reconnectInterval,
|
||||
IgnoreUnsafeCert: ignoreUnsafeCert,
|
||||
Interval: interval,
|
||||
AutoUpdate: autoUpdate,
|
||||
}, nil
|
||||
}
|
||||
|
25
main.go
25
main.go
@@ -17,8 +17,8 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
CurrentVersion string
|
||||
repo = "komari-monitor/komari-agent"
|
||||
CurrentVersion string = "0.0.1"
|
||||
repo = "komari-monitor/komari-agent"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -30,10 +30,7 @@ func main() {
|
||||
if localConfig.IgnoreUnsafeCert {
|
||||
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
|
||||
}
|
||||
err = uploadBasicInfo(localConfig.Endpoint, localConfig.Token)
|
||||
if err != nil {
|
||||
log.Fatalln("Failed to upload basic info:", err)
|
||||
}
|
||||
|
||||
go func() {
|
||||
err = uploadBasicInfo(localConfig.Endpoint, localConfig.Token)
|
||||
ticker := time.NewTicker(time.Duration(time.Minute * 15))
|
||||
@@ -58,13 +55,15 @@ func main() {
|
||||
ticker := time.NewTicker(time.Duration(localConfig.Interval * float64(time.Second)))
|
||||
defer ticker.Stop()
|
||||
|
||||
// Check for updates
|
||||
go func() {
|
||||
ticker_ := time.NewTicker(time.Duration(6) * time.Hour)
|
||||
for range ticker_.C {
|
||||
if localConfig.AutoUpdate {
|
||||
go func() {
|
||||
ticker_ := time.NewTicker(time.Duration(6) * time.Hour)
|
||||
update_komari()
|
||||
}
|
||||
}()
|
||||
for range ticker_.C {
|
||||
update_komari()
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
for range ticker.C {
|
||||
// If no connection, attempt to connect
|
||||
@@ -113,7 +112,7 @@ func update_komari() {
|
||||
// 检查并更新
|
||||
err := updater.CheckAndUpdate()
|
||||
if err != nil {
|
||||
log.Fatalf("更新失败: %v", err)
|
||||
log.Fatalf("Update Failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -2,6 +2,8 @@ package update
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/blang/semver"
|
||||
"github.com/rhysd/go-github-selfupdate/selfupdate"
|
||||
@@ -21,6 +23,7 @@ func NewUpdater(currentVersion, repo string) *Updater {
|
||||
|
||||
// 检查更新并执行自动更新
|
||||
func (u *Updater) CheckAndUpdate() error {
|
||||
log.Println("Checking update...")
|
||||
// 解析当前版本
|
||||
currentSemVer, err := semver.Parse(u.CurrentVersion)
|
||||
if err != nil {
|
||||
@@ -45,8 +48,19 @@ func (u *Updater) CheckAndUpdate() error {
|
||||
fmt.Println("当前版本已是最新版本:", u.CurrentVersion)
|
||||
return nil
|
||||
}
|
||||
execPath, err := os.Executable()
|
||||
if err != nil {
|
||||
return fmt.Errorf("获取当前执行路径失败: %v", err)
|
||||
}
|
||||
|
||||
_, err = os.StartProcess(execPath, os.Args, &os.ProcAttr{
|
||||
Files: []*os.File{os.Stdin, os.Stdout, os.Stderr},
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("重新启动程序失败: %v", err)
|
||||
}
|
||||
fmt.Printf("成功更新到版本 %s\n", latest.Version)
|
||||
fmt.Printf("发布说明:\n%s\n", latest.ReleaseNotes)
|
||||
os.Exit(0)
|
||||
return nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user