Skip to content

Commit

Permalink
Merge pull request #21 from Karmenzind/dev-fix-first-launch
Browse files Browse the repository at this point in the history
(Fixed #20) 优化daemon校验规则;增加周期检查daemon信息;set version 0.0.6
  • Loading branch information
Karmenzind authored Jan 3, 2024
2 parents 9e4393c + fec743d commit 6f30a64
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 40 deletions.
13 changes: 8 additions & 5 deletions cmd/kd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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") != "" {
Expand Down
12 changes: 6 additions & 6 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`

Expand Down Expand Up @@ -107,7 +107,7 @@ func InitConfig() error {
err = Cfg.CheckAndApply()
if err != nil {
fmt.Println(err)
os.Exit(1)
os.Exit(1)
}
return err
}
4 changes: 2 additions & 2 deletions internal/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
52 changes: 42 additions & 10 deletions internal/daemon/cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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() {
Expand All @@ -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)
}

}
}
Expand Down
46 changes: 32 additions & 14 deletions internal/daemon/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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() {
Expand All @@ -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" {
Expand Down
4 changes: 1 addition & 3 deletions plan.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

## wip

- 记录daemon更多信息
- 运行时候比较server版本
- check if fits daemon.json
- 读文件加锁
- 指定port
- release增加version,aur判断此文件
- 长句查询 (另外缓存)
Expand Down

0 comments on commit 6f30a64

Please sign in to comment.