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

View File

@ -42,7 +42,19 @@ func Initialize(cfg *Config) error {
if err != nil {
return err
}
go provider.(*memory.Store).AutoSave(7 * time.Minute)
go autoSave(7 * time.Minute)
}
return nil
}
// autoSave automatically calls the Save function of the provider at every interval
func autoSave(interval time.Duration) {
for {
time.Sleep(interval)
log.Printf("[storage][autoSave] Saving")
err := provider.Save()
if err != nil {
log.Println("[storage][autoSave] Save failed:", err.Error())
}
}
}