Skip to content

Commit

Permalink
fix: terminate func
Browse files Browse the repository at this point in the history
  • Loading branch information
donald1218 committed May 1, 2024
1 parent 2076683 commit 5437fd6
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
14 changes: 13 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package main

import (
"context"
"os"
"os/signal"
"path/filepath"
"runtime/debug"
"syscall"

"github.com/urfave/cli"

Expand Down Expand Up @@ -53,14 +56,23 @@ func action(cliCtx *cli.Context) error {

logger.MainLog.Infoln("AMF version: ", version.GetVersion())

ctx, cancel := context.WithCancel(context.Background())
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM)

go func() {
<-sigCh // Wait for interrupt signal to gracefully shutdown
cancel() // Notify each goroutine and wait them stopped
}()

cfg, err := factory.ReadConfig(cliCtx.String("config"))
if err != nil {
return err
}
factory.AmfConfig = cfg

appStart, appStop := utils.InitFunc(tlsKeyLogPath)
amf, err := service.NewApp(cfg, appStart, appStop, tlsKeyLogPath)
amf, err := service.NewApp(ctx, cfg, appStart, appStop, tlsKeyLogPath)
if err != nil {
return err
}
Expand Down
21 changes: 20 additions & 1 deletion pkg/service/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"io"
"os"
"runtime/debug"
"sync"

"github.com/sirupsen/logrus"

Expand Down Expand Up @@ -31,6 +33,7 @@ type AmfApp struct {
amfCtx *amf_context.AMFContext
ctx context.Context
cancel context.CancelFunc
wg sync.WaitGroup

consumer *consumer.Consumer

Expand All @@ -42,7 +45,7 @@ func GetApp() AmfAppInterface {
return AMF
}

func NewApp(cfg *factory.Config, startFunc, terminateFunc func(*AmfApp), tlsKeyLogPath string) (*AmfApp, error) {
func NewApp(ctx context.Context, cfg *factory.Config, startFunc, terminateFunc func(*AmfApp), tlsKeyLogPath string) (*AmfApp, error) {

Check failure on line 48 in pkg/service/init.go

View workflow job for this annotation

GitHub Actions / lint (1.21)

line is 134 characters (lll)
amf := &AmfApp{
cfg: cfg,
start: startFunc,
Expand All @@ -52,6 +55,7 @@ func NewApp(cfg *factory.Config, startFunc, terminateFunc func(*AmfApp), tlsKeyL
amf.SetLogLevel(cfg.GetLogLevel())
amf.SetReportCaller(cfg.GetLogReportCaller())

amf.ctx, amf.cancel = context.WithCancel(ctx)
amf.amfCtx = amf_context.GetSelf()
amf_context.InitAmfContext(amf.amfCtx)

Expand Down Expand Up @@ -111,6 +115,8 @@ func (a *AmfApp) SetReportCaller(reportCaller bool) {
func (a *AmfApp) Start(tlsKeyLogPath string) {
logger.InitLog.Infoln("Server started")

a.wg.Add(1)
go a.listenShutdownEvent()
a.start(a)
}

Expand All @@ -137,3 +143,16 @@ func (a *AmfApp) CancelContext() context.Context {
func (a *AmfApp) Consumer() *consumer.Consumer {
return a.consumer
}

func (a *AmfApp) listenShutdownEvent() {
defer func() {
if p := recover(); p != nil {
// Print stack for panic to log. Fatalf() will let program exit.
logger.MainLog.Fatalf("panic: %v\n%s", p, string(debug.Stack()))
}
a.wg.Done()
}()

<-a.ctx.Done()
a.Terminate()
}
2 changes: 2 additions & 0 deletions pkg/utils/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,9 @@ func InitFunc(tlsKeyLogPath string) (func(a *service.AmfApp), func(a *service.Am
ngap_message.SendAMFStatusIndication(ran, unavailableGuamiList)
return true
})
ngap_service.Stop()
callback.SendAmfStatusChangeNotify((string)(models.StatusChange_UNAVAILABLE), amfSelf.ServedGuamiList)

Check failure on line 134 in pkg/utils/util.go

View workflow job for this annotation

GitHub Actions / lint (1.21)

File is not `gofumpt`-ed (gofumpt)
}

Check failure on line 135 in pkg/utils/util.go

View workflow job for this annotation

GitHub Actions / lint (1.21)

unnecessary trailing newline (whitespace)
return appStart, appStop
}

0 comments on commit 5437fd6

Please sign in to comment.