Fix duplicate saving on quit
Slight refactor to split server stop and close functions Quit function explicitly sends SIGTERM SIGTERM doesn't trigger an os.Exit Bonus: Properly save the world on spawning the rover
This commit is contained in:
parent
141827fa57
commit
8586bdabd7
6 changed files with 28 additions and 13 deletions
|
@ -29,7 +29,7 @@ func TestMain(m *testing.M) {
|
|||
fmt.Printf("Test server hosted on %s", serv)
|
||||
code := m.Run()
|
||||
|
||||
if err := s.Close(); err != nil {
|
||||
if err := s.StopAndClose(); err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
|
|
@ -49,21 +49,17 @@ func InnerMain() {
|
|||
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
|
||||
go func() {
|
||||
<-c
|
||||
fmt.Println("SIGTERM recieved, exiting...")
|
||||
if err := s.Close(); err != nil {
|
||||
fmt.Println("Quit requested, exiting...")
|
||||
if err := s.Stop(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
os.Exit(0)
|
||||
}()
|
||||
|
||||
// Quit after a time if requested
|
||||
if *quit != 0 {
|
||||
go func() {
|
||||
time.Sleep(time.Duration(*quit) * time.Second)
|
||||
if err := s.Close(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
os.Exit(0)
|
||||
syscall.Kill(syscall.Getpid(), syscall.SIGTERM)
|
||||
}()
|
||||
}
|
||||
|
||||
|
|
|
@ -28,7 +28,7 @@ func TestMain(m *testing.M) {
|
|||
fmt.Printf("Test server hosted on %s", address)
|
||||
code := m.Run()
|
||||
|
||||
if err := s.Close(); err != nil {
|
||||
if err := s.StopAndClose(); err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
|
|
@ -129,6 +129,9 @@ func HandleSpawn(s *Server, vars map[string]string, b io.ReadCloser, w io.Writer
|
|||
} else if attribs, rover, err := s.SpawnRoverForAccount(id); err != nil {
|
||||
response.Error = err.Error()
|
||||
|
||||
} else if err := s.SaveWorld(); err != nil {
|
||||
response.Error = fmt.Sprintf("Internal server error when saving world: %s", err)
|
||||
|
||||
} else {
|
||||
fmt.Printf("New rover spawned\taccount:%s\trover:%s\tattributes:%+v\n", id, rover, attribs)
|
||||
|
||||
|
|
|
@ -164,8 +164,8 @@ func (s *Server) Run() {
|
|||
}
|
||||
}
|
||||
|
||||
// Close closes up the server
|
||||
func (s *Server) Close() error {
|
||||
// Stop will stop the current server
|
||||
func (s *Server) Stop() error {
|
||||
// Stop the cron
|
||||
s.schedule.Stop()
|
||||
|
||||
|
@ -176,6 +176,11 @@ func (s *Server) Close() error {
|
|||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Close waits until the server is finished and closes up shop
|
||||
func (s *Server) Close() error {
|
||||
// Wait until the server has shut down
|
||||
s.sync.Wait()
|
||||
|
||||
|
@ -183,6 +188,17 @@ func (s *Server) Close() error {
|
|||
return s.SaveAll()
|
||||
}
|
||||
|
||||
// Close waits until the server is finished and closes up shop
|
||||
func (s *Server) StopAndClose() error {
|
||||
// Stop the server
|
||||
if err := s.Stop(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Close and return
|
||||
return s.Close()
|
||||
}
|
||||
|
||||
// SaveWorld will save out the world file
|
||||
func (s *Server) SaveWorld() error {
|
||||
if s.persistence == PersistentData {
|
||||
|
|
|
@ -39,7 +39,7 @@ func TestServer_Run(t *testing.T) {
|
|||
|
||||
go server.Run()
|
||||
|
||||
if err := server.Close(); err != nil {
|
||||
if err := server.StopAndClose(); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
@ -54,7 +54,7 @@ func TestServer_RunPersistentData(t *testing.T) {
|
|||
|
||||
go server.Run()
|
||||
|
||||
if err := server.Close(); err != nil {
|
||||
if err := server.StopAndClose(); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue