Skip to content

Commit

Permalink
perf: update Web App termination procedure
Browse files Browse the repository at this point in the history
  • Loading branch information
andy89923 committed May 9, 2024
1 parent 16d466e commit a4831ec
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 34 deletions.
27 changes: 11 additions & 16 deletions backend/billing/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"os"
"strconv"
"sync"
"time"

"github.com/fclairamb/ftpserver/config"
"github.com/fclairamb/ftpserver/server"
Expand All @@ -19,6 +18,7 @@ import (
type BillingDomain struct {
ftpServer *ftpserver.FtpServer
driver *server.Server
wg *sync.WaitGroup
}

type Access struct {
Expand All @@ -39,7 +39,9 @@ func OpenServer(wg *sync.WaitGroup) *BillingDomain {
// Arguments vars
confFile := "/tmp/webconsole/ftpserver.json"

b := &BillingDomain{}
b := &BillingDomain{
wg: wg,
}
if _, err := os.Stat("/tmp/webconsole"); err != nil {
if err := os.Mkdir("/tmp/webconsole", os.ModePerm); err != nil {
logger.BillingLog.Error(err)
Expand Down Expand Up @@ -105,33 +107,26 @@ func OpenServer(wg *sync.WaitGroup) *BillingDomain {
// Setting up the ftpserver logger
b.ftpServer.Logger = logger.FtpServerLog

go b.Serve(wg)
go b.Serve()
logger.BillingLog.Info("Billing server Start")

return b
}

func (b *BillingDomain) Serve(wg *sync.WaitGroup) {
defer func() {
logger.BillingLog.Error("Billing server stopped")
b.Stop()
wg.Done()
}()

func (b *BillingDomain) Serve() {
if err := b.ftpServer.ListenAndServe(); err != nil {
logger.BillingLog.Error("Problem listening ", "err", err)
}

// We wait at most 1 minutes for all clients to disconnect
if err := b.driver.WaitGracefully(time.Minute); err != nil {
logger.BillingLog.Warn("Problem stopping server", "Err", err)
}
}

func (b *BillingDomain) Stop() {
b.driver.Stop()
logger.BillingLog.Infoln("Stop BillingDomain server")

b.driver.Stop()
if err := b.ftpServer.Stop(); err != nil {
logger.BillingLog.Error("Problem stopping server", "Err", err)
}

logger.BillingLog.Infoln("BillingDomain server stopped")
b.wg.Done()
}
7 changes: 5 additions & 2 deletions backend/webui_context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/free5gc/openapi/Nnrf_NFManagement"
"github.com/free5gc/openapi/models"
"github.com/free5gc/openapi/oauth"
"github.com/free5gc/webconsole/backend/billing"
"github.com/free5gc/webconsole/backend/factory"
"github.com/free5gc/webconsole/backend/logger"
)
Expand All @@ -21,7 +20,9 @@ type WEBUIContext struct {
NfInstanceID string
NFProfiles []models.NfProfile
NFOamInstances []NfOamInstance
BillingServer *billing.BillingDomain

// is registered to NRF as AF
IsRegistered bool

NrfUri string
OAuth2Required bool
Expand All @@ -40,6 +41,8 @@ func Init() {
webuiContext.NfInstanceID = uuid.New().String()
webuiContext.NrfUri = factory.WebuiConfig.Configuration.NrfUri

webuiContext.IsRegistered = false

ManagementConfig := Nnrf_NFManagement.NewConfiguration()
ManagementConfig.SetBasePath(GetSelf().NrfUri)
webuiContext.NFManagementClient = Nnrf_NFManagement.NewAPIClient(ManagementConfig)
Expand Down
2 changes: 1 addition & 1 deletion backend/webui_context/nrf_management.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func SendNFRegistration() error {
NFInstanceIDDocumentApi.
RegisterNFInstance(context.TODO(), GetSelf().NfInstanceID, profile)
if err != nil || res == nil {
logger.ConsumerLog.Infof("Webconsole-AF register to NRF Error[%s]", err.Error())
logger.ConsumerLog.Warnf("Webconsole-AF register to NRF Error[%s]", err.Error())
time.Sleep(2 * time.Second)
retryTime += 1
if retryTime == 10 {
Expand Down
47 changes: 32 additions & 15 deletions backend/webui_service/webui_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,17 @@ import (
type WebuiApp struct {
cfg *factory.Config
webuiCtx *webui_context.WEBUIContext

wg *sync.WaitGroup

billingServer *billing.BillingDomain
}

func NewApp(cfg *factory.Config) (*WebuiApp, error) {
webui := &WebuiApp{cfg: cfg}
webui := &WebuiApp{
cfg: cfg,
wg: &sync.WaitGroup{},
}
webui.SetLogEnable(cfg.GetLogEnable())
webui.SetLogLevel(cfg.GetLogLevel())
webui.SetReportCaller(cfg.GetLogReportCaller())
Expand Down Expand Up @@ -87,6 +94,7 @@ func (a *WebuiApp) Start(tlsKeyLogPath string) {

logger.InitLog.Infoln("Server started")

a.wg.Add(1)
signalChannel := make(chan os.Signal, 1)
signal.Notify(signalChannel, os.Interrupt, syscall.SIGTERM)
go func() {
Expand All @@ -99,7 +107,7 @@ func (a *WebuiApp) Start(tlsKeyLogPath string) {

<-signalChannel
a.Terminate()
os.Exit(0)
a.wg.Done()
}()

go func() {
Expand All @@ -110,6 +118,8 @@ func (a *WebuiApp) Start(tlsKeyLogPath string) {
logger.InitLog.Errorln(retry_err)
logger.InitLog.Warningln("The registration to NRF failed, resulting in limited functionalities.")
}
} else {
a.webuiCtx.IsRegistered = true
}
}()

Expand All @@ -135,12 +145,10 @@ func (a *WebuiApp) Start(tlsKeyLogPath string) {
self := webui_context.GetSelf()
self.UpdateNfProfiles()

wg := sync.WaitGroup{}

if billingServer.Enable {
wg.Add(1)
self.BillingServer = billing.OpenServer(&wg)
if self.BillingServer == nil {
a.wg.Add(1)
a.billingServer = billing.OpenServer(a.wg)
if a.billingServer == nil {
logger.InitLog.Errorln("Billing Server open error.")
}
}
Expand All @@ -153,19 +161,28 @@ func (a *WebuiApp) Start(tlsKeyLogPath string) {
logger.InitLog.Infoln(router.Run(":5000"))
}

wg.Wait()
logger.MainLog.Infoln("wait all routine stopped")
a.wg.Wait()
}

func (a *WebuiApp) Terminate() {
logger.InitLog.Infoln("Terminating WebUI-AF...")

if a.billingServer != nil {
a.billingServer.Stop()
}

// Deregister with NRF
problemDetails, err := webui_context.SendDeregisterNFInstance()
if problemDetails != nil {
logger.InitLog.Errorf("Deregister NF instance Failed Problem[%+v]", problemDetails)
} else if err != nil {
logger.InitLog.Errorf("Deregister NF instance Error[%+v]", err)
} else {
logger.InitLog.Infof("Deregister from NRF successfully")
if a.webuiCtx.IsRegistered {
problemDetails, err := webui_context.SendDeregisterNFInstance()
if problemDetails != nil {
logger.InitLog.Errorf("Deregister NF instance Failed Problem[%+v]", problemDetails)
} else if err != nil {
logger.InitLog.Errorf("Deregister NF instance Error[%+v]", err)
} else {
logger.InitLog.Infof("Deregister from NRF successfully")
}
}

logger.InitLog.Infoln("WebUI-AF Terminated")
}

0 comments on commit a4831ec

Please sign in to comment.