package cmd import ( "context" "encoding/json" "fmt" "github.com/gammazero/workerpool" "github.com/spf13/cobra" "go.uber.org/zap" "os" "os/signal" "syscall" "taskExecute/config" "taskExecute/mq" "taskExecute/pkg/docker" "taskExecute/pkg/utils" "git.hpds.cc/Component/logging" ) var ( ConfigFileFlag string = "./config/config.yaml" ) func must(err error) { if err != nil { _, _ = fmt.Fprint(os.Stderr, err) os.Exit(1) } } func NewStartCmd() *cobra.Command { cmd := &cobra.Command{ Use: "start", Short: "Start hpds_web application", Run: func(cmd *cobra.Command, args []string) { var ( cfg *config.TaskExecutorConfig err error ) ctx, cancel := context.WithCancel(context.Background()) defer cancel() configFileFlag, err := cmd.Flags().GetString("c") if err != nil { fmt.Println("get local config err: ", err) return } must(err) cfg, err = config.ParseConfigByFile(configFileFlag) must(err) logger := LoadLoggerConfig(cfg.Logging) config.Cfg = cfg if len(cfg.TmpPath) > 0 { _ = os.MkdirAll(cfg.TmpPath, 0755) } config.Logger = logger //创建本地容器配置list docker.ContainerList = make([]docker.ContainerStore, 0) b := utils.PathExists(cfg.Store) if b { store, err := os.ReadFile(cfg.Store) must(err) err = json.Unmarshal(store, &docker.ContainerList) must(err) } else { f, _ := os.Create(cfg.Store) defer func() { _ = f.Close() }() } exitChannel := make(chan os.Signal) defer close(exitChannel) // 退出信号监听 go func(c chan os.Signal) { docker.SaveStore() signal.Notify(c, syscall.SIGINT, syscall.SIGTERM) }(exitChannel) //创建消息连接点 mq.MqList, err = mq.NewMqClient(cfg.Functions, cfg.Node, logger) must(err) //任务队列 config.WorkPool = workerpool.New(cfg.TaskPoolCount) 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") return cmd } func LoadLoggerConfig(opt config.LogOptions) *logging.Logger { return logging.NewLogger( logging.SetPath(opt.Path), logging.SetPrefix(opt.Prefix), logging.SetDevelopment(opt.Development), logging.SetDebugFileSuffix(opt.DebugFileSuffix), logging.SetWarnFileSuffix(opt.WarnFileSuffix), logging.SetErrorFileSuffix(opt.ErrorFileSuffix), logging.SetInfoFileSuffix(opt.InfoFileSuffix), logging.SetMaxAge(opt.MaxAge), logging.SetMaxBackups(opt.MaxBackups), logging.SetMaxSize(opt.MaxSize), logging.SetLevel(logging.LogLevel["debug"]), ) }