优化command模块

This commit is contained in:
boyce
2020-07-31 20:47:28 +08:00
parent 0d547b1e83
commit 7b07d85ee5
2 changed files with 26 additions and 42 deletions

View File

@@ -2,11 +2,9 @@ package console
import ( import (
"fmt" "fmt"
"strconv"
"strings"
) )
type CommandFunctionCB func(param interface{})error type CommandFunctionCB func(args []string)error
var mapRegisterCmd map[string]CommandFunctionCB = map[string]CommandFunctionCB{} var mapRegisterCmd map[string]CommandFunctionCB = map[string]CommandFunctionCB{}
var programName string var programName string
@@ -22,36 +20,8 @@ func Run(args []string) error {
return fmt.Errorf("command not found, try `%s help` for help",args[0]) return fmt.Errorf("command not found, try `%s help` for help",args[0])
} }
switch args[1] { return fn(args)
case "start":
if len(args)<2 {
return fmt.Errorf("command not found, try `%s help` for help",args[0])
}else{
return start(fn,args[2])
} }
case "stop":
return fn(nil)
}
return fmt.Errorf("command not found, try `%s help` for help",args[0])
}
func start(fn CommandFunctionCB,param string) error {
sparam := strings.Split(param,"=")
if len(sparam) != 2 {
return fmt.Errorf("invalid option %s",param)
}
if sparam[0]!="nodeid" {
return fmt.Errorf("invalid option %s",param)
}
nodeId,err:= strconv.Atoi(sparam[1])
if err != nil {
return fmt.Errorf("invalid option %s",param)
}
return fn(nodeId)
}
func RegisterCommand(cmd string,fn CommandFunctionCB){ func RegisterCommand(cmd string,fn CommandFunctionCB){
mapRegisterCmd[cmd] = fn mapRegisterCmd[cmd] = fn

View File

@@ -11,6 +11,7 @@ import (
"os" "os"
"os/signal" "os/signal"
"strconv" "strconv"
"strings"
"syscall" "syscall"
"time" "time"
) )
@@ -99,7 +100,7 @@ func Start() {
} }
func stopNode(arg interface{}) error { func stopNode(args []string) error {
processid,err := getRunProcessPid() processid,err := getRunProcessPid()
if err != nil { if err != nil {
return err return err
@@ -109,22 +110,35 @@ func stopNode(arg interface{}) error {
return nil return nil
} }
func startNode(paramNodeId interface{}) error { func startNode(args []string) error {
//1.解析参数
param := args[2]
sparam := strings.Split(param,"=")
if len(sparam) != 2 {
return fmt.Errorf("invalid option %s",param)
}
if sparam[0]!="nodeid" {
return fmt.Errorf("invalid option %s",param)
}
nodeId,err:= strconv.Atoi(sparam[1])
if err != nil {
return fmt.Errorf("invalid option %s",param)
}
log.Release("Start running server.") log.Release("Start running server.")
//2.初始化node
initNode(nodeId)
//1.初始化node //3.运行集群
initNode(paramNodeId.(int))
//2.运行集群
cluster.GetCluster().Start() cluster.GetCluster().Start()
//3.运行service //4.运行service
service.Start() service.Start()
//4.记录进程id号 //5.记录进程id号
writeProcessPid() writeProcessPid()
//5.监听程序退出信号&性能报告 //6.监听程序退出信号&性能报告
bRun := true bRun := true
var pProfilerTicker *time.Ticker = &time.Ticker{} var pProfilerTicker *time.Ticker = &time.Ticker{}
if profilerInterval>0 { if profilerInterval>0 {
@@ -140,7 +154,7 @@ func startNode(paramNodeId interface{}) error {
} }
} }
//6.退出 //7.退出
close(closeSig) close(closeSig)
service.WaitStop() service.WaitStop()