2021-02-03 05:06:34 +01:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
2021-05-19 04:29:15 +02:00
|
|
|
"context"
|
2021-02-03 05:06:34 +01:00
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/TwinProduction/gatus/storage/store"
|
|
|
|
"github.com/TwinProduction/gatus/storage/store/memory"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
provider store.Store
|
|
|
|
|
|
|
|
// initialized keeps track of whether the storage provider was initialized
|
|
|
|
// Because store.Store is an interface, a nil check wouldn't be sufficient, so instead of doing reflection
|
|
|
|
// every single time Get is called, we'll just lazily keep track of its existence through this variable
|
|
|
|
initialized bool
|
2021-05-19 04:29:15 +02:00
|
|
|
|
|
|
|
ctx context.Context
|
|
|
|
cancelFunc context.CancelFunc
|
2021-02-03 05:06:34 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Get retrieves the storage provider
|
|
|
|
func Get() store.Store {
|
|
|
|
if !initialized {
|
|
|
|
log.Println("[storage][Get] Provider requested before it was initialized, automatically initializing")
|
|
|
|
err := Initialize(nil)
|
|
|
|
if err != nil {
|
|
|
|
panic("failed to automatically initialize store: " + err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return provider
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize instantiates the storage provider based on the Config provider
|
|
|
|
func Initialize(cfg *Config) error {
|
|
|
|
initialized = true
|
|
|
|
var err error
|
|
|
|
if cfg == nil || len(cfg.File) == 0 {
|
|
|
|
log.Println("[storage][Initialize] Creating storage provider")
|
|
|
|
provider, err = memory.NewStore("")
|
2021-02-21 01:00:47 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-02-03 05:06:34 +01:00
|
|
|
} else {
|
2021-05-19 04:29:15 +02:00
|
|
|
if cancelFunc != nil {
|
|
|
|
// Stop the active autoSave task
|
|
|
|
cancelFunc()
|
|
|
|
}
|
|
|
|
ctx, cancelFunc = context.WithCancel(context.Background())
|
2021-02-03 05:06:34 +01:00
|
|
|
log.Printf("[storage][Initialize] Creating storage provider with file=%s", cfg.File)
|
|
|
|
provider, err = memory.NewStore(cfg.File)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-05-19 04:29:15 +02:00
|
|
|
go autoSave(7*time.Minute, ctx)
|
2021-02-03 05:06:34 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2021-02-06 02:45:28 +01:00
|
|
|
|
2021-05-19 04:29:15 +02:00
|
|
|
// autoSave automatically calls the SaveFunc function of the provider at every interval
|
|
|
|
func autoSave(interval time.Duration, ctx context.Context) {
|
2021-02-06 02:45:28 +01:00
|
|
|
for {
|
2021-05-19 04:29:15 +02:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
log.Printf("[storage][autoSave] Stopping active job")
|
|
|
|
return
|
|
|
|
case <-time.After(interval):
|
|
|
|
log.Printf("[storage][autoSave] Saving")
|
|
|
|
err := provider.Save()
|
|
|
|
if err != nil {
|
|
|
|
log.Println("[storage][autoSave] Save failed:", err.Error())
|
|
|
|
}
|
2021-02-06 02:45:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|