diff --git a/README.md b/README.md index 57eeb3a..fecbba2 100644 --- a/README.md +++ b/README.md @@ -75,7 +75,8 @@ cluster.json如下: * ServiceList:该Node将安装的服务列表 --------------- -在启动程序命令program start nodeid=1中nodeid就是根据该配置装载服务。 +在启动程序命令originserver -start nodeid=1中nodeid就是根据该配置装载服务。 +更多参数使用,请使用originserver -help查看。 service.json如下: --------------- ``` @@ -267,7 +268,7 @@ func main(){ 编译后运行结果如下: ``` -#originserver start nodeid=1 +#originserver -start nodeid=1 TestService1 OnInit. TestService2 OnInit. ``` diff --git a/console/command.go b/console/command.go index 6560479..0e4803e 100644 --- a/console/command.go +++ b/console/command.go @@ -1,28 +1,89 @@ package console import ( + "flag" "fmt" + "os" ) -type CommandFunctionCB func(args []string)error +type valueType int +type CommandFunctionCB func(args interface{}) error +var commandList []*command +const( + boolType valueType = iota + stringType valueType = iota +) + +type command struct{ + valType valueType + name string + bValue bool + strValue string + usage string + fn CommandFunctionCB +} + +func (cmd *command) execute() error{ + if cmd.valType == boolType { + return cmd.fn(cmd.bValue) + }else if cmd.valType == stringType { + return cmd.fn(cmd.strValue) + }else{ + return fmt.Errorf("unknow command type!") + } + + return nil +} + -var mapRegisterCmd map[string]CommandFunctionCB = map[string]CommandFunctionCB{} var programName string func Run(args []string) error { + flag.Parse() programName = args[0] - if len(args) <= 1 { - return fmt.Errorf("command not found, try `%s help` for help",args[0]) + if flag.NFlag() <= 0 { + return fmt.Errorf("Command input parameter error,try `%s -help` for help",args[0]) } - fn,ok := mapRegisterCmd[args[1]] - if ok == false{ - return fmt.Errorf("command not found, try `%s help` for help",args[0]) + var startCmd *command + for _,val := range commandList { + if val.name == "start" { + startCmd = val + continue + } + err := val.execute() + if err != nil { + return err + } } - return fn(args) + return startCmd.execute() } -func RegisterCommand(cmd string,fn CommandFunctionCB){ - mapRegisterCmd[cmd] = fn +func RegisterCommandBool(cmdName string, defaultValue bool, usage string,fn CommandFunctionCB){ + var cmd command + cmd.valType = boolType + cmd.name = cmdName + cmd.fn = fn + cmd.usage = usage + flag.BoolVar(&cmd.bValue, cmdName, defaultValue, usage) + commandList = append(commandList,&cmd) +} + +func RegisterCommandString(cmdName string, defaultValue string, usage string,fn CommandFunctionCB){ + var cmd command + cmd.valType = stringType + cmd.name = cmdName + cmd.fn = fn + cmd.usage = usage + flag.StringVar(&cmd.strValue, cmdName, defaultValue, usage) + commandList = append(commandList,&cmd) +} + +func PrintDefaults(){ + fmt.Fprintf(os.Stderr, "Options:\n") + + for _,val := range commandList { + fmt.Fprintf(os.Stderr, " -%-10s%10s\n",val.name,val.usage) + } } diff --git a/node/node.go b/node/node.go index a32186b..165b44b 100644 --- a/node/node.go +++ b/node/node.go @@ -8,12 +8,14 @@ import ( "github.com/duanhf2012/origin/profiler" "github.com/duanhf2012/origin/service" "io/ioutil" + "net/http" "os" "os/signal" "strconv" "strings" "syscall" "time" + _ "net/http/pprof" ) var closeSig chan bool @@ -21,13 +23,63 @@ var sigs chan os.Signal var nodeId int var preSetupService []service.IService //预安装 var profilerInterval time.Duration +var bValid bool func init() { + closeSig = make(chan bool,1) sigs = make(chan os.Signal, 3) signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM,syscall.Signal(10)) + + console.RegisterCommandBool("help",false,"This help.",usage) + console.RegisterCommandString("start","","Run originserver.",startNode) + console.RegisterCommandBool("stop",false,"Stop originserver process",stopNode) + console.RegisterCommandString("config","","Configuration file path.",setConfigPath) + console.RegisterCommandString("pprof","","Open performance analysis.",setPprof) } +func usage(val interface{}) error{ + ret := val.(bool) + if ret == false { + return nil + } + + fmt.Fprintf(os.Stderr, `orgin version: orgin/2.10.20201019 +Usage: originserver [-help] [-start node=1] [-stop] [-config path] [-pprof 0.0.0.0:6060] +`) + console.PrintDefaults() + return nil +} + +func setPprof(val interface{}) error { + listenAddr := val.(string) + if listenAddr==""{ + return nil + } + + go func(){ + err := http.ListenAndServe(listenAddr, nil) + if err != nil { + panic(fmt.Errorf("%+v",err)) + } + }() + + return nil +} + +func setConfigPath(val interface{}) error{ + configPath := val.(string) + if configPath==""{ + return nil + } + _, err := os.Stat(configPath) + if err != nil { + return fmt.Errorf("Cannot find file path %s",configPath) + } + + cluster.SetConfigDir(configPath) + return nil +} func getRunProcessPid() (int,error) { f, err := os.OpenFile(os.Args[0]+".pid", os.O_RDONLY, 0600) @@ -90,8 +142,6 @@ func initNode(id int){ } func Start() { - console.RegisterCommand("start",startNode) - console.RegisterCommand("stop",stopNode) err := console.Run(os.Args) if err!=nil { fmt.Printf("%+v\n",err) @@ -99,20 +149,28 @@ func Start() { } } +func stopNode(args interface{}) error { + isStop := args.(bool) + if isStop == false{ + return nil + } -func stopNode(args []string) error { - processid,err := getRunProcessPid() + processId,err := getRunProcessPid() if err != nil { return err } - KillProcess(processid) + KillProcess(processId) return nil } -func startNode(args []string) error { +func startNode(args interface{}) error{ //1.解析参数 - param := args[2] + param := args.(string) + if param == "" { + return nil + } + sparam := strings.Split(param,"=") if len(sparam) != 2 { return fmt.Errorf("invalid option %s",param)