增加stop命令

This commit is contained in:
duanhf2012
2020-04-01 16:35:13 +08:00
parent 27ff65726a
commit 9c7a7644ee
8 changed files with 56 additions and 29 deletions

View File

@@ -6,7 +6,9 @@ import (
"strings"
)
var mapRegisterCmd map[string]func(param interface{}) = map[string]func(param interface{}){}
type CommandFunctionCB func(param interface{})error
var mapRegisterCmd map[string]CommandFunctionCB = map[string]CommandFunctionCB{}
var programName string
func Run(args []string) error {
@@ -27,12 +29,14 @@ func Run(args []string) error {
}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 func(param interface{}),param string) error {
func start(fn CommandFunctionCB,param string) error {
sparam := strings.Split(param,"=")
if len(sparam) != 2 {
return fmt.Errorf("invalid option %s",param)
@@ -45,10 +49,10 @@ func start(fn func(param interface{}),param string) error {
return fmt.Errorf("invalid option %s",param)
}
fn(nodeId)
return nil
return fn(nodeId)
}
func RegisterCommand(cmd string,fn func(param interface{})){
func RegisterCommand(cmd string,fn CommandFunctionCB){
mapRegisterCmd[cmd] = fn
}

View File

@@ -6,18 +6,6 @@
"NodeName": "Node_Test1",
"remark":"//以_打头的表示只在本机进程不对整个子网开发",
"ServiceList": ["TestService1","TestService2","TestServiceCall","GateService","TcpService"]
},
{
"NodeId": 2,
"ListenAddr":"127.0.0.1:8002",
"NodeName": "Node_Gate2",
"ServiceList": ["SubNet1_Service"]
},
{
"NodeId": 3,
"ListenAddr":"127.0.0.1:8003",
"NodeName": "Node_Room",
"ServiceList": ["SubNet1_Service"]
}
]
}

View File

@@ -128,12 +128,14 @@ func (slf *TestServiceCall) Test(){
param.B = "xxxxxxxxxxxxxxxxxxxxxxx"
param.Pa = []string{"ccccc","asfsdfsdaf","bbadfsdf","ewrwefasdf","safsadfka;fksd"}
param.Index = index
slf.AsyncCall("TestService1.RPC_Test",&param, func(reply *Param, err error) {
slf.AsyncCall("TestService1.RPC_Test1",&param, func(reply *Param, err error) {
fmt.Print(reply,"\n")
})
slf.AfterFunc(time.Second*1,slf.Test)
}
func (slf *TestServiceCall) OnRelease(){
fmt.Print("OnRelease")
}
func (slf *TestServiceCall) Run(){
//var ret int
var input int = 1
@@ -163,7 +165,9 @@ func (slf *TestServiceCall) Run(){
func (slf *TestService1) RPC_Test(a *Param,b *Param) error {
//*a = *b
//a = nil
*b = *a
return nil
}

View File

@@ -12,7 +12,6 @@ import (
"os/signal"
"strconv"
"syscall"
"time"
)
var closeSig chan bool
@@ -97,27 +96,34 @@ func Start() {
}
func stopNode(processid interface{}){
func stopNode(arg interface{}) error {
processid,err := getRunProcessPid()
if err != nil {
return err
}
KillProcess(processid)
return nil
}
func startNode(paramNodeId interface{}) {
func startNode(paramNodeId interface{}) error {
initNode(paramNodeId.(int))
cluster.GetCluster().Start()
service.Start()
writeProcessPid()
for {
bRun := true
for bRun {
select {
case <-sigs:
fmt.Printf("Recv stop sig")
break
default:
time.Sleep(time.Second)
log.Debug("receipt stop signal.")
bRun = false
}
}
close(closeSig)
service.WaitStop()
return nil
}

17
node/node_linux.go Normal file
View File

@@ -0,0 +1,17 @@
// +build linux
package node
import (
"fmt"
"syscall"
)
func KillProcess(processId int){
err := syscall.Kill(processId,syscall.Signal(10))
if err != nil {
fmt.Printf("kill processid %d is fail:%+v.\n",processId,err)
}else{
fmt.Printf("kill processid %d is successful.\n",processId)
}
}

1
node/node_other.go Normal file
View File

@@ -0,0 +1 @@
package node

View File

@@ -108,6 +108,7 @@ func (slf *Service) Run() {
if bStop == true {
if atomic.AddInt32(&slf.gorouterNum,-1)<=0 {
slf.startStatus = false
slf.Release()
slf.OnRelease()
}
break
@@ -120,8 +121,7 @@ func (slf *Service) GetName() string{
return slf.name
}
func (slf *Service) OnRelease(){
func (slf *Service) Release(){
defer func() {
if r := recover(); r != nil {
buf := make([]byte, 40960)
@@ -134,6 +134,9 @@ func (slf *Service) OnRelease(){
slf.this.OnRelease()
}
func (slf *Service) OnRelease(){
}
func (slf *Service) OnInit() error {
return nil
}

View File

@@ -170,3 +170,7 @@ func (slf *TcpService) Close(clientid uint64) {
return
}
func (slf *TcpService) OnRelease() {
}