diff --git a/internal/nodes/node.go b/internal/nodes/node.go index 3f501fe..5518d4a 100644 --- a/internal/nodes/node.go +++ b/internal/nodes/node.go @@ -714,6 +714,7 @@ func (this *Node) listenSock() error { _ = this.sock.Close() events.Notify(events.EventQuit) + events.Notify(events.EventTerminated) // 监控连接数,如果连接数为0,则退出进程 goman.New(func() { diff --git a/internal/nodes/node_status_executor.go b/internal/nodes/node_status_executor.go index 94a34d2..856f6a7 100644 --- a/internal/nodes/node_status_executor.go +++ b/internal/nodes/node_status_executor.go @@ -30,10 +30,14 @@ type NodeStatusExecutor struct { cpuUpdatedTime time.Time cpuLogicalCount int cpuPhysicalCount int + + ticker *time.Ticker } func NewNodeStatusExecutor() *NodeStatusExecutor { - return &NodeStatusExecutor{} + return &NodeStatusExecutor{ + ticker: time.NewTicker(30 * time.Second), + } } func (this *NodeStatusExecutor) Listen() { @@ -41,15 +45,12 @@ func (this *NodeStatusExecutor) Listen() { this.cpuUpdatedTime = time.Now() this.update() - // TODO 这个时间间隔可以配置 - var ticker = time.NewTicker(30 * time.Second) - events.OnKey(events.EventQuit, this, func() { remotelogs.Println("NODE_STATUS", "quit executor") - ticker.Stop() + this.ticker.Stop() }) - for range ticker.C { + for range this.ticker.C { this.isFirstTime = false this.update() } diff --git a/internal/nodes/upgrade_manager.go b/internal/nodes/upgrade_manager.go index ead0d1a..f13443f 100644 --- a/internal/nodes/upgrade_manager.go +++ b/internal/nodes/upgrade_manager.go @@ -14,8 +14,10 @@ import ( "github.com/TeaOSLab/EdgeNode/internal/utils" "github.com/iwind/TeaGo/Tea" stringutil "github.com/iwind/TeaGo/utils/string" + "github.com/iwind/gosock/pkg/gosock" "os" "os/exec" + "path/filepath" "runtime" "time" ) @@ -46,15 +48,12 @@ func (this *UpgradeManager) Start() { } this.isInstalling = true - // 还原安装状态 - defer func() { - this.isInstalling = false - }() - remotelogs.Println("UPGRADE_MANAGER", "upgrading node ...") err := this.install() if err != nil { remotelogs.Error("UPGRADE_MANAGER", "download failed: "+err.Error()) + + this.isInstalling = false return } @@ -65,6 +64,8 @@ func (this *UpgradeManager) Start() { if err != nil { remotelogs.Error("UPGRADE_MANAGER", err.Error()) } + + this.isInstalling = false }) } @@ -83,7 +84,7 @@ func (this *UpgradeManager) install() error { } // 创建临时文件 - dir := Tea.Root + "/tmp" + var dir = Tea.Root + "/tmp" _, err := os.Stat(dir) if err != nil { if os.IsNotExist(err) { @@ -98,7 +99,7 @@ func (this *UpgradeManager) install() error { remotelogs.Println("UPGRADE_MANAGER", "downloading new node ...") - path := dir + "/edge-node" + ".tmp" + var path = dir + "/edge-node" + ".tmp" fp, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0777) if err != nil { return err @@ -203,16 +204,16 @@ func (this *UpgradeManager) unzip(zipPath string) error { } // 先改先前的可执行文件 - err := os.Rename(target+"/bin/edge-node", target+"/bin/.edge-node.dist") - hasBackup := err == nil + err := os.Rename(target+"/bin/"+teaconst.ProcessName, target+"/bin/."+teaconst.ProcessName+".dist") + var hasBackup = err == nil defer func() { if !isOk && hasBackup { // 失败时还原 - _ = os.Rename(target+"/bin/.edge-node.dist", target+"/bin/edge-node") + _ = os.Rename(target+"/bin/."+teaconst.ProcessName+".dist", target+"/bin/"+teaconst.ProcessName) } }() - unzip := utils.NewUnzip(zipPath, target, "edge-node/") + var unzip = utils.NewUnzip(zipPath, target, "edge-node/") err = unzip.Run() if err != nil { return err @@ -225,6 +226,9 @@ func (this *UpgradeManager) unzip(zipPath string) error { // 重启 func (this *UpgradeManager) restart() error { + // 关闭当前sock,防止无法重启 + _ = gosock.NewTmpSock(teaconst.ProcessName).Close() + // 重新启动 if DaemonIsOn && DaemonPid == os.Getppid() { utils.Exit() // TODO 试着更优雅重启 @@ -241,7 +245,9 @@ func (this *UpgradeManager) restart() error { events.Notify(events.EventTerminated) // 启动 - cmd := exec.Command(exe, "start") + exe = filepath.Dir(exe) + "/" + teaconst.ProcessName + + var cmd = exec.Command(exe, "start") err = cmd.Start() if err != nil { return err