From 9c7a7644ee0e604406c3f8772c1905690aad04d7 Mon Sep 17 00:00:00 2001 From: duanhf2012 Date: Wed, 1 Apr 2020 16:35:13 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0stop=E5=91=BD=E4=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- console/command.go | 14 +++++++++----- example/config/cluster/subnet/cluster.json | 12 ------------ example/main.go | 8 ++++++-- node/node.go | 22 ++++++++++++++-------- node/node_linux.go | 17 +++++++++++++++++ node/node_other.go | 1 + service/service.go | 7 +++++-- sysservice/tcpservice.go | 4 ++++ 8 files changed, 56 insertions(+), 29 deletions(-) create mode 100644 node/node_linux.go create mode 100644 node/node_other.go diff --git a/console/command.go b/console/command.go index eec9794..b940224 100644 --- a/console/command.go +++ b/console/command.go @@ -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 } diff --git a/example/config/cluster/subnet/cluster.json b/example/config/cluster/subnet/cluster.json index 7553826..1b5d92e 100644 --- a/example/config/cluster/subnet/cluster.json +++ b/example/config/cluster/subnet/cluster.json @@ -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"] } ] } \ No newline at end of file diff --git a/example/main.go b/example/main.go index 4b13be6..86322e2 100644 --- a/example/main.go +++ b/example/main.go @@ -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",¶m, func(reply *Param, err error) { + slf.AsyncCall("TestService1.RPC_Test1",¶m, 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 } diff --git a/node/node.go b/node/node.go index 661b599..2791d60 100644 --- a/node/node.go +++ b/node/node.go @@ -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 } diff --git a/node/node_linux.go b/node/node_linux.go new file mode 100644 index 0000000..3e7d4d5 --- /dev/null +++ b/node/node_linux.go @@ -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) + } +} diff --git a/node/node_other.go b/node/node_other.go new file mode 100644 index 0000000..2b4023a --- /dev/null +++ b/node/node_other.go @@ -0,0 +1 @@ +package node diff --git a/service/service.go b/service/service.go index f937b26..632c922 100644 --- a/service/service.go +++ b/service/service.go @@ -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 } diff --git a/sysservice/tcpservice.go b/sysservice/tcpservice.go index 5ae5a0b..609373d 100644 --- a/sysservice/tcpservice.go +++ b/sysservice/tcpservice.go @@ -170,3 +170,7 @@ func (slf *TcpService) Close(clientid uint64) { return } + +func (slf *TcpService) OnRelease() { + +} \ No newline at end of file