From 75e5bd3b9807bad8d4259a6790c0ed15d42d741f Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Mon, 8 Oct 2012 18:13:28 +0300 Subject: [PATCH] shutdown session manager and close all sessions on cgr-rater exit --- cmd/cgr-rater/cgr-rater.go | 5 ++++- cmd/cgr-rater/registration.go | 13 +++++++++++++ fsock/fsock.go | 5 +++-- sessionmanager/fsevent.go | 1 + sessionmanager/fssessionmanager.go | 16 ++++++++++++++-- sessionmanager/sessionmanager.go | 4 +++- 6 files changed, 38 insertions(+), 6 deletions(-) diff --git a/cmd/cgr-rater/cgr-rater.go b/cmd/cgr-rater/cgr-rater.go index e453786be..0a913434d 100644 --- a/cmd/cgr-rater/cgr-rater.go +++ b/cmd/cgr-rater/cgr-rater.go @@ -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 { diff --git a/cmd/cgr-rater/registration.go b/cmd/cgr-rater/registration.go index 493afbc04..34b82c3d2 100644 --- a/cmd/cgr-rater/registration.go +++ b/cmd/cgr-rater/registration.go @@ -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 +} diff --git a/fsock/fsock.go b/fsock/fsock.go index 6a17fe865..716933025 100644 --- a/fsock/fsock.go +++ b/fsock/fsock.go @@ -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 diff --git a/sessionmanager/fsevent.go b/sessionmanager/fsevent.go index 179a0c071..734f98660 100644 --- a/sessionmanager/fsevent.go +++ b/sessionmanager/fsevent.go @@ -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. diff --git a/sessionmanager/fssessionmanager.go b/sessionmanager/fssessionmanager.go index 6f4a6a56e..af9b67037 100644 --- a/sessionmanager/fssessionmanager.go +++ b/sessionmanager/fssessionmanager.go @@ -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 +} diff --git a/sessionmanager/sessionmanager.go b/sessionmanager/sessionmanager.go index 0f360243f..60188cab0 100644 --- a/sessionmanager/sessionmanager.go +++ b/sessionmanager/sessionmanager.go @@ -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 }