From 63723711e37b8e587ac1d72222a00e8ff81b3bea Mon Sep 17 00:00:00 2001 From: Trial97 Date: Wed, 2 Oct 2019 13:44:08 +0300 Subject: [PATCH] Added Guardian as service in ServiceManager --- cmd/cgr-engine/cgr-engine.go | 17 ++----- services/guardian.go | 94 ++++++++++++++++++++++++++++++++++++ servmanager/servmanager.go | 1 + utils/consts.go | 1 + 4 files changed, 100 insertions(+), 13 deletions(-) create mode 100644 services/guardian.go diff --git a/cmd/cgr-engine/cgr-engine.go b/cmd/cgr-engine/cgr-engine.go index 520cf7819..7f2efac16 100644 --- a/cmd/cgr-engine/cgr-engine.go +++ b/cmd/cgr-engine/cgr-engine.go @@ -282,14 +282,6 @@ func startAnalyzerService(internalAnalyzerSChan chan rpcclient.RpcClientConnecti internalAnalyzerSChan <- aSv1 } -func initGuardianSv1(internalGuardianSChan chan rpcclient.RpcClientConnection, server *utils.Server) { - grdSv1 := v1.NewGuardianSv1() - if !cfg.DispatcherSCfg().Enabled { - server.RpcRegister(grdSv1) - } - internalGuardianSChan <- grdSv1 -} - func initCoreSv1(internalCoreSv1Chan chan rpcclient.RpcClientConnection, server *utils.Server) { cSv1 := v1.NewCoreSv1(engine.NewCoreService()) if !cfg.DispatcherSCfg().Enabled { @@ -639,15 +631,12 @@ func main() { filterSChan := make(chan *engine.FilterS, 1) internalDispatcherSChan := make(chan rpcclient.RpcClientConnection, 1) internalAnalyzerSChan := make(chan rpcclient.RpcClientConnection, 1) - internalGuardianSChan := make(chan rpcclient.RpcClientConnection, 1) + internalLoaderSChan := make(chan rpcclient.RpcClientConnection, 1) internalServeManagerChan := make(chan rpcclient.RpcClientConnection, 1) internalConfigChan := make(chan rpcclient.RpcClientConnection, 1) internalCoreSv1Chan := make(chan rpcclient.RpcClientConnection, 1) - // init GuardianSv1 - initGuardianSv1(internalGuardianSChan, server) - // init CoreSv1 initCoreSv1(internalCoreSv1Chan, server) @@ -668,7 +657,8 @@ func main() { apiv2, _ := srvManager.GetService(utils.ApierV2) resp, _ := srvManager.GetService(utils.ResponderS) smg := services.NewSessionService() - srvManager.AddService(chS, attrS, chrS, tS, stS, reS, supS, schS, cdrS, rals, smg, + grd := services.NewGuardianService() + srvManager.AddService(chS, attrS, chrS, tS, stS, reS, supS, schS, cdrS, rals, smg, grd, services.NewEventReaderService(), services.NewDNSAgent(), services.NewFreeswitchAgent(), @@ -692,6 +682,7 @@ func main() { internalRaterChan := resp.GetIntenternalChan() internalRALsv1Chan := rals.GetIntenternalChan() internalSMGChan := smg.GetIntenternalChan() + internalGuardianSChan := grd.GetIntenternalChan() srvManager.StartServices() cacheS := srvManager.GetCacheS() diff --git a/services/guardian.go b/services/guardian.go new file mode 100644 index 000000000..0ac296bf5 --- /dev/null +++ b/services/guardian.go @@ -0,0 +1,94 @@ +/* +Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments +Copyright (C) ITsysCOM GmbH + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see +*/ + +package services + +import ( + "sync" + + v1 "github.com/cgrates/cgrates/apier/v1" + "github.com/cgrates/cgrates/servmanager" + "github.com/cgrates/cgrates/utils" + "github.com/cgrates/rpcclient" +) + +// NewGuardianService returns the Guardian Service +func NewGuardianService() servmanager.Service { + return &GuardianService{ + connChan: make(chan rpcclient.RpcClientConnection, 1), + } +} + +// GuardianService implements Service interface +type GuardianService struct { + sync.RWMutex + rpc *v1.GuardianSv1 + connChan chan rpcclient.RpcClientConnection +} + +// Start should handle the sercive start +// populates internal channel for RPC conns +func (grd *GuardianService) Start(sp servmanager.ServiceProvider, waitGuardian bool) (err error) { + // safe to not check GuardianS should never be stoped and then started again + // if grd.IsRunning() { + // return fmt.Errorf("service aleady running") + // } + + grd.Lock() + defer grd.Unlock() + + grd.rpc = v1.NewGuardianSv1() + if !sp.GetConfig().DispatcherSCfg().Enabled { + sp.GetServer().RpcRegister(grd.rpc) + } + grd.connChan <- grd.rpc + + return +} + +// GetIntenternalChan returns the internal connection chanel +func (grd *GuardianService) GetIntenternalChan() (conn chan rpcclient.RpcClientConnection) { + return grd.connChan +} + +// Reload handles the change of config +func (grd *GuardianService) Reload(sp servmanager.ServiceProvider) (err error) { + return +} + +// Shutdown stops the service +func (grd *GuardianService) Shutdown() (err error) { + return +} + +// GetRPCInterface returns the interface to register for server +func (grd *GuardianService) GetRPCInterface() interface{} { + return grd.rpc +} + +// IsRunning returns if the service is running +func (grd *GuardianService) IsRunning() bool { + grd.RLock() + defer grd.RUnlock() + return grd != nil && grd.rpc != nil +} + +// ServiceName returns the service name +func (grd *GuardianService) ServiceName() string { + return utils.GuardianS +} diff --git a/servmanager/servmanager.go b/servmanager/servmanager.go index a6a2498fb..053f02218 100644 --- a/servmanager/servmanager.go +++ b/servmanager/servmanager.go @@ -245,6 +245,7 @@ func (srvMngr *ServiceManager) StartServices() (err error) { } chS.Start(srvMngr, true) } + go srvMngr.startService(utils.GuardianS) go srvMngr.handleReload() if srvMngr.GetConfig().AttributeSCfg().Enabled { diff --git a/utils/consts.go b/utils/consts.go index dc4fd12e8..980b1bbf9 100755 --- a/utils/consts.go +++ b/utils/consts.go @@ -602,6 +602,7 @@ const ( AnalyzerS = "AnalyzerS" CDRServer = "CDRServer" ResponderS = "ResponderS" + GuardianS = "GuardianS" ) // Lower service names