Files
cgrates/services/cdrs.go
2020-01-09 08:05:20 +02:00

161 lines
4.3 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"
v1 "github.com/cgrates/cgrates/apier/v1"
v2 "github.com/cgrates/cgrates/apier/v2"
"github.com/cgrates/cgrates/config"
"github.com/cgrates/cgrates/engine"
"github.com/cgrates/cgrates/servmanager"
"github.com/cgrates/cgrates/utils"
"github.com/cgrates/rpcclient"
)
// NewCDRServer returns the CDR Server
func NewCDRServer(cfg *config.CGRConfig, dm *DataDBService,
storDB *StorDBService, filterSChan chan *engine.FilterS,
server *utils.Server, internalCDRServerChan chan rpcclient.ClientConnector,
connMgr *engine.ConnManager) servmanager.Service {
return &CDRServer{
connChan: internalCDRServerChan,
cfg: cfg,
dm: dm,
storDB: storDB,
filterSChan: filterSChan,
server: server,
connMgr: connMgr,
}
}
// CDRServer implements Service interface
type CDRServer struct {
sync.RWMutex
cfg *config.CGRConfig
dm *DataDBService
storDB *StorDBService
filterSChan chan *engine.FilterS
server *utils.Server
cdrS *engine.CDRServer
rpcv1 *v1.CDRsV1
rpcv2 *v2.CDRsV2
connChan chan rpcclient.ClientConnector
connMgr *engine.ConnManager
syncStop chan struct{}
storDBChan chan engine.StorDB
}
// Start should handle the sercive start
func (cdrS *CDRServer) Start() (err error) {
if cdrS.IsRunning() {
return fmt.Errorf("service aleady running")
}
utils.Logger.Info(fmt.Sprintf("<%s> starting <%s> subsystem", utils.CoreS, utils.CDRs))
filterS := <-cdrS.filterSChan
cdrS.filterSChan <- filterS
cdrS.Lock()
defer cdrS.Unlock()
cdrS.storDBChan = make(chan engine.StorDB, 1)
cdrS.syncStop = make(chan struct{})
cdrS.storDB.RegisterSyncChan(cdrS.storDBChan)
stordb := <-cdrS.storDBChan
cdrS.cdrS = engine.NewCDRServer(cdrS.cfg, stordb, cdrS.dm.GetDM(),
filterS, cdrS.connMgr)
utils.Logger.Info("Registering CDRS HTTP Handlers.")
cdrS.cdrS.RegisterHandlersToServer(cdrS.server)
utils.Logger.Info("Registering CDRS RPC service.")
cdrS.rpcv1 = v1.NewCDRsV1(cdrS.cdrS)
cdrS.rpcv2 = &v2.CDRsV2{CDRsV1: *cdrS.rpcv1}
cdrS.server.RpcRegister(cdrS.rpcv1)
cdrS.server.RpcRegister(cdrS.rpcv2)
// Make the cdr server available for internal communication
cdrS.server.RpcRegister(cdrS.cdrS) // register CdrServer for internal usage (TODO: refactor this)
cdrS.connChan <- cdrS.cdrS // Signal that cdrS is operational
go cdrS.sync()
return
}
// GetIntenternalChan returns the internal connection chanel
func (cdrS *CDRServer) GetIntenternalChan() (conn chan rpcclient.ClientConnector) {
return cdrS.connChan
}
// Reload handles the change of config
func (cdrS *CDRServer) Reload() (err error) {
return
}
// Shutdown stops the service
func (cdrS *CDRServer) Shutdown() (err error) {
cdrS.Lock()
close(cdrS.syncStop)
cdrS.cdrS = nil
cdrS.rpcv1 = nil
cdrS.rpcv2 = nil
<-cdrS.connChan
cdrS.Unlock()
return
}
// IsRunning returns if the service is running
func (cdrS *CDRServer) IsRunning() bool {
cdrS.RLock()
defer cdrS.RUnlock()
return cdrS != nil && cdrS.cdrS != nil
}
// ServiceName returns the service name
func (cdrS *CDRServer) ServiceName() string {
return utils.CDRServer
}
// ShouldRun returns if the service should be running
func (cdrS *CDRServer) ShouldRun() bool {
return cdrS.cfg.CdrsCfg().Enabled
}
// sync handles stordb sync
func (cdrS *CDRServer) sync() {
for {
select {
case <-cdrS.syncStop:
return
case stordb, ok := <-cdrS.storDBChan:
if !ok { // the chanel was closed by the shutdown of stordbService
return
}
cdrS.Lock()
if cdrS.cdrS != nil {
cdrS.cdrS.SetStorDB(stordb)
}
cdrS.Unlock()
}
}
}