Implement graceful shutdown

- Shutdown the HTTP server before exiting
- Persist data to store before exiting, if applicable
This commit is contained in:
TwinProduction
2021-02-05 20:45:28 -05:00
parent 8698736e7d
commit 8e2a2c4dbc
7 changed files with 79 additions and 17 deletions

22
main.go
View File

@ -1,17 +1,37 @@
package main
import (
"log"
"os"
"os/signal"
"syscall"
"github.com/TwinProduction/gatus/config"
"github.com/TwinProduction/gatus/controller"
"github.com/TwinProduction/gatus/storage"
"github.com/TwinProduction/gatus/watchdog"
)
func main() {
cfg := loadConfiguration()
go watchdog.Monitor(cfg)
controller.Handle()
go controller.Handle()
// Wait for termination signal
sig := make(chan os.Signal, 1)
done := make(chan bool, 1)
signal.Notify(sig, os.Interrupt, os.Kill, syscall.SIGTERM)
go func() {
<-sig
log.Println("Received interruption signal, attempting to gracefully shut down")
controller.Shutdown()
err := storage.Get().Save()
if err != nil {
log.Println("Failed to save storage provider:", err.Error())
}
done <- true
}()
<-done
log.Println("Shutting down")
}
func loadConfiguration() *config.Config {