2015-08-18 05:57:27 +05:30
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
2015-08-25 04:05:44 +05:30
|
|
|
"github.com/go-gorp/gorp"
|
2015-08-18 05:57:27 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
func NewHealthChecker(dbm *gorp.DbMap) *healthChecker {
|
|
|
|
return &healthChecker{dbMap: dbm}
|
|
|
|
}
|
|
|
|
|
|
|
|
type healthChecker struct {
|
|
|
|
dbMap *gorp.DbMap
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hc *healthChecker) Healthy() (err error) {
|
|
|
|
if err = hc.dbMap.Db.Ping(); err != nil {
|
|
|
|
err = fmt.Errorf("database error: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
num, err := hc.dbMap.SelectInt("SELECT 1")
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if num != 1 {
|
|
|
|
err = errors.New("unable to connect to database")
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|