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:
Marc Di Luzio 2020-06-07 18:06:34 +01:00
parent 141827fa57
commit 8586bdabd7
6 changed files with 28 additions and 13 deletions

View file

@ -29,7 +29,7 @@ func TestMain(m *testing.M) {
fmt.Printf("Test server hosted on %s", serv) fmt.Printf("Test server hosted on %s", serv)
code := m.Run() code := m.Run()
if err := s.Close(); err != nil { if err := s.StopAndClose(); err != nil {
fmt.Println(err) fmt.Println(err)
os.Exit(1) os.Exit(1)
} }

View file

@ -49,21 +49,17 @@ func InnerMain() {
signal.Notify(c, os.Interrupt, syscall.SIGTERM) signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() { go func() {
<-c <-c
fmt.Println("SIGTERM recieved, exiting...") fmt.Println("Quit requested, exiting...")
if err := s.Close(); err != nil { if err := s.Stop(); err != nil {
panic(err) panic(err)
} }
os.Exit(0)
}() }()
// Quit after a time if requested // Quit after a time if requested
if *quit != 0 { if *quit != 0 {
go func() { go func() {
time.Sleep(time.Duration(*quit) * time.Second) time.Sleep(time.Duration(*quit) * time.Second)
if err := s.Close(); err != nil { syscall.Kill(syscall.Getpid(), syscall.SIGTERM)
panic(err)
}
os.Exit(0)
}() }()
} }

View file

@ -28,7 +28,7 @@ func TestMain(m *testing.M) {
fmt.Printf("Test server hosted on %s", address) fmt.Printf("Test server hosted on %s", address)
code := m.Run() code := m.Run()
if err := s.Close(); err != nil { if err := s.StopAndClose(); err != nil {
fmt.Println(err) fmt.Println(err)
os.Exit(1) os.Exit(1)
} }

View file

@ -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 { } else if attribs, rover, err := s.SpawnRoverForAccount(id); err != nil {
response.Error = err.Error() response.Error = err.Error()
} else if err := s.SaveWorld(); err != nil {
response.Error = fmt.Sprintf("Internal server error when saving world: %s", err)
} else { } else {
fmt.Printf("New rover spawned\taccount:%s\trover:%s\tattributes:%+v\n", id, rover, attribs) fmt.Printf("New rover spawned\taccount:%s\trover:%s\tattributes:%+v\n", id, rover, attribs)

View file

@ -164,8 +164,8 @@ func (s *Server) Run() {
} }
} }
// Close closes up the server // Stop will stop the current server
func (s *Server) Close() error { func (s *Server) Stop() error {
// Stop the cron // Stop the cron
s.schedule.Stop() s.schedule.Stop()
@ -176,6 +176,11 @@ func (s *Server) Close() error {
return err 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 // Wait until the server has shut down
s.sync.Wait() s.sync.Wait()
@ -183,6 +188,17 @@ func (s *Server) Close() error {
return s.SaveAll() 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 // SaveWorld will save out the world file
func (s *Server) SaveWorld() error { func (s *Server) SaveWorld() error {
if s.persistence == PersistentData { if s.persistence == PersistentData {

View file

@ -39,7 +39,7 @@ func TestServer_Run(t *testing.T) {
go server.Run() go server.Run()
if err := server.Close(); err != nil { if err := server.StopAndClose(); err != nil {
t.Error(err) t.Error(err)
} }
} }
@ -54,7 +54,7 @@ func TestServer_RunPersistentData(t *testing.T) {
go server.Run() go server.Run()
if err := server.Close(); err != nil { if err := server.StopAndClose(); err != nil {
t.Error(err) t.Error(err)
} }
} }