From 56307a744dc94f9b1eed3f7513a4be16a130e984 Mon Sep 17 00:00:00 2001 From: zhangqian <zhangqian@123.com> Date: 星期二, 17 十月 2023 17:20:25 +0800 Subject: [PATCH] 定时同步任务状态 --- main.go | 68 +++++++++++++-------------------- 1 files changed, 27 insertions(+), 41 deletions(-) diff --git a/main.go b/main.go index 00d2599..8c631f7 100644 --- a/main.go +++ b/main.go @@ -12,9 +12,6 @@ "apsClient/service/plc_address" "fmt" "net/http" - "os" - "os/signal" - "syscall" "time" ) @@ -28,22 +25,8 @@ return } - if err := nsq.Init(); err != nil { - logx.Errorf("nsq Init err:%v", err) - return - } - - if err := crontask.InitTask(); err != nil { - logx.Errorf("crontab task Init err:%v", err) - return - } //鍔犺浇plc鍐欏叆鍦板潃 plc_address.LoadAddressFromFile() - - ////鎻愬墠鍔犺浇浠诲姟 - //service.NewTaskService().GetTask() - - //go shutdown() // 鍚姩鏁版嵁鍚屾 var serfStartChan = make(chan bool) @@ -53,16 +36,26 @@ "process_model", "production_progress", "work_order", + "task_status_sync", } agent := serf.InitAgent("apsClient", syncTables, sqlitex.GetDB()) agent.RegisterClusterEvent(serfClusterEvent) go agent.Serve(serfStartChan) - <-serfStartChan // 鍒ゆ柇褰撳墠闆嗙兢鐘舵�� - //agent.ClusterStatus == "master" + if agent.ClusterStatus != "slave" { + if err := nsq.Init(); err != nil { + logx.Errorf("nsq Init err:%v", err) + return + } + crontask.Once() + if err := crontask.StartTask(); err != nil { + logx.Errorf("crontab task Init err:%v", err) + return + } + } logx.Infof("apsClient start serve...") server := &http.Server{ @@ -74,29 +67,22 @@ 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 - - logx.Infof("apsClient exited...") - os.Exit(0) -} - func serfClusterEvent(stat int) { switch stat { - - case serf.EventCreateCluster: - // 鍒涘缓闆嗙兢 - case serf.EventJoinCluster: - // 鍔犲叆闆嗙兢 - case serf.EventLeaveCluster: - // 閫�鍑洪泦缇� - case serf.EventSlave2Master: - // 鍒囨崲涓轰富鑺傜偣 - case serf.EventMaster2Slave: - // 鍒囨崲涓哄瓙鑺傜偣 + case serf.EventCreateCluster, serf.EventSlave2Master, serf.EventLeaveCluster: + if err := nsq.Init(); err != nil { + logx.Errorf("nsq Init err:%v", err) + return + } + crontask.Once() + if err := crontask.StartTask(); err != nil { + logx.Errorf("crontab task Init err:%v", err) + return + } + case serf.EventJoinCluster, serf.EventMaster2Slave: + nsq.Stop() + crontask.StopTask() } - fmt.Println("clusterEvent:", stat) -} \ No newline at end of file + logx.Infof("serf cluster event: %v", stat) +} -- Gitblit v1.8.0