mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-15 05:09:54 +05:00
better shutdown
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user