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)
code := m.Run()
if err := s.Close(); err != nil {
if err := s.StopAndClose(); err != nil {
fmt.Println(err)
os.Exit(1)
}

View file

@ -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)
}()
}