diff --git a/cmd/kd.go b/cmd/kd.go index 2cf24e4..f8d7825 100644 --- a/cmd/kd.go +++ b/cmd/kd.go @@ -24,7 +24,7 @@ import ( "go.uber.org/zap" ) -var VERSION = "v0.0.5" +var VERSION = "v0.0.6" func showPrompt() { exename, err := pkg.GetExecutableBasename() @@ -174,7 +174,7 @@ func flagEditConfig(*cli.Context, bool) error { } func flagStatus(*cli.Context, bool) error { - di := daemon.GetDaemonInfo() + di, _ := daemon.GetDaemonInfo() d.EchoRun("运行和相关配置信息如下:") fmt.Printf(" Daemon端口:%s\n", di.Port) fmt.Printf(" Daemon PID:%d\n", di.PID) @@ -284,9 +284,12 @@ func main() { } } - if cfg.FileExists && cfg.ModTime > daemon.GetDaemonInfo().StartTime { - d.EchoWarn("检测到配置文件发生修改,正在重启守护进程") - flagRestart(cCtx, true) + if cfg.FileExists { + di, err := daemon.GetDaemonInfo() + if err == nil && cfg.ModTime > di.StartTime { + d.EchoWarn("检测到配置文件发生修改,正在重启守护进程") + flagRestart(cCtx, true) + } } if cCtx.String("theme") != "" { diff --git a/config/config.go b/config/config.go index a3ad38e..cc1bdc7 100644 --- a/config/config.go +++ b/config/config.go @@ -32,11 +32,11 @@ type Config struct { PagerCommand string `toml:"pager_command"` AutoClear bool `default:"false" toml:"auto_clear"` // MaxCached uint `default:"10000" toml:"max_cached"` - EnglishOnly bool `default:"false" toml:"english_only"` - Theme string `default:"temp" toml:"theme"` - HTTPProxy string `toml:"http_proxy"` - ClearScreen bool `toml:"clear_screen" default:"false"` - FreqAlert bool `toml:"freq_alert" default:"false"` + EnglishOnly bool `default:"false" toml:"english_only"` + Theme string `default:"temp" toml:"theme"` + HTTPProxy string `toml:"http_proxy"` + ClearScreen bool `toml:"clear_screen" default:"false"` + FreqAlert bool `toml:"freq_alert" default:"false"` Logging LoggerConfig `toml:"logging"` @@ -107,7 +107,7 @@ func InitConfig() error { err = Cfg.CheckAndApply() if err != nil { fmt.Println(err) - os.Exit(1) + os.Exit(1) } return err } diff --git a/internal/client.go b/internal/client.go index cd0385e..05b22a8 100644 --- a/internal/client.go +++ b/internal/client.go @@ -28,8 +28,8 @@ func ensureDaemon(running chan bool) { } else { var warn string // recorded daemon info - recDi := daemon.GetDaemonInfo() - if run.Info.Version != recDi.Version { + recDi, err := daemon.GetDaemonInfo() + if err == nil && run.Info.Version != recDi.Version { warn = fmt.Sprintf("正在运行的守护程序版本(%s)与当前程序(%s)不一致", recDi.Version, run.Info.Version) } else if daemonExepath, _ := p.Exe(); run.Info.ExePath != daemonExepath { warn = fmt.Sprintf("正在运行的守护程序(%s)与当前程序(%s)文件路径不一致", daemonExepath, run.Info.ExePath) diff --git a/internal/daemon/cron.go b/internal/daemon/cron.go index 0b8df52..4a14e01 100644 --- a/internal/daemon/cron.go +++ b/internal/daemon/cron.go @@ -11,8 +11,10 @@ import ( "time" "github.com/Karmenzind/kd/internal/cache" + "github.com/Karmenzind/kd/internal/run" "github.com/Karmenzind/kd/internal/update" "github.com/Karmenzind/kd/pkg" + d "github.com/Karmenzind/kd/pkg/decorate" _ "github.com/mattn/go-sqlite3" "go.uber.org/zap" ) @@ -21,6 +23,36 @@ func InitCron() { go cronCheckUpdate() go cronUpdateDataZip() go cronDeleteSpam() + go cronEnsureDaemonJsonFile() +} + +func cronEnsureDaemonJsonFile() { + ticker := time.NewTicker(5 * time.Minute) + for { + <-ticker.C + di, err := GetDaemonInfoFromFile() + // TODO 检查信息与当前是否匹配 + var needUpdate bool + if err != nil { + zap.S().Warnf("Failed to get daemon info from file: %s", err) + needUpdate = true + } else { + ri := run.Info + if !(ri.StartTime == di.StartTime && + ri.PID == di.PID && + ri.Port == di.Port && + ri.ExeName == di.ExeName && + ri.ExePath == di.ExePath && + ri.Version == di.Version) { + zap.S().Warn("DaemonInfo from json is different from current run.Info") + needUpdate = true + } + } + if needUpdate { + run.Info.SaveToFile(GetDaemonInfoPath()) + d.EchoRun("Update daemon.json") + } + } } func cronDeleteSpam() { @@ -44,18 +76,18 @@ func cronDeleteSpam() { func cronCheckUpdate() { ticker := time.NewTicker(3600 * 12 * time.Second) for { - // TODO (k): <2024-01-01> 改成检查文件Stat来判断时长 + // TODO (k): <2024-01-01> 改成检查文件Stat来判断时长 <-ticker.C - for i := 0; i < 3; i++ { - tag, err := update.GetLatestTag() - if err == nil { - fmt.Println("最新Tag", tag) - break - } - zap.S().Warnf("Failed to get latest tag: %s", err) - time.Sleep(5 * time.Second) - } + for i := 0; i < 3; i++ { + tag, err := update.GetLatestTag() + if err == nil { + fmt.Println("最新Tag", tag) + break + } + zap.S().Warnf("Failed to get latest tag: %s", err) + time.Sleep(5 * time.Second) + } } } diff --git a/internal/daemon/process.go b/internal/daemon/process.go index 3c81129..7a97c36 100644 --- a/internal/daemon/process.go +++ b/internal/daemon/process.go @@ -8,8 +8,8 @@ import ( "strings" "time" - "github.com/Karmenzind/kd/internal/cache" "github.com/Karmenzind/kd/internal/model" + "github.com/Karmenzind/kd/internal/run" "github.com/Karmenzind/kd/pkg" d "github.com/Karmenzind/kd/pkg/decorate" "github.com/Karmenzind/kd/pkg/proc" @@ -29,22 +29,40 @@ var DaemonInfo = &model.RunInfo{} // func RecordRunInfo(port string) { // run.Info.Port = port -// err := pkg.SaveJson(filepath.Join(run.CACHE_RUN_PATH, "daemon.json"), run.Info) -// if err == nil { -// zap.S().Infof("Recorded running information of daemon %+v", DaemonInfo) -// } else { -// zap.S().Warnf("Failed to record running info of daemon %+v", err) -// } -// } +// err := pkg.SaveJson(filepath.Join(run.CACHE_RUN_PATH, "daemon.json"), run.Info) +// if err == nil { +// zap.S().Infof("Recorded running information of daemon %+v", DaemonInfo) +// } else { +// zap.S().Warnf("Failed to record running info of daemon %+v", err) +// } +// } + +func GetDaemonInfoPath() string { + return filepath.Join(run.CACHE_RUN_PATH, "daemon.json") +} -func GetDaemonInfo() *model.RunInfo { +func GetDaemonInfoFromFile() (*model.RunInfo, error) { + dipath := filepath.Join(run.CACHE_RUN_PATH, "daemon.json") + if !pkg.IsPathExists(dipath) { + return DaemonInfo, fmt.Errorf("获取守护进程信息失败,文件不存在") + } + err := pkg.LoadJson(dipath, DaemonInfo) + return DaemonInfo, err +} + +func GetDaemonInfo() (*model.RunInfo, error) { + var err error if *DaemonInfo == (model.RunInfo{}) { - err := pkg.LoadJson(filepath.Join(cache.CACHE_RUN_PATH, "daemon.json"), DaemonInfo) + dipath := filepath.Join(run.CACHE_RUN_PATH, "daemon.json") + if !pkg.IsPathExists(dipath) { + return DaemonInfo, fmt.Errorf("获取守护进程信息失败,文件不存在") + } + err := pkg.LoadJson(dipath, DaemonInfo) if err != nil { - d.EchoFatal("获取守护进程信息失败,请执行`kd --stop && kd --daemon`") + return DaemonInfo, err } } - return DaemonInfo + return DaemonInfo, err } func getKdPIDs() { @@ -71,11 +89,11 @@ func FindServerProcess() (*process.Process, error) { if err != nil { return nil, err } - di := GetDaemonInfo() for _, p := range processes { // XXX err n, _ := p.Name() - if p.Pid == int32(di.PID) { + di, err := GetDaemonInfo() + if err == nil && p.Pid == int32(di.PID) { zap.S().Debugf("Got daemon process %v via daemon info", di.PID) cmdslice, _ := p.CmdlineSlice() if len(cmdslice) > 1 && cmdslice[1] == "--server" { diff --git a/plan.md b/plan.md index a439e5d..063eec6 100644 --- a/plan.md +++ b/plan.md @@ -2,9 +2,7 @@ ## wip -- 记录daemon更多信息 - - 运行时候比较server版本 - - check if fits daemon.json +- 读文件加锁 - 指定port - release增加version,aur判断此文件 - 长句查询 (另外缓存)