From 90d54bf3e27c40b1b93e3e296e4a4cbe09b631aa Mon Sep 17 00:00:00 2001 From: boyce Date: Wed, 15 May 2024 10:06:50 +0800 Subject: [PATCH] =?UTF-8?q?1.=E6=96=B0=E5=A2=9E=E6=9C=8D=E5=8A=A1=E5=92=8C?= =?UTF-8?q?Global=E9=85=8D=E7=BD=AE=E6=8E=A5=E5=8F=A3=EF=BC=8C=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E9=80=9A=E8=BF=87=E7=BB=93=E6=9E=84=E4=BD=93=E8=A7=A3?= =?UTF-8?q?=E6=9E=90=202.=E4=BC=98=E5=8C=96=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cluster/cluster.go | 21 +++++++++++++++++++++ service/service.go | 19 +++++++++++++++++++ service/servicemgr.go | 2 +- 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/cluster/cluster.go b/cluster/cluster.go index 8601992..86c5c7e 100644 --- a/cluster/cluster.go +++ b/cluster/cluster.go @@ -8,6 +8,8 @@ import ( "strings" "sync" "github.com/duanhf2012/origin/v2/event" + "errors" + "reflect" ) var configDir = "./config/" @@ -433,6 +435,25 @@ func (cls *Cluster) GetGlobalCfg() interface{} { return cls.globalCfg } + +func (cls *Cluster) ParseGlobalCfg(cfg interface{}) error{ + if cls.globalCfg == nil { + return errors.New("no service configuration found") + } + + rv := reflect.ValueOf(cls.globalCfg) + if rv.Kind() == reflect.Ptr && rv.IsNil() { + return errors.New("no service configuration found") + } + + bytes,err := json.Marshal(cls.globalCfg) + if err != nil { + return err + } + + return json.Unmarshal(bytes,cfg) +} + func (cls *Cluster) GetNodeInfo(nodeId string) (NodeInfo,bool) { cls.locker.RLock() defer cls.locker.RUnlock() diff --git a/service/service.go b/service/service.go index 1042c7c..5490187 100644 --- a/service/service.go +++ b/service/service.go @@ -14,6 +14,7 @@ import ( "sync" "sync/atomic" "github.com/duanhf2012/origin/v2/concurrent" + "encoding/json" ) var timerDispatcherLen = 100000 @@ -295,6 +296,24 @@ func (s *Service) GetServiceCfg()interface{}{ return s.serviceCfg } +func (s *Service) ParseServiceCfg(cfg interface{}) error{ + if s.serviceCfg == nil { + return errors.New("no service configuration found") + } + + rv := reflect.ValueOf(s.serviceCfg) + if rv.Kind() == reflect.Ptr && rv.IsNil() { + return errors.New("no service configuration found") + } + + bytes,err := json.Marshal(s.serviceCfg) + if err != nil { + return err + } + + return json.Unmarshal(bytes,cfg) +} + func (s *Service) GetProfiler() *profiler.Profiler{ return s.profiler } diff --git a/service/servicemgr.go b/service/servicemgr.go index 3047d60..236871e 100644 --- a/service/servicemgr.go +++ b/service/servicemgr.go @@ -23,7 +23,7 @@ func Init() { for _,s := range setupServiceList { err := s.OnInit() if err != nil { - log.SError("Failed to initialize "+s.GetName()+" service:"+err.Error()) + log.Error("Failed to initialize "+s.GetName()+" service",log.ErrorAttr("err",err)) os.Exit(1) } }