gitee.com/quant1x/engine@v1.8.4/command/command_service.go (about) 1 package command 2 3 import ( 4 "fmt" 5 "gitee.com/quant1x/engine/cache" 6 "gitee.com/quant1x/engine/services" 7 "gitee.com/quant1x/gox/daemon" 8 "gitee.com/quant1x/gox/logger" 9 cmder "github.com/spf13/cobra" 10 "os" 11 "runtime" 12 "strings" 13 ) 14 15 const ( 16 serviceCommand = "service" 17 ) 18 19 var ( 20 // CmdService 守护进程 21 CmdService *cmder.Command 22 ) 23 24 // FirstUpper 字符串首字母大写 25 func FirstUpper(s string) string { 26 if s == "" { 27 return "" 28 } 29 return strings.ToUpper(s[:1]) + s[1:] 30 } 31 32 // FirstLower 字符串首字母小写 33 func FirstLower(s string) string { 34 if s == "" { 35 return "" 36 } 37 return strings.ToLower(s[:1]) + s[1:] 38 } 39 40 func initService() { 41 CmdService = &cmder.Command{ 42 Use: serviceCommand, 43 Example: Application + " " + serviceCommand + " install | uninstall | remove | start | stop | list | status", 44 Short: "守护进程/服务", 45 //Args: cobra.MinimumNArgs(1), 46 Run: func(cmd *cmder.Command, args []string) { 47 if len(args) > 0 { 48 serviceSubCommand = args[0] 49 } 50 //logger.Infof("serviceCommand:%s", serviceCommand) 51 daemonKind := daemon.SystemDaemon 52 applicationName, _, _ := strings.Cut(Application, ".") 53 serviceName := applicationName 54 switch runtime.GOOS { 55 case "darwin": 56 daemonKind = daemon.UserAgent 57 case "windows": 58 //serviceName = "Quant1X-Stock" 59 serviceName = "Quant1X-" + FirstUpper(applicationName) 60 serviceDescription += " V" + MinVersion 61 } 62 srv, err := daemon.New(serviceName, serviceDescription, daemonKind) 63 if err != nil { 64 logger.Errorf("Error: %+v", err) 65 fmt.Println("Error: ", err) 66 os.Exit(1) 67 } 68 service := &Service{srv} 69 replacer := strings.NewReplacer("${ROOT_PATH}", cache.GetRootPath(), "${LOG_PATH}", cache.GetLoggerPath()) 70 properties := replacer.Replace(propertyList) 71 _ = service.daemon.SetTemplate(properties) 72 status, err := service.Manage() 73 if err != nil { 74 logger.Errorf("Error: %+v", err) 75 fmt.Println(status, "\nError: ", err) 76 os.Exit(1) 77 } 78 fmt.Println(status) 79 }, 80 } 81 } 82 83 var ( 84 serviceDescription = "Quant1X量化系统数据服务" 85 serviceSubCommand = "" // 守护进程维护指令 86 serviceProgramArguments = "service" 87 ) 88 89 // Service is the daemon service struct 90 type Service struct { 91 daemon daemon.Daemon 92 } 93 94 func (service *Service) Start() { 95 // TODO: 启动服务需要做的事情 96 } 97 98 func (service *Service) Stop() { 99 // TODO: 关闭服务需要做的事情 100 } 101 102 func (service *Service) Run() { 103 // 运行服务 104 services.DaemonService() 105 } 106 107 // Manage by daemon commands or run the daemon 108 func (service *Service) Manage() (string, error) { 109 if len(serviceSubCommand) > 1 { 110 switch serviceSubCommand { 111 case "install": 112 return service.daemon.Install(serviceProgramArguments) 113 case "remove", "uninstall": 114 return service.daemon.Remove() 115 case "start": 116 return service.daemon.Start() 117 case "stop": 118 // No need to explicitly stop cron since job will be killed 119 return service.daemon.Stop() 120 case "list": 121 services.PrintJobList() 122 return "", nil 123 case "status": 124 return service.daemon.Status() 125 default: 126 //return usage, nil 127 } 128 } 129 // serviceCommand = service 130 return service.daemon.Run(service) 131 }