Improve health check endpoint to test database connection
This commit is contained in:
parent
c2571f9f47
commit
e3c28a6c96
4 changed files with 29 additions and 14 deletions
10
cli/cli.go
10
cli/cli.go
|
@ -126,17 +126,21 @@ func Parse() {
|
||||||
config.Opts.DatabaseMaxConns(),
|
config.Opts.DatabaseMaxConns(),
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Fatal("Unable to connect to the database: %v", err)
|
logger.Fatal("Unable to initialize database connection pool: %v", err)
|
||||||
}
|
}
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
|
|
||||||
|
store := storage.NewStorage(db)
|
||||||
|
|
||||||
|
if err := store.Ping(); err != nil {
|
||||||
|
logger.Fatal("Unable to connect to the database: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
if flagMigrate {
|
if flagMigrate {
|
||||||
database.Migrate(db)
|
database.Migrate(db)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
store := storage.NewStorage(db)
|
|
||||||
|
|
||||||
if flagResetFeedErrors {
|
if flagResetFeedErrors {
|
||||||
store.ResetFeedErrors()
|
store.ResetFeedErrors()
|
||||||
return
|
return
|
||||||
|
|
|
@ -184,6 +184,11 @@ func setupHandler(store *storage.Storage, pool *worker.Pool) *mux.Router {
|
||||||
ui.Serve(router, store, pool)
|
ui.Serve(router, store, pool)
|
||||||
|
|
||||||
router.HandleFunc("/healthcheck", func(w http.ResponseWriter, r *http.Request) {
|
router.HandleFunc("/healthcheck", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if err := store.Ping(); err != nil {
|
||||||
|
http.Error(w, "Database Connection Error", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
w.Write([]byte("OK"))
|
w.Write([]byte("OK"))
|
||||||
}).Name("healthcheck")
|
}).Name("healthcheck")
|
||||||
|
|
||||||
|
|
|
@ -79,17 +79,6 @@ func (s *Storage) CountFeeds(userID int64) int {
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// DatabaseVersion returns the version of the database which is in use
|
|
||||||
func (s *Storage) DatabaseVersion() string {
|
|
||||||
var dbVersion string
|
|
||||||
err := s.db.QueryRow(`SELECT current_setting('server_version')`).Scan(&dbVersion)
|
|
||||||
if err != nil {
|
|
||||||
return err.Error()
|
|
||||||
}
|
|
||||||
|
|
||||||
return dbVersion
|
|
||||||
}
|
|
||||||
|
|
||||||
// CountUserFeedsWithErrors returns the number of feeds with parsing errors that belong to the given user.
|
// CountUserFeedsWithErrors returns the number of feeds with parsing errors that belong to the given user.
|
||||||
func (s *Storage) CountUserFeedsWithErrors(userID int64) int {
|
func (s *Storage) CountUserFeedsWithErrors(userID int64) int {
|
||||||
pollingParsingErrorLimit := config.Opts.PollingParsingErrorLimit()
|
pollingParsingErrorLimit := config.Opts.PollingParsingErrorLimit()
|
||||||
|
|
|
@ -17,3 +17,20 @@ type Storage struct {
|
||||||
func NewStorage(db *sql.DB) *Storage {
|
func NewStorage(db *sql.DB) *Storage {
|
||||||
return &Storage{db}
|
return &Storage{db}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DatabaseVersion returns the version of the database which is in use.
|
||||||
|
func (s *Storage) DatabaseVersion() string {
|
||||||
|
var dbVersion string
|
||||||
|
err := s.db.QueryRow(`SELECT current_setting('server_version')`).Scan(&dbVersion)
|
||||||
|
if err != nil {
|
||||||
|
return err.Error()
|
||||||
|
}
|
||||||
|
|
||||||
|
return dbVersion
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ping checks if the database connection works.
|
||||||
|
func (s *Storage) Ping() error {
|
||||||
|
_, err := s.db.Exec(`SELECT true`)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue