修改退出卡死的bug

This commit is contained in:
wangjian 2023-03-13 10:30:50 +08:00
parent 45b9192571
commit b98cda7c10
1 changed files with 20 additions and 2 deletions

View File

@ -1,6 +1,7 @@
package cmd package cmd
import ( import (
"context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -21,7 +22,7 @@ var (
func must(err error) { func must(err error) {
if err != nil { if err != nil {
fmt.Fprint(os.Stderr, err) _, _ = fmt.Fprint(os.Stderr, err)
os.Exit(1) os.Exit(1)
} }
} }
@ -34,6 +35,9 @@ func NewStartCmd() *cobra.Command {
cfg *config.StreamFuncCaptureDBConfig cfg *config.StreamFuncCaptureDBConfig
err error err error
) )
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
configFileFlag, err := cmd.Flags().GetString("c") configFileFlag, err := cmd.Flags().GetString("c")
if err != nil { if err != nil {
fmt.Println("get local config err: ", err) fmt.Println("get local config err: ", err)
@ -42,6 +46,7 @@ func NewStartCmd() *cobra.Command {
must(err) must(err)
cfg, err = config.ParseConfigByFile(configFileFlag) cfg, err = config.ParseConfigByFile(configFileFlag)
must(err) must(err)
logger := LoadLoggerConfig(cfg.Logging)
//连接数据库 //连接数据库
model.New(cfg.Db.DriveName, cfg.Db.Conn) model.New(cfg.Db.DriveName, cfg.Db.Conn)
@ -61,7 +66,20 @@ func NewStartCmd() *cobra.Command {
_ = sf.SetHandler(handler) _ = sf.SetHandler(handler)
// start // start
_ = sf.Connect() _ = sf.Connect()
select {} for {
select {
case <-ctx.Done():
logger.With(
zap.String("web", "exit"),
).Error(ctx.Err().Error())
return
case errs := <-exitChannel:
logger.With(
zap.String("web", "服务退出"),
).Info(errs.String())
return
}
}
}, },
} }
cmd.Flags().StringVar(&ConfigFileFlag, "c", "./config/config.yaml", "The configuration file path") cmd.Flags().StringVar(&ConfigFileFlag, "c", "./config/config.yaml", "The configuration file path")