Re-factor clean exiting to be usable in the client also

This commit is contained in:
Aaron Lindsay 2013-09-03 22:21:18 -04:00
parent 4eb75e1db1
commit 2dcd08d502
3 changed files with 10 additions and 5 deletions

View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"asink"
"net" "net"
"net/http" "net/http"
"net/rpc" "net/rpc"
@ -68,7 +69,7 @@ func (u *UserModifier) RemoveUser(user *User, result *int) error {
type ServerStopper int type ServerStopper int
func (s *ServerStopper) StopServer(code *int, result *int) error { func (s *ServerStopper) StopServer(code *int, result *int) error {
Exit(*code) asink.Exit(*code)
*result = 0 *result = 0
return nil return nil
} }
@ -92,5 +93,5 @@ func StartRPC(sock string, tornDown chan int, adb *AsinkDB) {
go http.Serve(l, nil) go http.Serve(l, nil)
WaitOnExit() asink.WaitOnExit()
} }

View File

@ -29,6 +29,8 @@ func init() {
if err != nil { if err != nil {
panic(err) panic(err)
} }
asink.SetupCleanExitOnSignals()
} }
const sock_usage = "Socket to use to connect to the Asink server." const sock_usage = "Socket to use to connect to the Asink server."
@ -60,7 +62,7 @@ func StartServer(args []string) {
go http.Serve(l, nil) go http.Serve(l, nil)
//TODO handle errors from http.Serve? //TODO handle errors from http.Serve?
WaitOnExit() asink.WaitOnExit()
<-rpcTornDown <-rpcTornDown
} }

View File

@ -1,4 +1,4 @@
package main package asink
import ( import (
"os" "os"
@ -14,9 +14,11 @@ func init() {
exitWaiterCount = 0 exitWaiterCount = 0
exitWaiterChan = make(chan int) exitWaiterChan = make(chan int)
exitCalled = make(chan int) exitCalled = make(chan int)
go setupCleanExitOnSignals()
} }
func SetupCleanExitOnSignals() {
go setupCleanExitOnSignals()
}
func setupCleanExitOnSignals() { func setupCleanExitOnSignals() {
//wait to properly close the socket when we're exiting //wait to properly close the socket when we're exiting
exitCode := 0 exitCode := 0