mirror of
https://github.com/duanhf2012/origin.git
synced 2026-02-04 06:54:45 +08:00
优化command模块
This commit is contained in:
@@ -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,37 +20,9 @@ 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
|
||||||
}
|
}
|
||||||
|
|||||||
34
node/node.go
34
node/node.go
@@ -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()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user