refactor of db init/checking
This commit is contained in:
parent
5ac077030d
commit
074d5ac97c
1 changed files with 36 additions and 30 deletions
66
web/web.go
66
web/web.go
|
@ -123,47 +123,48 @@ func (web *Web) initDB() {
|
||||||
|
|
||||||
conf := &gorm.Config{}
|
conf := &gorm.Config{}
|
||||||
conf.PrepareStmt = true
|
conf.PrepareStmt = true
|
||||||
|
var dialector gorm.Dialector
|
||||||
var db *gorm.DB
|
|
||||||
var err error
|
|
||||||
if strings.HasPrefix(dsn, "sqlserver") {
|
if strings.HasPrefix(dsn, "sqlserver") {
|
||||||
db, err = gorm.Open(sqlserver.Open(dsn), conf)
|
dialector = sqlserver.Open(dsn)
|
||||||
log.Println("Using SQLServer database")
|
|
||||||
} else if strings.HasPrefix(dsn, "postgres") {
|
} else if strings.HasPrefix(dsn, "postgres") {
|
||||||
db, err = gorm.Open(postgres.Open(dsn), conf)
|
dialector = postgres.Open(dsn)
|
||||||
log.Println("Using PostgreSQL database")
|
|
||||||
} else if strings.HasPrefix(dsn, "mysql") {
|
} else if strings.HasPrefix(dsn, "mysql") {
|
||||||
db, err = gorm.Open(mysql.Open(dsn), conf)
|
dialector = mysql.Open(dsn)
|
||||||
log.Println("Using MySQL database")
|
|
||||||
} else {
|
} else {
|
||||||
db, err = gorm.Open(sqlite.Open(dsn), conf)
|
dialector = sqlite.Open(dsn)
|
||||||
log.Println("Using sqlite database at:", dsn)
|
|
||||||
}
|
|
||||||
if db == nil {
|
|
||||||
log.Panicln("Could not recognize database.dsn: ", dsn)
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
log.Panicln("Could not start DB: ", err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// retry connection to the db a couple times with exp retry time
|
// retry connection to the db a couple times with exp retry time
|
||||||
web.db = db
|
var err error
|
||||||
delay := time.Duration(1) * time.Second
|
delay := time.Duration(1) * time.Second
|
||||||
maxDelay := time.Duration(8) * time.Second
|
maxDelay := time.Duration(32) * time.Second
|
||||||
for {
|
for {
|
||||||
err := web.db.AutoMigrate(&Watch{}, &Filter{}, &FilterConnection{}, &FilterOutput{})
|
|
||||||
if err == nil {
|
|
||||||
log.Println("Connected to DB!")
|
|
||||||
break
|
|
||||||
}
|
|
||||||
if delay >= maxDelay {
|
|
||||||
web.startupWarning("Could not initialize database", err)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
log.Println("Could not connect to DB, retry in:", delay.String())
|
|
||||||
time.Sleep(delay)
|
time.Sleep(delay)
|
||||||
delay *= 2
|
delay *= 2
|
||||||
|
if delay >= maxDelay {
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
web.db, err = gorm.Open(dialector, conf)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Could not open db connection, retry in:", delay.String(), err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
db, err := web.db.DB()
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Could not get DB, retry in:", delay.String(), err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
err = db.Ping()
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Could not ping db, retry in:", delay.String(), err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
break
|
||||||
}
|
}
|
||||||
|
web.db.AutoMigrate(&Watch{}, &Filter{}, &FilterConnection{}, &FilterOutput{})
|
||||||
}
|
}
|
||||||
|
|
||||||
// initRouer initializes the GoWatch routes, binding web.func to a url path
|
// initRouer initializes the GoWatch routes, binding web.func to a url path
|
||||||
|
@ -279,7 +280,12 @@ func (web *Web) initCronJobs() {
|
||||||
|
|
||||||
// add some delay to cron jobs, so watches with the same schedule don't
|
// add some delay to cron jobs, so watches with the same schedule don't
|
||||||
// 'burst' at the same time after restarting GoWatch
|
// 'burst' at the same time after restarting GoWatch
|
||||||
cronDelayStr := viper.GetString("schedule.delay")
|
var cronDelayStr string
|
||||||
|
if viper.IsSet("schedule.delay") {
|
||||||
|
cronDelayStr = viper.GetString("schedule.delay")
|
||||||
|
} else {
|
||||||
|
cronDelayStr = "100ms"
|
||||||
|
}
|
||||||
cronDelay, delayErr := time.ParseDuration(cronDelayStr)
|
cronDelay, delayErr := time.ParseDuration(cronDelayStr)
|
||||||
if delayErr == nil {
|
if delayErr == nil {
|
||||||
log.Println("Delaying job startup by:", cronDelay.String())
|
log.Println("Delaying job startup by:", cronDelay.String())
|
||||||
|
|
Loading…
Add table
Reference in a new issue