mirror of
https://github.com/TeaOSLab/EdgeNode.git
synced 2025-11-09 20:10:27 +08:00
修复节点自动升级时无法自动启动的Bug
This commit is contained in:
@@ -714,6 +714,7 @@ func (this *Node) listenSock() error {
|
|||||||
_ = this.sock.Close()
|
_ = this.sock.Close()
|
||||||
|
|
||||||
events.Notify(events.EventQuit)
|
events.Notify(events.EventQuit)
|
||||||
|
events.Notify(events.EventTerminated)
|
||||||
|
|
||||||
// 监控连接数,如果连接数为0,则退出进程
|
// 监控连接数,如果连接数为0,则退出进程
|
||||||
goman.New(func() {
|
goman.New(func() {
|
||||||
|
|||||||
@@ -30,10 +30,14 @@ type NodeStatusExecutor struct {
|
|||||||
cpuUpdatedTime time.Time
|
cpuUpdatedTime time.Time
|
||||||
cpuLogicalCount int
|
cpuLogicalCount int
|
||||||
cpuPhysicalCount int
|
cpuPhysicalCount int
|
||||||
|
|
||||||
|
ticker *time.Ticker
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewNodeStatusExecutor() *NodeStatusExecutor {
|
func NewNodeStatusExecutor() *NodeStatusExecutor {
|
||||||
return &NodeStatusExecutor{}
|
return &NodeStatusExecutor{
|
||||||
|
ticker: time.NewTicker(30 * time.Second),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *NodeStatusExecutor) Listen() {
|
func (this *NodeStatusExecutor) Listen() {
|
||||||
@@ -41,15 +45,12 @@ func (this *NodeStatusExecutor) Listen() {
|
|||||||
this.cpuUpdatedTime = time.Now()
|
this.cpuUpdatedTime = time.Now()
|
||||||
this.update()
|
this.update()
|
||||||
|
|
||||||
// TODO 这个时间间隔可以配置
|
|
||||||
var ticker = time.NewTicker(30 * time.Second)
|
|
||||||
|
|
||||||
events.OnKey(events.EventQuit, this, func() {
|
events.OnKey(events.EventQuit, this, func() {
|
||||||
remotelogs.Println("NODE_STATUS", "quit executor")
|
remotelogs.Println("NODE_STATUS", "quit executor")
|
||||||
ticker.Stop()
|
this.ticker.Stop()
|
||||||
})
|
})
|
||||||
|
|
||||||
for range ticker.C {
|
for range this.ticker.C {
|
||||||
this.isFirstTime = false
|
this.isFirstTime = false
|
||||||
this.update()
|
this.update()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,8 +14,10 @@ import (
|
|||||||
"github.com/TeaOSLab/EdgeNode/internal/utils"
|
"github.com/TeaOSLab/EdgeNode/internal/utils"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
stringutil "github.com/iwind/TeaGo/utils/string"
|
stringutil "github.com/iwind/TeaGo/utils/string"
|
||||||
|
"github.com/iwind/gosock/pkg/gosock"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@@ -46,15 +48,12 @@ func (this *UpgradeManager) Start() {
|
|||||||
}
|
}
|
||||||
this.isInstalling = true
|
this.isInstalling = true
|
||||||
|
|
||||||
// 还原安装状态
|
|
||||||
defer func() {
|
|
||||||
this.isInstalling = false
|
|
||||||
}()
|
|
||||||
|
|
||||||
remotelogs.Println("UPGRADE_MANAGER", "upgrading node ...")
|
remotelogs.Println("UPGRADE_MANAGER", "upgrading node ...")
|
||||||
err := this.install()
|
err := this.install()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
remotelogs.Error("UPGRADE_MANAGER", "download failed: "+err.Error())
|
remotelogs.Error("UPGRADE_MANAGER", "download failed: "+err.Error())
|
||||||
|
|
||||||
|
this.isInstalling = false
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -65,6 +64,8 @@ func (this *UpgradeManager) Start() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
remotelogs.Error("UPGRADE_MANAGER", err.Error())
|
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)
|
_, err := os.Stat(dir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
@@ -98,7 +99,7 @@ func (this *UpgradeManager) install() error {
|
|||||||
|
|
||||||
remotelogs.Println("UPGRADE_MANAGER", "downloading new node ...")
|
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)
|
fp, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0777)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
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")
|
err := os.Rename(target+"/bin/"+teaconst.ProcessName, target+"/bin/."+teaconst.ProcessName+".dist")
|
||||||
hasBackup := err == nil
|
var hasBackup = err == nil
|
||||||
defer func() {
|
defer func() {
|
||||||
if !isOk && hasBackup {
|
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()
|
err = unzip.Run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -225,6 +226,9 @@ func (this *UpgradeManager) unzip(zipPath string) error {
|
|||||||
|
|
||||||
// 重启
|
// 重启
|
||||||
func (this *UpgradeManager) restart() error {
|
func (this *UpgradeManager) restart() error {
|
||||||
|
// 关闭当前sock,防止无法重启
|
||||||
|
_ = gosock.NewTmpSock(teaconst.ProcessName).Close()
|
||||||
|
|
||||||
// 重新启动
|
// 重新启动
|
||||||
if DaemonIsOn && DaemonPid == os.Getppid() {
|
if DaemonIsOn && DaemonPid == os.Getppid() {
|
||||||
utils.Exit() // TODO 试着更优雅重启
|
utils.Exit() // TODO 试着更优雅重启
|
||||||
@@ -241,7 +245,9 @@ func (this *UpgradeManager) restart() error {
|
|||||||
events.Notify(events.EventTerminated)
|
events.Notify(events.EventTerminated)
|
||||||
|
|
||||||
// 启动
|
// 启动
|
||||||
cmd := exec.Command(exe, "start")
|
exe = filepath.Dir(exe) + "/" + teaconst.ProcessName
|
||||||
|
|
||||||
|
var cmd = exec.Command(exe, "start")
|
||||||
err = cmd.Start()
|
err = cmd.Start()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
Reference in New Issue
Block a user