Files
cgrates/services/thresholds.go
ionutboangiu da2052e7b3 Implement FilterService
And use ServiceIndexer to sync with it
2024-12-12 11:55:14 +01:00

162 lines
5.5 KiB
Go

/*
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 <http://www.gnu.org/licenses/>
*/
package services
import (
"fmt"
"sync"
"github.com/cgrates/birpc"
"github.com/cgrates/birpc/context"
"github.com/cgrates/cgrates/commonlisteners"
"github.com/cgrates/cgrates/config"
"github.com/cgrates/cgrates/engine"
"github.com/cgrates/cgrates/servmanager"
"github.com/cgrates/cgrates/utils"
)
// NewThresholdService returns the Threshold Service
func NewThresholdService(cfg *config.CGRConfig,
connMgr *engine.ConnManager,
srvDep map[string]*sync.WaitGroup,
srvIndexer *servmanager.ServiceIndexer) servmanager.Service {
return &ThresholdService{
cfg: cfg,
srvDep: srvDep,
connMgr: connMgr,
srvIndexer: srvIndexer,
stateDeps: NewStateDependencies([]string{utils.StateServiceUP}),
}
}
// ThresholdService implements Service interface
type ThresholdService struct {
sync.RWMutex
thrs *engine.ThresholdS
cl *commonlisteners.CommonListenerS
connMgr *engine.ConnManager
cfg *config.CGRConfig
srvDep map[string]*sync.WaitGroup
intRPCconn birpc.ClientConnector // expose API methods over internal connection
srvIndexer *servmanager.ServiceIndexer // access directly services from here
stateDeps *StateDependencies // channel subscriptions for state changes
}
// Start should handle the sercive start
func (thrs *ThresholdService) Start(ctx *context.Context, _ context.CancelFunc) (err error) {
if thrs.IsRunning() {
return utils.ErrServiceAlreadyRunning
}
thrs.srvDep[utils.DataDB].Add(1)
cls := thrs.srvIndexer.GetService(utils.CommonListenerS).(*CommonListenerService)
if utils.StructChanTimeout(cls.StateChan(utils.StateServiceUP), thrs.cfg.GeneralCfg().ConnectTimeout) {
return utils.NewServiceStateTimeoutError(utils.ThresholdS, utils.CommonListenerS, utils.StateServiceUP)
}
thrs.cl = cls.CLS()
cacheS := thrs.srvIndexer.GetService(utils.CacheS).(*CacheService)
if utils.StructChanTimeout(cacheS.StateChan(utils.StateServiceUP), thrs.cfg.GeneralCfg().ConnectTimeout) {
return utils.NewServiceStateTimeoutError(utils.ThresholdS, utils.CacheS, utils.StateServiceUP)
}
if err = cacheS.WaitToPrecache(ctx,
utils.CacheThresholdProfiles,
utils.CacheThresholds,
utils.CacheThresholdFilterIndexes); err != nil {
return
}
fs := thrs.srvIndexer.GetService(utils.FilterS).(*FilterService)
if utils.StructChanTimeout(fs.StateChan(utils.StateServiceUP), thrs.cfg.GeneralCfg().ConnectTimeout) {
return utils.NewServiceStateTimeoutError(utils.ThresholdS, utils.FilterS, utils.StateServiceUP)
}
dbs := thrs.srvIndexer.GetService(utils.DataDB).(*DataDBService)
if utils.StructChanTimeout(dbs.StateChan(utils.StateServiceUP), thrs.cfg.GeneralCfg().ConnectTimeout) {
return utils.NewServiceStateTimeoutError(utils.ThresholdS, utils.DataDB, utils.StateServiceUP)
}
anz := thrs.srvIndexer.GetService(utils.AnalyzerS).(*AnalyzerService)
if utils.StructChanTimeout(anz.StateChan(utils.StateServiceUP), thrs.cfg.GeneralCfg().ConnectTimeout) {
return utils.NewServiceStateTimeoutError(utils.ThresholdS, utils.AnalyzerS, utils.StateServiceUP)
}
thrs.Lock()
defer thrs.Unlock()
thrs.thrs = engine.NewThresholdService(dbs.DataManager(), thrs.cfg, fs.FilterS(), thrs.connMgr)
utils.Logger.Info(fmt.Sprintf("<%s> starting <%s> subsystem", utils.CoreS, utils.ThresholdS))
thrs.thrs.StartLoop(ctx)
srv, _ := engine.NewService(thrs.thrs)
// srv, _ := birpc.NewService(apis.NewThresholdSv1(thrs.thrs), "", false)
if !thrs.cfg.DispatcherSCfg().Enabled {
for _, s := range srv {
thrs.cl.RpcRegister(s)
}
}
thrs.intRPCconn = anz.GetInternalCodec(srv, utils.ThresholdS)
close(thrs.stateDeps.StateChan(utils.StateServiceUP))
return
}
// Reload handles the change of config
func (thrs *ThresholdService) Reload(ctx *context.Context, _ context.CancelFunc) (_ error) {
thrs.Lock()
thrs.thrs.Reload(ctx)
thrs.Unlock()
return
}
// Shutdown stops the service
func (thrs *ThresholdService) Shutdown() (_ error) {
defer thrs.srvDep[utils.DataDB].Done()
thrs.Lock()
defer thrs.Unlock()
thrs.thrs.Shutdown(context.TODO())
thrs.thrs = nil
thrs.cl.RpcUnregisterName(utils.ThresholdSv1)
return
}
// IsRunning returns if the service is running
func (thrs *ThresholdService) IsRunning() bool {
thrs.RLock()
defer thrs.RUnlock()
return thrs.thrs != nil
}
// ServiceName returns the service name
func (thrs *ThresholdService) ServiceName() string {
return utils.ThresholdS
}
// ShouldRun returns if the service should be running
func (thrs *ThresholdService) ShouldRun() bool {
return thrs.cfg.ThresholdSCfg().Enabled
}
// StateChan returns signaling channel of specific state
func (thrs *ThresholdService) StateChan(stateID string) chan struct{} {
return thrs.stateDeps.StateChan(stateID)
}
// IntRPCConn returns the internal connection used by RPCClient
func (thrs *ThresholdService) IntRPCConn() birpc.ClientConnector {
return thrs.intRPCconn
}