mirror of
https://github.com/duanhf2012/origin.git
synced 2026-02-03 22:45:13 +08:00
优化command模块
This commit is contained in:
@@ -2,11 +2,9 @@ package console
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type CommandFunctionCB func(param interface{})error
|
||||
type CommandFunctionCB func(args []string)error
|
||||
|
||||
var mapRegisterCmd map[string]CommandFunctionCB = map[string]CommandFunctionCB{}
|
||||
var programName string
|
||||
@@ -22,37 +20,9 @@ func Run(args []string) error {
|
||||
return fmt.Errorf("command not found, try `%s help` for help",args[0])
|
||||
}
|
||||
|
||||
switch args[1] {
|
||||
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])
|
||||
return fn(args)
|
||||
}
|
||||
|
||||
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){
|
||||
mapRegisterCmd[cmd] = fn
|
||||
}
|
||||
|
||||
34
node/node.go
34
node/node.go
@@ -11,6 +11,7 @@ import (
|
||||
"os"
|
||||
"os/signal"
|
||||
"strconv"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
)
|
||||
@@ -99,7 +100,7 @@ func Start() {
|
||||
}
|
||||
|
||||
|
||||
func stopNode(arg interface{}) error {
|
||||
func stopNode(args []string) error {
|
||||
processid,err := getRunProcessPid()
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -109,22 +110,35 @@ func stopNode(arg interface{}) error {
|
||||
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.")
|
||||
//2.初始化node
|
||||
initNode(nodeId)
|
||||
|
||||
//1.初始化node
|
||||
initNode(paramNodeId.(int))
|
||||
|
||||
//2.运行集群
|
||||
//3.运行集群
|
||||
cluster.GetCluster().Start()
|
||||
|
||||
//3.运行service
|
||||
//4.运行service
|
||||
service.Start()
|
||||
|
||||
//4.记录进程id号
|
||||
//5.记录进程id号
|
||||
writeProcessPid()
|
||||
|
||||
//5.监听程序退出信号&性能报告
|
||||
//6.监听程序退出信号&性能报告
|
||||
bRun := true
|
||||
var pProfilerTicker *time.Ticker = &time.Ticker{}
|
||||
if profilerInterval>0 {
|
||||
@@ -140,7 +154,7 @@ func startNode(paramNodeId interface{}) error {
|
||||
}
|
||||
}
|
||||
|
||||
//6.退出
|
||||
//7.退出
|
||||
close(closeSig)
|
||||
service.WaitStop()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user