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