2020-10-21 04:06:19 -05:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2021-10-11 07:35:03 -05:00
|
|
|
"context"
|
2020-10-21 04:06:19 -05:00
|
|
|
"time"
|
|
|
|
|
2022-12-13 04:03:36 -06:00
|
|
|
"github.com/grafana/grafana/pkg/infra/db"
|
2020-10-21 04:06:19 -05:00
|
|
|
)
|
|
|
|
|
2021-10-11 07:35:03 -05:00
|
|
|
func (hs *HTTPServer) databaseHealthy(ctx context.Context) bool {
|
2020-10-21 04:06:19 -05:00
|
|
|
const cacheKey = "db-healthy"
|
|
|
|
|
|
|
|
if cached, found := hs.CacheService.Get(cacheKey); found {
|
|
|
|
return cached.(bool)
|
|
|
|
}
|
|
|
|
|
2022-12-13 04:03:36 -06:00
|
|
|
err := hs.SQLStore.WithDbSession(ctx, func(session *db.Session) error {
|
|
|
|
_, err := session.Exec("SELECT 1")
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
healthy := err == nil
|
2020-10-21 04:06:19 -05:00
|
|
|
|
|
|
|
hs.CacheService.Set(cacheKey, healthy, time.Second*5)
|
|
|
|
return healthy
|
|
|
|
}
|