diff --git a/services/analyzers.go b/services/analyzers.go index 6f091a0eb..8005f79cd 100644 --- a/services/analyzers.go +++ b/services/analyzers.go @@ -56,7 +56,7 @@ type AnalyzerService struct { // Start should handle the sercive start func (anz *AnalyzerService) Start() (err error) { if anz.IsRunning() { - return fmt.Errorf("service aleady running") + return utils.ErrServiceAlreadyRunning } if anz.anz, err = analyzers.NewAnalyzerService(); err != nil { utils.Logger.Crit(fmt.Sprintf("<%s> Could not init, error: %s", utils.AnalyzerS, err.Error())) diff --git a/services/apierv1.go b/services/apierv1.go index 82b988170..197612830 100644 --- a/services/apierv1.go +++ b/services/apierv1.go @@ -76,7 +76,7 @@ type APIerSv1Service struct { // For this service the start should be called from RAL Service func (apiService *APIerSv1Service) Start() (err error) { if apiService.IsRunning() { - return fmt.Errorf("service aleady running") + return utils.ErrServiceAlreadyRunning } filterS := <-apiService.filterSChan diff --git a/services/apierv2.go b/services/apierv2.go index 99645817b..d0faf254d 100644 --- a/services/apierv2.go +++ b/services/apierv2.go @@ -19,7 +19,6 @@ along with this program. If not, see package services import ( - "fmt" "sync" v2 "github.com/cgrates/cgrates/apier/v2" @@ -55,7 +54,7 @@ type APIerSv2Service struct { // For this service the start should be called from RAL Service func (api *APIerSv2Service) Start() (err error) { if api.IsRunning() { - return fmt.Errorf("service aleady running") + return utils.ErrServiceAlreadyRunning } apiV1Chan := api.apiv1.GetAPIerSv1Chan() diff --git a/services/asteriskagent.go b/services/asteriskagent.go index 200ecd090..3479e1f03 100644 --- a/services/asteriskagent.go +++ b/services/asteriskagent.go @@ -53,7 +53,7 @@ type AsteriskAgent struct { // Start should handle the sercive start func (ast *AsteriskAgent) Start() (err error) { if ast.IsRunning() { - return fmt.Errorf("service aleady running") + return utils.ErrServiceAlreadyRunning } ast.Lock() diff --git a/services/attributes.go b/services/attributes.go index c702e1c8f..1c2830309 100644 --- a/services/attributes.go +++ b/services/attributes.go @@ -61,7 +61,7 @@ type AttributeService struct { // Start should handle the sercive start func (attrS *AttributeService) Start() (err error) { if attrS.IsRunning() { - return fmt.Errorf("service aleady running") + return utils.ErrServiceAlreadyRunning } <-attrS.cacheS.GetPrecacheChannel(utils.CacheAttributeProfiles) diff --git a/services/cdrs.go b/services/cdrs.go index dc5964fe5..ac6e886fe 100644 --- a/services/cdrs.go +++ b/services/cdrs.go @@ -70,7 +70,7 @@ type CDRServer struct { // Start should handle the sercive start func (cdrService *CDRServer) Start() (err error) { if cdrService.IsRunning() { - return fmt.Errorf("service aleady running") + return utils.ErrServiceAlreadyRunning } utils.Logger.Info(fmt.Sprintf("<%s> starting <%s> subsystem", utils.CoreS, utils.CDRs)) diff --git a/services/chargers.go b/services/chargers.go index 38d6a3104..f585afa91 100644 --- a/services/chargers.go +++ b/services/chargers.go @@ -63,7 +63,7 @@ type ChargerService struct { // Start should handle the sercive start func (chrS *ChargerService) Start() (err error) { if chrS.IsRunning() { - return fmt.Errorf("service aleady running") + return utils.ErrServiceAlreadyRunning } <-chrS.cacheS.GetPrecacheChannel(utils.CacheChargerProfiles) diff --git a/services/datadb.go b/services/datadb.go index 17989f3bb..6d520b2e0 100644 --- a/services/datadb.go +++ b/services/datadb.go @@ -50,7 +50,7 @@ type DataDBService struct { // Start should handle the sercive start func (db *DataDBService) Start() (err error) { if db.IsRunning() { - return fmt.Errorf("service aleady running") + return utils.ErrServiceAlreadyRunning } db.Lock() defer db.Unlock() diff --git a/services/diameteragent.go b/services/diameteragent.go index 998b77c43..9f6d037d5 100644 --- a/services/diameteragent.go +++ b/services/diameteragent.go @@ -54,7 +54,7 @@ type DiameterAgent struct { // Start should handle the sercive start func (da *DiameterAgent) Start() (err error) { if da.IsRunning() { - return fmt.Errorf("service aleady running") + return utils.ErrServiceAlreadyRunning } filterS := <-da.filterSChan diff --git a/services/dispatchers.go b/services/dispatchers.go index 4c8135255..bd2bedc74 100644 --- a/services/dispatchers.go +++ b/services/dispatchers.go @@ -64,7 +64,7 @@ type DispatcherService struct { // Start should handle the sercive start func (dspS *DispatcherService) Start() (err error) { if dspS.IsRunning() { - return fmt.Errorf("service aleady running") + return utils.ErrServiceAlreadyRunning } utils.Logger.Info("Starting CGRateS Dispatcher service.") fltrS := <-dspS.filterSChan diff --git a/services/dnsagent.go b/services/dnsagent.go index 49e023b7a..f2c70366d 100644 --- a/services/dnsagent.go +++ b/services/dnsagent.go @@ -56,7 +56,7 @@ type DNSAgent struct { // Start should handle the sercive start func (dns *DNSAgent) Start() (err error) { if dns.IsRunning() { - return fmt.Errorf("service aleady running") + return utils.ErrServiceAlreadyRunning } filterS := <-dns.filterSChan diff --git a/services/ers.go b/services/ers.go index e36cfbbd5..bb2ea947b 100644 --- a/services/ers.go +++ b/services/ers.go @@ -57,7 +57,7 @@ type EventReaderService struct { // Start should handle the sercive start func (erS *EventReaderService) Start() (err error) { if erS.IsRunning() { - return fmt.Errorf("service aleady running") + return utils.ErrServiceAlreadyRunning } erS.Lock() diff --git a/services/freeswitchagent.go b/services/freeswitchagent.go index 119811565..8e23235b0 100644 --- a/services/freeswitchagent.go +++ b/services/freeswitchagent.go @@ -53,7 +53,7 @@ type FreeswitchAgent struct { // Start should handle the sercive start func (fS *FreeswitchAgent) Start() (err error) { if fS.IsRunning() { - return fmt.Errorf("service aleady running") + return utils.ErrServiceAlreadyRunning } fS.Lock() diff --git a/services/httpagent.go b/services/httpagent.go index fbac3c18c..62c64b3aa 100644 --- a/services/httpagent.go +++ b/services/httpagent.go @@ -19,7 +19,6 @@ along with this program. If not, see package services import ( - "fmt" "sync" "github.com/cgrates/cgrates/agents" @@ -54,7 +53,7 @@ type HTTPAgent struct { // Start should handle the sercive start func (ha *HTTPAgent) Start() (err error) { if ha.IsRunning() { - return fmt.Errorf("service aleady running") + return utils.ErrServiceAlreadyRunning } filterS := <-ha.filterSChan diff --git a/services/kamailioagent.go b/services/kamailioagent.go index 299c45a57..d5c38b589 100644 --- a/services/kamailioagent.go +++ b/services/kamailioagent.go @@ -54,7 +54,7 @@ type KamailioAgent struct { // Start should handle the sercive start func (kam *KamailioAgent) Start() (err error) { if kam.IsRunning() { - return fmt.Errorf("service aleady running") + return utils.ErrServiceAlreadyRunning } kam.Lock() diff --git a/services/loaders.go b/services/loaders.go index ad37c5e18..4270a4fbf 100644 --- a/services/loaders.go +++ b/services/loaders.go @@ -19,7 +19,6 @@ along with this program. If not, see package services import ( - "fmt" "sync" v1 "github.com/cgrates/cgrates/apier/v1" @@ -65,7 +64,7 @@ type LoaderService struct { // Start should handle the sercive start func (ldrs *LoaderService) Start() (err error) { if ldrs.IsRunning() { - return fmt.Errorf("service aleady running") + return utils.ErrServiceAlreadyRunning } filterS := <-ldrs.filterSChan diff --git a/services/radiusagent.go b/services/radiusagent.go index b2d37a5bd..5dc663da6 100644 --- a/services/radiusagent.go +++ b/services/radiusagent.go @@ -54,7 +54,7 @@ type RadiusAgent struct { // Start should handle the sercive start func (rad *RadiusAgent) Start() (err error) { if rad.IsRunning() { - return fmt.Errorf("service aleady running") + return utils.ErrServiceAlreadyRunning } filterS := <-rad.filterSChan diff --git a/services/rals.go b/services/rals.go index 6ac8b8950..a81d67f30 100644 --- a/services/rals.go +++ b/services/rals.go @@ -19,7 +19,6 @@ along with this program. If not, see package services import ( - "fmt" "sync" v1 "github.com/cgrates/cgrates/apier/v1" @@ -62,7 +61,7 @@ type RalService struct { // For this service the start should be called from RAL Service func (rals *RalService) Start() (err error) { if rals.IsRunning() { - return fmt.Errorf("service aleady running") + return utils.ErrServiceAlreadyRunning } engine.SetRpSubjectPrefixMatching(rals.cfg.RalsCfg().RpSubjectPrefixMatching) rals.Lock() diff --git a/services/resources.go b/services/resources.go index b4d90d985..4bef97e16 100644 --- a/services/resources.go +++ b/services/resources.go @@ -64,7 +64,7 @@ type ResourceService struct { // Start should handle the sercive start func (reS *ResourceService) Start() (err error) { if reS.IsRunning() { - return fmt.Errorf("service aleady running") + return utils.ErrServiceAlreadyRunning } <-reS.cacheS.GetPrecacheChannel(utils.CacheResourceProfiles) diff --git a/services/responders.go b/services/responders.go index 0a52b8785..beef9793d 100644 --- a/services/responders.go +++ b/services/responders.go @@ -19,7 +19,6 @@ along with this program. If not, see package services import ( - "fmt" "sync" "github.com/cgrates/cgrates/config" @@ -55,7 +54,7 @@ type ResponderService struct { // For this service the start should be called from RAL Service func (resp *ResponderService) Start() (err error) { if resp.IsRunning() { - return fmt.Errorf("service aleady running") + return utils.ErrServiceAlreadyRunning } resp.Lock() diff --git a/services/schedulers.go b/services/schedulers.go index 5688b2f1c..4a03bcd56 100644 --- a/services/schedulers.go +++ b/services/schedulers.go @@ -19,7 +19,6 @@ along with this program. If not, see package services import ( - "fmt" "sync" v1 "github.com/cgrates/cgrates/apier/v1" @@ -64,7 +63,7 @@ type SchedulerService struct { // Start should handle the sercive start func (schS *SchedulerService) Start() (err error) { if schS.IsRunning() { - return fmt.Errorf("service aleady running") + return utils.ErrServiceAlreadyRunning } <-schS.cacheS.GetPrecacheChannel(utils.CacheActionPlans) // wait for ActionPlans to be cached diff --git a/services/sessions.go b/services/sessions.go index f2b1c148d..37d2cd6d2 100644 --- a/services/sessions.go +++ b/services/sessions.go @@ -67,7 +67,7 @@ type SessionService struct { // Start should handle the sercive start func (smg *SessionService) Start() (err error) { if smg.IsRunning() { - return fmt.Errorf("service aleady running") + return utils.ErrServiceAlreadyRunning } var datadb *engine.DataManager if smg.dm.IsRunning() { diff --git a/services/stats.go b/services/stats.go index 48af8755a..41898e604 100644 --- a/services/stats.go +++ b/services/stats.go @@ -63,7 +63,7 @@ type StatService struct { // Start should handle the sercive start func (sts *StatService) Start() (err error) { if sts.IsRunning() { - return fmt.Errorf("service aleady running") + return utils.ErrServiceAlreadyRunning } <-sts.cacheS.GetPrecacheChannel(utils.CacheStatQueueProfiles) diff --git a/services/stordb.go b/services/stordb.go index 72791c870..a08f6f2fb 100644 --- a/services/stordb.go +++ b/services/stordb.go @@ -51,7 +51,7 @@ type StorDBService struct { // Start should handle the sercive start func (db *StorDBService) Start() (err error) { if db.IsRunning() { - return fmt.Errorf("service aleady running") + return utils.ErrServiceAlreadyRunning } db.Lock() defer db.Unlock() diff --git a/services/thresholds.go b/services/thresholds.go index b52cd47fb..04f29d11b 100644 --- a/services/thresholds.go +++ b/services/thresholds.go @@ -61,7 +61,7 @@ type ThresholdService struct { // Start should handle the sercive start func (thrs *ThresholdService) Start() (err error) { if thrs.IsRunning() { - return fmt.Errorf("service aleady running") + return utils.ErrServiceAlreadyRunning } <-thrs.cacheS.GetPrecacheChannel(utils.CacheThresholdProfiles) diff --git a/servmanager/servmanager.go b/servmanager/servmanager.go index 5504c3738..fad7db69c 100644 --- a/servmanager/servmanager.go +++ b/servmanager/servmanager.go @@ -147,34 +147,17 @@ func (srvMngr *ServiceManager) GetConfig() *config.CGRConfig { // StartServices starts all enabled services func (srvMngr *ServiceManager) StartServices() (err error) { go srvMngr.handleReload() - for serviceName, shouldRun := range map[string]bool{ - utils.APIerSv1: srvMngr.GetConfig().ApierCfg().Enabled, - utils.APIerSv2: srvMngr.GetConfig().ApierCfg().Enabled, - utils.StorDB: srvMngr.GetConfig().RalsCfg().Enabled || srvMngr.GetConfig().CdrsCfg().Enabled, - utils.AttributeS: srvMngr.GetConfig().AttributeSCfg().Enabled, - utils.ChargerS: srvMngr.GetConfig().ChargerSCfg().Enabled, - utils.ThresholdS: srvMngr.GetConfig().ThresholdSCfg().Enabled, - utils.StatS: srvMngr.GetConfig().StatSCfg().Enabled, - utils.ResourceS: srvMngr.GetConfig().ResourceSCfg().Enabled, - utils.SupplierS: srvMngr.GetConfig().SupplierSCfg().Enabled, - utils.SchedulerS: srvMngr.GetConfig().SchedulerCfg().Enabled, - utils.RALService: srvMngr.GetConfig().RalsCfg().Enabled, - utils.CDRServer: srvMngr.GetConfig().CdrsCfg().Enabled, - utils.SessionS: srvMngr.GetConfig().SessionSCfg().Enabled, - utils.ERs: srvMngr.GetConfig().ERsCfg().Enabled, - utils.DNSAgent: srvMngr.GetConfig().DNSAgentCfg().Enabled, - utils.FreeSWITCHAgent: srvMngr.GetConfig().FsAgentCfg().Enabled, - utils.KamailioAgent: srvMngr.GetConfig().KamAgentCfg().Enabled, - utils.AsteriskAgent: srvMngr.GetConfig().AsteriskAgentCfg().Enabled, - utils.RadiusAgent: srvMngr.GetConfig().RadiusAgentCfg().Enabled, - utils.DiameterAgent: srvMngr.GetConfig().DiameterAgentCfg().Enabled, - utils.HTTPAgent: len(srvMngr.GetConfig().HttpAgentCfg()) != 0, - utils.LoaderS: srvMngr.GetConfig().LoaderCfg().Enabled(), - utils.AnalyzerS: srvMngr.GetConfig().AnalyzerSCfg().Enabled, - utils.DispatcherS: srvMngr.GetConfig().DispatcherSCfg().Enabled, - } { - if shouldRun { - go srvMngr.startService(serviceName) + for _, service := range srvMngr.subsystems { + if service.ShouldRun() && !service.IsRunning() { + go func(srv Service) { + if err := srv.Start(); err != nil { + if err == utils.ErrServiceAlreadyRunning { // in case the service was started in another gorutine + return + } + utils.Logger.Err(fmt.Sprintf("<%s> failed to start %s because: %s", utils.ServiceManager, service.ServiceName(), err)) + srvMngr.engineShutdown <- true + } + }(service) } } // startServer() @@ -341,14 +324,6 @@ func (srvMngr *ServiceManager) reloadService(srviceName string) (err error) { return } -func (srvMngr *ServiceManager) startService(srviceName string) { - srv := srvMngr.GetService(srviceName) - if err := srv.Start(); err != nil { - utils.Logger.Err(fmt.Sprintf("<%s> Failed to start %s because: %s", utils.ServiceManager, srviceName, err)) - srvMngr.engineShutdown <- true - } -} - // GetService returns the named service func (srvMngr *ServiceManager) GetService(subsystem string) (srv Service) { var has bool diff --git a/utils/errors.go b/utils/errors.go index a779098c1..6c7fee4eb 100644 --- a/utils/errors.go +++ b/utils/errors.go @@ -73,6 +73,7 @@ var ( ErrMaxIncrementsExceeded = errors.New("MAX_INCREMENTS_EXCEEDED") ErrIndexOutOfBounds = errors.New("INDEX_OUT_OF_BOUNDS") ErrWrongPath = errors.New("WRONG_PATH") + ErrServiceAlreadyRunning = fmt.Errorf("service already running") ErrMap = map[string]error{ ErrNoMoreData.Error(): ErrNoMoreData,