|
@@ -21,11 +21,14 @@ var (
|
|
|
cloudStatus = "NONE"
|
|
|
lastLocalStatus = "idle"
|
|
|
lastCloudStatus = "NONE"
|
|
|
+ localTurnLength = 1 // s,本地状态刷新时间
|
|
|
+ cloudTurnLength = 60 // s,云端状态刷新时间
|
|
|
+ renewTurnLength = 3 // s,续约状态刷新时间
|
|
|
+ waitStopLength = 1 // min,停止master前等待时间
|
|
|
launchedFlag = false
|
|
|
renewedFlag = false
|
|
|
- configuredFlag = false
|
|
|
renewTimer *time.Timer // 续约定时器
|
|
|
- RenewDur = 5 // 续约时间为5分钟
|
|
|
+ RenewDur = 5 // min, 续约时间
|
|
|
)
|
|
|
|
|
|
func init() {
|
|
@@ -45,14 +48,20 @@ func init() {
|
|
|
}
|
|
|
|
|
|
func renew() {
|
|
|
- if renewTimer != nil {
|
|
|
- renewTimer.Stop()
|
|
|
+ for {
|
|
|
+ time.Sleep(time.Duration(renewTurnLength) * time.Second)
|
|
|
+ if localStatus == "running" && !renewedFlag { // 设备处于运行状态,且尚未续约
|
|
|
+ c_log.GlobalLogger.Info("设备仍处于运行状态,续约 - 开始。")
|
|
|
+ if renewTimer != nil {
|
|
|
+ renewTimer.Stop()
|
|
|
+ }
|
|
|
+ renewedFlag = true
|
|
|
+ renewTimer = time.AfterFunc(time.Duration(RenewDur)*time.Minute, func() {
|
|
|
+ renewedFlag = false
|
|
|
+ })
|
|
|
+ c_log.GlobalLogger.Infof("续约时间【%v】分钟 - 成功。", RenewDur)
|
|
|
+ }
|
|
|
}
|
|
|
- renewedFlag = true
|
|
|
- renewTimer = time.AfterFunc(time.Duration(RenewDur)*time.Minute, func() {
|
|
|
- renewedFlag = false
|
|
|
- })
|
|
|
- c_log.GlobalLogger.Infof("续约时间【%v】分钟 - 成功。", RenewDur)
|
|
|
}
|
|
|
|
|
|
func startMasterNode() {
|
|
@@ -85,9 +94,6 @@ func stopMasterNode() {
|
|
|
reply := 0
|
|
|
if err = KillRpcClient.Call("KillService.Kill", killArgs, &reply); err != nil {
|
|
|
c_log.GlobalLogger.Error("发送 rpc 请求到 master 报错:", err)
|
|
|
- // 这里可能会报错unexpected EOF但是不影响,先注释 close 和 continue
|
|
|
- //KillRpcClient.Close()
|
|
|
- //continue
|
|
|
}
|
|
|
|
|
|
c_log.GlobalLogger.Info("结束任务后,将数据闭环平台配置置空。")
|
|
@@ -101,10 +107,6 @@ func stopMasterNode() {
|
|
|
}
|
|
|
|
|
|
func main() {
|
|
|
- localTurnLength := 2 // s
|
|
|
- cloudTurnLength := 60 // s
|
|
|
- waitStopLength := 1 // min
|
|
|
-
|
|
|
// 更新本地任务状态
|
|
|
go pkg.GetLocalStatus(&localStatus, &lastLocalStatus, localTurnLength)
|
|
|
// 更新云端任务状态
|
|
@@ -135,19 +137,13 @@ func main() {
|
|
|
if !launchedFlag { // 目前未启动数采程序
|
|
|
c_log.GlobalLogger.Info("数采程序启动 - 开始。")
|
|
|
startMasterNode()
|
|
|
- } else { // 已启动数采程序
|
|
|
- c_log.GlobalLogger.Info("设备仍处于运行状态,续约 - 开始。")
|
|
|
- // 续约
|
|
|
- renew()
|
|
|
}
|
|
|
- continue
|
|
|
} else if localStatus == "idle" {
|
|
|
if !renewedFlag && launchedFlag && len(entity.TimeWindowConsumerQueue) == 0 {
|
|
|
- c_log.GlobalLogger.Info("设备不在运行状态且没有待处理的数据,数采程序关闭 - 开始。")
|
|
|
time.Sleep(time.Duration(waitStopLength) * time.Minute)
|
|
|
+ c_log.GlobalLogger.Info("设备不在运行状态且没有待处理的数据,数采程序关闭 - 开始。")
|
|
|
stopMasterNode()
|
|
|
}
|
|
|
- continue
|
|
|
} else if localStatus == "error" {
|
|
|
c_log.GlobalLogger.Error("设备运行状态出错,停止数采程序。")
|
|
|
stopMasterNode()
|
|
@@ -155,66 +151,12 @@ func main() {
|
|
|
c_log.GlobalLogger.Error("未知的设备运行状态。【status】=", localStatus)
|
|
|
}
|
|
|
|
|
|
+ // 定期检查本地任务状态,执行续约,避免短时间内多次启停
|
|
|
+ go renew()
|
|
|
+
|
|
|
// 云端任务状态负责更新配置
|
|
|
go pkg.GetCloudConfig(cloudStatus, lastCloudStatus, cloudTurnLength)
|
|
|
|
|
|
- //if cloudStatus == "UN_CHANGE" {
|
|
|
- // continue
|
|
|
- //} else if cloudStatus == "CHANGE" || cloudStatus == "NONE" {
|
|
|
- // if lastCloudStatus == "CHANGE" && cloudStatus == "CHANGE" { // 供更新使用
|
|
|
- // commonConfig.InitPlatformConfig()
|
|
|
- // continue
|
|
|
- // }
|
|
|
- // if lastCloudStatus == "NONE" && cloudStatus == "NONE" {
|
|
|
- // continue
|
|
|
- // }
|
|
|
- //
|
|
|
- // if lastCloudStatus == "NONE" && cloudStatus == "CHANGE" {
|
|
|
- // if _, err := util.ExecuteWithPath(commonConfig.LocalConfig.RestartCmd.Dir, commonConfig.LocalConfig.RestartCmd.Name, commonConfig.LocalConfig.RestartCmd.Args...); err != nil {
|
|
|
- // c_log.GlobalLogger.Info("启动新程序失败,【path】=", commonConfig.LocalConfig.RestartCmd.Dir, "【cmd】=", commonConfig.LocalConfig.RestartCmd.Name, commonConfig.LocalConfig.RestartCmd.Args, ":", err)
|
|
|
- // os.Exit(-1)
|
|
|
- // }
|
|
|
- // c_log.GlobalLogger.Info("启动任务,本地执行启动命令:【path】=", commonConfig.LocalConfig.RestartCmd.Dir, "【cmd】=", commonConfig.LocalConfig.RestartCmd.Name, commonConfig.LocalConfig.RestartCmd.Args)
|
|
|
- // c_log.GlobalLogger.Info("获取数据闭环平台最新配置。")
|
|
|
- // commonConfig.InitPlatformConfig()
|
|
|
- // continue
|
|
|
- // }
|
|
|
- //
|
|
|
- // // 3 发送rpc信号杀死采集程序
|
|
|
- // var killArgs commonService.KillSignal
|
|
|
- // if lastCloudStatus == "UN_CHANGE" && cloudStatus == "CHANGE" {
|
|
|
- // killArgs = commonService.KillSignal{NodeName: "master", DropUploadData: commonConfig.PlatformConfig.DropUploadData, Restart: true}
|
|
|
- // c_log.GlobalLogger.Info("更新任务,发送rpc重启信号:", killArgs)
|
|
|
- // }
|
|
|
- // if lastCloudStatus == "UN_CHANGE" && cloudStatus == "NONE" {
|
|
|
- // killArgs = commonService.KillSignal{NodeName: "master", DropUploadData: commonConfig.PlatformConfig.DropUploadData, Restart: false}
|
|
|
- // c_log.GlobalLogger.Info("杀死任务,发送rpc结束信号:", killArgs)
|
|
|
- // }
|
|
|
- //
|
|
|
- // KillRpcClient, err := rpc.Dial("tcp", commonConfig.LocalConfig.Node.Ip+":"+commonConfig.CloudConfig.RpcPort)
|
|
|
- // if err != nil {
|
|
|
- // // 此处如果连接失败说明采集程序已经停止了
|
|
|
- // lastCloudStatus = "NONE"
|
|
|
- // c_log.GlobalLogger.Error("采集程序已经停止:", err)
|
|
|
- // continue
|
|
|
- // }
|
|
|
- //
|
|
|
- // reply := 0
|
|
|
- // if err = KillRpcClient.Call("KillService.Kill", killArgs, &reply); err != nil {
|
|
|
- // c_log.GlobalLogger.Error("发送 rpc 请求到 master 报错:", err)
|
|
|
- // // 这里可能会报错unexpected EOF但是不影响,先注释 close 和 continue
|
|
|
- // //KillRpcClient.Close()
|
|
|
- // //continue
|
|
|
- // }
|
|
|
- //
|
|
|
- // c_log.GlobalLogger.Info("结束任务后,将数据闭环平台配置置空。")
|
|
|
- // commonConfig.PlatformConfig = commonConfig.PlatformConfigStruct{}
|
|
|
- // if err = KillRpcClient.Close(); err != nil {
|
|
|
- // // 不做处理
|
|
|
- // }
|
|
|
- //} else {
|
|
|
- // c_log.GlobalLogger.Error("未知的采集任务状态。【cloudStatus】=", cloudStatus)
|
|
|
- //}
|
|
|
}
|
|
|
- //select {}
|
|
|
+
|
|
|
}
|