From cf0b93fc7b4f95fc2326abc768f1e12c4212c3a8 Mon Sep 17 00:00:00 2001 From: Wangjunqi123 Date: Wed, 1 Nov 2023 10:04:13 +0800 Subject: [PATCH] move initdb() to service --- server/dao/init.go | 49 -------------------- server/main.go | 4 +- server/service/background/initdb.go | 71 +++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+), 51 deletions(-) create mode 100644 server/service/background/initdb.go diff --git a/server/dao/init.go b/server/dao/init.go index 2a805a0..07a0cc0 100755 --- a/server/dao/init.go +++ b/server/dao/init.go @@ -1,50 +1 @@ package dao - -import ( - "os" - "time" - - "github.com/pkg/errors" - - "gitee.com/openeuler/PilotGo-plugin-topology-server/agentmanager" - "gitee.com/openeuler/PilotGo-plugin-topology-server/conf" -) - -func InitDB() { - var unixtime_now int64 - var period int64 - var runningAgents int - period = conf.Global_config.Topo.Period - - switch conf.Global_config.Topo.Database { - case "neo4j": - go func(pt int64) { - for true { - // if runningAgents = agentmanager.Topo.GetRunningAgentNumber(); runningAgents <= 0 { - // err := errors.New("no running agent **warn**1") - // agentmanager.Topo.ErrCh <- err - - // time.Sleep(5 * time.Second) - // continue - // } - - unixtime_now = time.Now().Unix() - Neo4j = CreateNeo4j(conf.Global_config.Neo4j.Addr, conf.Global_config.Neo4j.Username, conf.Global_config.Neo4j.Password, conf.Global_config.Neo4j.DB) - PeriodProcessNeo4j(unixtime_now, runningAgents) - time.Sleep(time.Duration(period) * time.Second) - - // break - } - }(period) - case "otherDB": - - default: - err := errors.Errorf("unknown database in config_server.yaml: %s **fatal**4", conf.Global_config.Topo.Database) // err top - agentmanager.Topo.ErrCh <- err - agentmanager.Topo.Errmu.Lock() - agentmanager.Topo.ErrCond.Wait() - agentmanager.Topo.Errmu.Unlock() - close(agentmanager.Topo.ErrCh) - os.Exit(1) - } -} diff --git a/server/main.go b/server/main.go index d42f721..dc77116 100644 --- a/server/main.go +++ b/server/main.go @@ -4,8 +4,8 @@ import ( "fmt" "gitee.com/openeuler/PilotGo-plugin-topology-server/agentmanager" - "gitee.com/openeuler/PilotGo-plugin-topology-server/dao" "gitee.com/openeuler/PilotGo-plugin-topology-server/handler" + service "gitee.com/openeuler/PilotGo-plugin-topology-server/service/background" ) func main() { @@ -40,7 +40,7 @@ func main() { /* init database */ - dao.InitDB() + service.InitDB() /* init web server diff --git a/server/service/background/initdb.go b/server/service/background/initdb.go new file mode 100644 index 0000000..2b1c8a4 --- /dev/null +++ b/server/service/background/initdb.go @@ -0,0 +1,71 @@ +package service + +import ( + "os" + "time" + + "github.com/pkg/errors" + + "gitee.com/openeuler/PilotGo-plugin-topology-server/agentmanager" + "gitee.com/openeuler/PilotGo-plugin-topology-server/conf" + "gitee.com/openeuler/PilotGo-plugin-topology-server/dao" +) + +func InitDB() { + var graphperiod int64 + var promeperiod int64 + var runningAgents int + + graphperiod = conf.Global_config.Topo.Period + switch conf.Global_config.Topo.GraphDB { + case "neo4j": + dao.Neo4j = dao.CreateNeo4j(conf.Global_config.Neo4j.Addr, conf.Global_config.Neo4j.Username, conf.Global_config.Neo4j.Password, conf.Global_config.Neo4j.DB) + go func(interval int64) { + if conf.Config().Topo.Use { + for true { + // if runningAgents = agentmanager.Topo.GetRunningAgentNumber(); runningAgents <= 0 { + // err := errors.New("no running agent **warn**1") + // agentmanager.Topo.ErrCh <- err + + // time.Sleep(5 * time.Second) + // continue + // } + + unixtime_now := time.Now().Unix() + PeriodProcessNeo4j(unixtime_now, runningAgents) + time.Sleep(time.Duration(interval) * time.Second) + + // break + } + } + }(graphperiod) + case "otherDB": + + default: + err := errors.Errorf("unknown database in config_server.yaml: %s **fatal**4", conf.Global_config.Topo.GraphDB) // err top + agentmanager.Topo.ErrCh <- err + agentmanager.Topo.Errmu.Lock() + agentmanager.Topo.ErrCond.Wait() + agentmanager.Topo.Errmu.Unlock() + close(agentmanager.Topo.ErrCh) + os.Exit(1) + } + + promeperiod = conf.Config().Prometheus.Period + go func(interval int64) { + if conf.Config().Prometheus.Use { + dao.Prome = dao.CreatePrometheus(conf.Config().Prometheus.Addr) + err := dao.Prome.CreateAPI() + if err != nil { + err = errors.Wrap(err, " **warn**2") // err top + agentmanager.Topo.ErrCh <- err + } + + for true { + unixtime_now := time.Now().Unix() + PeriodProcessPrometheus(unixtime_now, runningAgents) + time.Sleep(time.Duration(interval) * time.Second) + } + } + }(promeperiod) +} -- Gitee