Wait for db (#1402)

Added a loop that attempts to connect to the configured database.
This commit is contained in:
gfrancqu 2019-03-28 04:48:31 +01:00 committed by Jordan Wright
parent 5c753465d1
commit af4c8f61da

View file

@ -4,6 +4,7 @@ import (
"crypto/rand"
"fmt"
"io"
"time"
"bitbucket.org/liamstask/goose/lib/goose"
@ -17,6 +18,8 @@ import (
var db *gorm.DB
var conf *config.Config
const MaxDatabaseConnectionAttempts int = 10
const (
CampaignInProgress string = "In progress"
CampaignQueued string = "Queued"
@ -94,7 +97,20 @@ func Setup(c *config.Config) error {
return err
}
// Open our database connection
db, err = gorm.Open(conf.DBName, conf.DBPath)
i := 0
for {
db, err = gorm.Open(conf.DBName, conf.DBPath)
if err == nil {
break
}
if err != nil && i >= MaxDatabaseConnectionAttempts {
log.Error(err)
return err
}
i += 1
log.Warn("waiting for database to be up...")
time.Sleep(5 * time.Second)
}
db.LogMode(false)
db.SetLogger(log.Logger)
db.DB().SetMaxOpenConns(1)