shutdown session manager and close all sessions on cgr-rater exit

This commit is contained in:
Radu Ioan Fericean
2012-10-08 18:13:28 +03:00
parent ef900e2795
commit 75e5bd3b98
6 changed files with 38 additions and 6 deletions

View File

@@ -108,6 +108,7 @@ var (
bal = balancer2go.NewBalancer()
exitChan = make(chan bool)
sm sessionmanager.SessionManager
)
// this function will reset to zero values the variables that are not present
@@ -256,7 +257,7 @@ func startSessionManager(responder *rater.Responder, loggerDb rater.DataStorage)
switch sm_switch_type {
case FS:
dp, _ := time.ParseDuration(fmt.Sprintf("%vs", sm_debit_period))
sm := sessionmanager.NewFSSessionManager(loggerDb, connector, dp)
sm = sessionmanager.NewFSSessionManager(loggerDb, connector, dp)
sm.Connect(freeswitch_server, freeswitch_pass)
default:
rater.Logger.Err(fmt.Sprintf("Cannot start session manger of type: %s!", sm_switch_type))
@@ -411,6 +412,8 @@ func main() {
if sm_enabled {
rater.Logger.Info("Starting CGRateS session manager.")
go startSessionManager(responder, loggerDb)
// close all sessions on shutdown
go shutdownSessionmanagerSingnalHandler()
}
if mediator_enabled {

View File

@@ -109,3 +109,16 @@ func reloadSchedulerSingnalHandler(sched *scheduler.Scheduler, getter rater.Data
sched.Restart()
}
}
/*
Listens for the SIGTERM, SIGINT, SIGQUIT system signals and shuts down the session manager.
*/
func shutdownSessionmanagerSingnalHandler() {
rater.Logger.Info("Handling stop signals...")
c := make(chan os.Signal)
signal.Notify(c, syscall.SIGTERM, syscall.SIGINT, syscall.SIGQUIT)
<-c
sm.Shutdown()
exitChan <- true
}

View File

@@ -155,10 +155,11 @@ func Connected() bool {
}
// Disconnects from socket
func Disconnect() {
func Disconnect() (err error) {
if fs.conn != nil {
fs.conn.Close()
err = fs.conn.Close()
}
return
}
// Auth to FS

View File

@@ -58,6 +58,7 @@ const (
INSUFFICIENT_FUNDS = "-INSUFFICIENT_FUNDS"
MISSING_PARAMETER = "-MISSING_PARAMETER"
SYSTEM_ERROR = "-SYSTEM_ERROR"
MANAGER_REQUEST = "+MANAGER_REQUEST"
)
// Nice printing for the event object.

View File

@@ -94,9 +94,10 @@ func (sm *FSSessionManager) GetSession(uuid string) *Session {
}
// Disconnects a session by sending hangup command to freeswitch
func (sm *FSSessionManager) DisconnectSession(s *Session, notify string) {
fsock.Disconnect()
func (sm *FSSessionManager) DisconnectSession(s *Session, notify string) error {
err := fsock.Disconnect()
s.Close()
return err
}
// Sends the transfer command to unpark the call to freeswitch
@@ -294,3 +295,14 @@ func (sm *FSSessionManager) GetDebitPeriod() time.Duration {
func (sm *FSSessionManager) GetDbLogger() rater.DataStorage {
return sm.loggerDB
}
func (sm *FSSessionManager) Shutdown() (err error) {
rater.Logger.Info("Shutting down all sessions...")
for _, s := range sm.sessions {
err = sm.DisconnectSession(s, MANAGER_REQUEST)
if err != nil {
return
}
}
return
}

View File

@@ -24,8 +24,10 @@ import (
)
type SessionManager interface {
DisconnectSession(*Session, string)
Connect(address, pass string) error
DisconnectSession(*Session, string) error
LoopAction(*Session, *rater.CallDescriptor)
GetDebitPeriod() time.Duration
GetDbLogger() rater.DataStorage
Shutdown() error
}