diff --git a/cmd/cgr-rater/cgr-rater.go b/cmd/cgr-rater/cgr-rater.go index 34ec18859..7bf7d3341 100644 --- a/cmd/cgr-rater/cgr-rater.go +++ b/cmd/cgr-rater/cgr-rater.go @@ -310,7 +310,6 @@ func checkConfigSanity() { } func configureDatabase(db_type, host, port, name, user, pass string) (getter rater.DataStorage, err error) { - switch db_type { case REDIS: db_nb, err := strconv.Atoi(name) diff --git a/cmd/cgr-rater/registration.go b/cmd/cgr-rater/registration.go index 34b82c3d2..a6e420d4e 100644 --- a/cmd/cgr-rater/registration.go +++ b/cmd/cgr-rater/registration.go @@ -116,9 +116,9 @@ Listens for the SIGTERM, SIGINT, SIGQUIT system signals and shuts down the sessi func shutdownSessionmanagerSingnalHandler() { rater.Logger.Info("Handling stop signals...") c := make(chan os.Signal) - signal.Notify(c, syscall.SIGTERM, syscall.SIGINT, syscall.SIGQUIT) + signal.Notify(c, syscall.SIGHUP, syscall.SIGTERM, syscall.SIGINT, syscall.SIGQUIT) <-c - sm.Shutdown() + sm.Shutdown() exitChan <- true } diff --git a/rater/calldesc.go b/rater/calldesc.go index 5a00832ec..e62cfe561 100644 --- a/rater/calldesc.go +++ b/rater/calldesc.go @@ -309,6 +309,7 @@ func (cd *CallDescriptor) GetMaxSessionTime() (seconds float64, err error) { Logger.Debug(fmt.Sprintf("available sec: %v credit: %v", availableSeconds, availableCredit)) } } else { + Logger.Err(fmt.Sprintf("Could not get user balance for %s.", cd.GetUserBalanceKey())) return cd.Amount, err } // check for zero balance diff --git a/sessionmanager/fsevent.go b/sessionmanager/fsevent.go index 734f98660..1dd1ad93b 100644 --- a/sessionmanager/fsevent.go +++ b/sessionmanager/fsevent.go @@ -54,7 +54,7 @@ const ( REQTYPE_PREPAID = "prepaid" REQTYPE_POSTPAID = "postpaid" AUTH_OK = "+AUTH_OK" - DISCCONECT = "+SWITCH DISCONNECT" + DISCONNECT = "+SWITCH DISCONNECT" INSUFFICIENT_FUNDS = "-INSUFFICIENT_FUNDS" MISSING_PARAMETER = "-MISSING_PARAMETER" SYSTEM_ERROR = "-SYSTEM_ERROR" diff --git a/sessionmanager/fssessionmanager.go b/sessionmanager/fssessionmanager.go index 582519b6f..a48c5a287 100644 --- a/sessionmanager/fssessionmanager.go +++ b/sessionmanager/fssessionmanager.go @@ -71,7 +71,6 @@ func (sm *FSSessionManager) createHandlers() (handlers map[string]func(string)) sm.OnChannelAnswer(ev) } ch := func(body string) { - rater.Logger.Info("hangup!") ev := new(FSEvent).New(body) sm.OnChannelHangupComplete(ev) } @@ -100,7 +99,7 @@ func (sm *FSSessionManager) DisconnectSession(s *Session, notify string) { if err != nil { rater.Logger.Err("could not send disconect api notification to freeswitch") } - err = fsock.SendMsgCmd(s.uuid, map[string]string{"call-command": "hangup", "hangup-cause": notify}) + err = fsock.SendMsgCmd(s.uuid, map[string]string{"call-command": "hangup", "hangup-cause": "MANAGER_REQUEST"}) // without + sign if err != nil { rater.Logger.Err("could not send disconect msg to freeswitch") } @@ -108,6 +107,16 @@ func (sm *FSSessionManager) DisconnectSession(s *Session, notify string) { return } +// Remove session from sessin list +func (sm *FSSessionManager) RemoveSession(s *Session) { + for i, ss := range sm.sessions { + if ss == s { + sm.sessions = append(sm.sessions[:i], sm.sessions[i+1:]...) + return + } + } +} + // Sends the transfer command to unpark the call to freeswitch func (sm *FSSessionManager) unparkCall(uuid, call_dest_nb, notify string) { err := fsock.SendApiCmd(fmt.Sprintf("uuid_setvar %s cgr_notify %s\n\n", uuid, notify)) @@ -280,6 +289,8 @@ func (sm *FSSessionManager) OnChannelHangupComplete(ev Event) { lastCC.Cost -= cost rater.Logger.Info(fmt.Sprintf("Rambursed %v cents, %v seconds", cost, seconds)) s.SaveOperations() + s.Close() + } func (sm *FSSessionManager) LoopAction(s *Session, cd *rater.CallDescriptor) { @@ -299,7 +310,7 @@ func (sm *FSSessionManager) LoopAction(s *Session, cd *rater.CallDescriptor) { } if remainingSeconds == 0 || err != nil { rater.Logger.Info(fmt.Sprintf("No credit left: Disconnect %v", s)) - s.Disconnect() + sm.DisconnectSession(s, DISCONNECT) return } s.CallCosts = append(s.CallCosts, cc) @@ -312,9 +323,12 @@ func (sm *FSSessionManager) GetDbLogger() rater.DataStorage { } func (sm *FSSessionManager) Shutdown() (err error) { - rater.Logger.Info("Shutting down all sessions...") - for _, s := range sm.sessions { - sm.DisconnectSession(s, MANAGER_REQUEST) - } + rater.Logger.Info("Shutting down all sessions...") + fsock.SendApiCmd("hupall MANAGER_REQUEST cgr_reqtype prepaid") + fsock.SendApiCmd("hupall MANAGER_REQUEST cgr_reqtype postpaid") + for len(sm.sessions)>0 { + time.Sleep(500*time.Millisecond) + rater.Logger.Info(fmt.Sprintf("sessions: %s", sm.sessions)) + } return } diff --git a/sessionmanager/session.go b/sessionmanager/session.go index 061be8664..81e37d418 100644 --- a/sessionmanager/session.go +++ b/sessionmanager/session.go @@ -113,11 +113,7 @@ func (s *Session) Close() { } s.stopDebit <- true s.callDescriptor.TimeEnd = time.Now() -} - -// Disconects a session using session manager -func (s *Session) Disconnect() { - s.sessionManager.DisconnectSession(s, DISCCONECT) + s.sessionManager.RemoveSession(s) } // Nice print for session diff --git a/sessionmanager/sessionmanager.go b/sessionmanager/sessionmanager.go index 7947c2c53..1a6b3a788 100644 --- a/sessionmanager/sessionmanager.go +++ b/sessionmanager/sessionmanager.go @@ -26,6 +26,7 @@ import ( type SessionManager interface { Connect(address, pass string) error DisconnectSession(*Session, string) + RemoveSession(*Session) LoopAction(*Session, *rater.CallDescriptor) GetDebitPeriod() time.Duration GetDbLogger() rater.DataStorage