X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=pkg%2Fxapp%2Fxapp.go;h=6c7c5673658639de2ac75c89742b853e76aadcea;hb=f22b458846a20a4a9fcafb49e3195ab44a16840e;hp=0ae9b02c0e51a8c017bda4f0d1124b0e5818bdb7;hpb=47faec8c255f19f51e22c2fec203e6ed513c791f;p=ric-plt%2Fxapp-frame.git diff --git a/pkg/xapp/xapp.go b/pkg/xapp/xapp.go index 0ae9b02..6c7c567 100755 --- a/pkg/xapp/xapp.go +++ b/pkg/xapp/xapp.go @@ -23,9 +23,15 @@ import ( "fmt" "github.com/spf13/viper" "net/http" + "os" + "os/signal" + "sync/atomic" + "syscall" + "time" ) type ReadyCB func(interface{}) +type ShutdownCB func() var ( // XApp is an application instance @@ -40,6 +46,9 @@ var ( Alarm *AlarmClient readyCb ReadyCB readyCbParams interface{} + shutdownCb ShutdownCB + shutdownFlag int32 + shutdownCnt int32 ) func IsReady() bool { @@ -53,12 +62,15 @@ func SetReadyCB(cb ReadyCB, params interface{}) { func xappReadyCb(params interface{}) { Alarm = NewAlarmClient(viper.GetString("alarm.MOId"), viper.GetString("alarm.APPId")) - if readyCb != nil { readyCb(readyCbParams) } } +func SetShutdownCB(cb ShutdownCB) { + shutdownCb = cb +} + func init() { // Load xapp configuration Logger = LoadConfig() @@ -80,6 +92,52 @@ func init() { } else { Sdl = NewSDLClient(viper.GetString("db.namespace")) } + + // + // Signal handlers to really exit program. + // shutdownCb can hang until application has + // made all needed gracefull shutdown actions + // hardcoded limit for shutdown is 20 seconds + // + interrupt := make(chan os.Signal, 1) + signal.Notify(interrupt, syscall.SIGINT, syscall.SIGTERM) + //signal handler function + go func() { + for _ = range interrupt { + if atomic.CompareAndSwapInt32(&shutdownFlag, 0, 1) { + // close function + go func() { + timeout := int(20) + sentry := make(chan struct{}) + defer close(sentry) + + // close callback + go func() { + if shutdownCb != nil { + shutdownCb() + } + sentry <- struct{}{} + }() + select { + case <-time.After(time.Duration(timeout) * time.Second): + Logger.Info("xapp-frame shutdown callback took more than %d seconds", timeout) + case <-sentry: + Logger.Info("xapp-frame shutdown callback handled within %d seconds", timeout) + } + os.Exit(0) + }() + } else { + newCnt := atomic.AddInt32(&shutdownCnt, 1) + Logger.Info("xapp-frame shutdown already ongoing. Forced exit counter %d/%d ", newCnt, 5) + if newCnt >= 5 { + Logger.Info("xapp-frame shutdown forced exit") + os.Exit(0) + } + continue + } + + } + }() } func RunWithParams(c MessageConsumer, sdlcheck bool) {