Browse Source

Wait for db (#1402)

Added a loop that attempts to connect to the configured database.
pull/1125/merge
gfrancqu 2 years ago
committed by Jordan Wright
parent
commit
af4c8f61da
1 changed files with 17 additions and 1 deletions
  1. +17
    -1
      models/models.go

+ 17
- 1
models/models.go 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)

Loading…
Cancel
Save