Skip to content

Commit

Permalink
Merge pull request #96 from andy89923/perf/web-app
Browse files Browse the repository at this point in the history
perf: update Web App termination procedure
  • Loading branch information
ianchen0119 authored May 16, 2024
2 parents 29acb89 + f4172da commit f3f91f2
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 43 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/commit-msg-check.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
name: 'Commit Message Check'

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

Expand Down
28 changes: 28 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
GO_BIN_PATH = bin

WEBCONSOLE = webconsole

WEBCONSOLE_GO_FILES = $(shell find -name "*.go" ! -name "*_test.go")
WEBCONSOLE_JS_FILES = $(shell find ./frontend -name '*.tsx' ! -path "*/node_modules/*")
WEBCONSOLE_FRONTEND = ./public

debug: GCFLAGS += -N -l

$(WEBCONSOLE): $(GO_BIN_PATH)/$(WEBCONSOLE) $(WEBCONSOLE_FRONTEND)

$(GO_BIN_PATH)/$(WEBCONSOLE): server.go $(WEBCONSOLE_GO_FILES)
@echo "Start building $(@F)...."
CGO_ENABLED=0 go build -ldflags "$(WEBCONSOLE_LDFLAGS)" -o $@ ./server.go

$(WEBCONSOLE_FRONTEND): $(WEBCONSOLE_JS_FILES)
@echo "Start building $(@F) frontend...."
cd frontend && \
sudo corepack enable && \
yarn install && \
yarn build && \
rm -rf ../public && \
cp -R build ../public

clean:
rm -rf $(GO_BIN_PATH)/$(WEBCONSOLE)
rm -rf public
5 changes: 4 additions & 1 deletion backend/WebUI/api_charging.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,10 @@ func GetChargingRecord(c *gin.Context) {
}

for rg, du := range ratingGroupDataUsages {
filter := bson.M{"ratingGroup": rg}
filter := bson.M{
"ueId": supi,
"ratingGroup": rg,
}
chargingDataInterface, err := mongoapi.RestfulAPIGetOne(chargingDataColl, filter)
if err != nil {
logger.ProcLog.Errorf("PostSubscriberByID err: %+v", err)
Expand Down
39 changes: 22 additions & 17 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 @@ -28,18 +28,27 @@ type Access struct {
Params map[string]string `json:"params"`
}

type PortRange struct {
Start int `json:"start"`
End int `json:"end"`
}

type FtpConfig struct {
Version int `json:"version"`
Accesses []Access `json:"accesses"`
Listen_address string `json:"listen_address"`

Passive_transfer_port_range PortRange `json:"passive_transfer_port_range"`
}

// The ftp server is for CDR Push method, that is the CHF will send the CDR file to the FTP server
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 @@ -68,7 +77,10 @@ func OpenServer(wg *sync.WaitGroup) *BillingDomain {
Params: params,
},
},

Passive_transfer_port_range: PortRange{
Start: 2123,
End: 2130,
},
Listen_address: addr,
}

Expand Down Expand Up @@ -105,33 +117,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
78 changes: 60 additions & 18 deletions backend/webui_service/webui_init.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package webui_service

import (
"context"
"io"
"net/http"
"os"
"os/signal"
"runtime/debug"
"sync"
"syscall"
"time"

"github.com/gin-contrib/cors"
"github.com/sirupsen/logrus"
Expand All @@ -22,10 +25,17 @@ import (
type WebuiApp struct {
cfg *factory.Config
webuiCtx *webui_context.WEBUIContext

wg *sync.WaitGroup
server *http.Server
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 +97,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 +110,7 @@ func (a *WebuiApp) Start(tlsKeyLogPath string) {

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

go func() {
Expand All @@ -110,6 +121,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,37 +148,66 @@ 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.")
}
}

router.NoRoute(ReturnPublic())

var addr string
if webServer != nil {
logger.InitLog.Infoln(router.Run(webServer.IP + ":" + webServer.PORT))
addr = webServer.IP + ":" + webServer.PORT
} else {
logger.InitLog.Infoln(router.Run(":5000"))
addr = ":5000"
}

wg.Wait()
a.server = &http.Server{
Addr: addr,
Handler: router,
}
go func() {
logger.MainLog.Infof("Http server listening on %+v", addr)
if err := a.server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
logger.MainLog.Fatalf("listen: %s\n", err)
}
}()

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

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

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

if a.server != nil {
logger.MainLog.Infoln("stopping HTTP server")

ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
if err := a.server.Shutdown(ctx); err != nil {
logger.MainLog.Fatal("HTTP server forced to shutdown: ", err)
}
}

// 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.MainLog.Infoln("WebUI-AF Terminated")
}
4 changes: 2 additions & 2 deletions config/webuicfg.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ configuration:
billingServer:
enable: true
hostIPv4: 127.0.0.1
listenPort: 2122
port: 2121
listenPort: 2121
port: 2122
tls:
pem: cert/chf.pem
key: cert/chf.key
Expand Down

0 comments on commit f3f91f2

Please sign in to comment.