From 2ea23e2364f742f528fb8b97657212c174d1bbd7 Mon Sep 17 00:00:00 2001 From: zhangqian <zhangqian@123.com> Date: 星期六, 21 十月 2023 14:06:59 +0800 Subject: [PATCH] 增加切换serf状态接口(调试用) --- main.go | 80 +++++++++++++++++++++++++++++++++++----- 1 files changed, 70 insertions(+), 10 deletions(-) diff --git a/main.go b/main.go index fbf0511..7acd081 100644 --- a/main.go +++ b/main.go @@ -2,14 +2,17 @@ import ( "apsClient/conf" + "apsClient/crontask" "apsClient/model" + "apsClient/nsq" "apsClient/pkg/logx" + "apsClient/pkg/sqlitex" "apsClient/router" + "apsClient/serf" + "apsClient/service/plc_address" "fmt" + "log" "net/http" - "os" - "os/signal" - "syscall" "time" ) @@ -23,7 +26,49 @@ return } - go shutdown() + //鍔犺浇plc鍐欏叆鍦板潃 + plc_address.LoadAddressFromFile() + + // 鍚姩鏁版嵁鍚屾 + var serfStartChan = make(chan bool) + + // 闇�瑕佸悓姝ョ殑琛� + var syncTables = []string{ + "procedures", + "process_model", + "production_progress", + "work_order", + "task_status_sync", + "device", + "device_plc", + } + + agent := serf.InitAgent("apsClient", syncTables, sqlitex.GetDB()) + agent.RegisterClusterEvent(serfClusterEvent) + + go agent.Serve(serfStartChan) + <-serfStartChan + //if !<-serfStartChan { + // logx.Errorf("serf Init err, exit") + // return + //} + + // 鍒ゆ柇褰撳墠闆嗙兢鐘舵�� + logx.Infof("current agent.ClusterStatus:%v", agent.ClusterStatus) + log.Println("current agent.ClusterStatus:", agent.ClusterStatus) + if agent.ClusterStatus != "slave" { + if err := nsq.Init(); err != nil { + logx.Errorf("nsq Init err:%v", err) + return + } + + } + crontask.Once(agent.ClusterStatus != "slave") + if err := crontask.StartTask(agent.ClusterStatus != "slave"); err != nil { + logx.Errorf("crontab task Init err:%v", err) + return + } + logx.Infof("apsClient start serve...") server := &http.Server{ Addr: fmt.Sprintf(":%d", conf.Conf.System.Port), @@ -34,11 +79,26 @@ logx.Error(server.ListenAndServe().Error()) } -func shutdown() { - quit := make(chan os.Signal, 1) - signal.Notify(quit, syscall.SIGKILL, syscall.SIGQUIT, syscall.SIGINT, syscall.SIGTERM) - <-quit +func serfClusterEvent(stat int) { + switch stat { + case serf.EventCreateCluster, serf.EventSlave2Master, serf.EventLeaveCluster: + if err := nsq.Init(); err != nil { //寮�鍚痭sq + logx.Errorf("nsq Init err:%v", err) + return + } + crontask.Once(true) + if err := crontask.RestartTask(true); err != nil { //浠aster鏂瑰紡閲嶅惎task + logx.Errorf("crontab task Init err:%v", err) + return + } + case serf.EventJoinCluster, serf.EventMaster2Slave: + nsq.Stop() //鍏抽棴nsq + crontask.Once(false) + if err := crontask.RestartTask(false); err != nil { //浠ラ潪master鏂瑰紡閲嶅惎task + logx.Errorf("crontab task Init err:%v", err) + return + } + } - logx.Infof("apsClient exited...") - os.Exit(0) + logx.Infof("serf cluster event: %v", stat) } -- Gitblit v1.8.0