diff options
author | Paul Brackin <pbrackin@users.noreply.github.com> | 2019-01-19 13:17:08 -0800 |
---|---|---|
committer | techknowlogick <hello@techknowlogick.com> | 2019-01-19 16:17:08 -0500 |
commit | 1bb22b2b4777b47b05bf023d7737c03dc9560b09 (patch) | |
tree | 37373f5a1f9d13c66bdee8579f4455a3b316cfb6 /routers | |
parent | f54e0d2f16b7e29b01d5745d94915e5ef52a9455 (diff) | |
download | gitea-1bb22b2b4777b47b05bf023d7737c03dc9560b09.tar.gz gitea-1bb22b2b4777b47b05bf023d7737c03dc9560b09.zip |
Make db connect more robust (#5738)
Diffstat (limited to 'routers')
-rw-r--r-- | routers/init.go | 28 |
1 files changed, 24 insertions, 4 deletions
diff --git a/routers/init.go b/routers/init.go index 3152988ba4..734aaaa131 100644 --- a/routers/init.go +++ b/routers/init.go @@ -7,6 +7,7 @@ package routers import ( "path" "strings" + "time" "code.gitea.io/git" "code.gitea.io/gitea/models" @@ -42,6 +43,24 @@ func NewServices() { cache.NewContext() } +// In case of problems connecting to DB, retry connection. Eg, PGSQL in Docker Container on Synology +func initDBEngine() (err error) { + log.Info("Beginning ORM engine initialization.") + for i := 0; i < setting.DBConnectRetries; i++ { + log.Info("ORM engine initialization attempt #%d/%d...", i+1, setting.DBConnectRetries) + if err := models.NewEngine(migrations.Migrate); err == nil { + break + } else if i == setting.DBConnectRetries-1 { + return err + } + log.Debug("ORM engine initialization attempt #%d/%d failed. Error: %v", i+1, setting.DBConnectRetries, err) + log.Info("Backing off for %d seconds", int64(setting.DBConnectBackoff/time.Second)) + time.Sleep(setting.DBConnectBackoff) + } + models.HasEngine = true + return nil +} + // GlobalInit is for global configuration reload-able. func GlobalInit() { setting.NewContext() @@ -56,11 +75,12 @@ func GlobalInit() { if setting.InstallLock { highlight.NewContext() markup.Init() - - if err := models.NewEngine(migrations.Migrate); err != nil { - log.Fatal(4, "Failed to initialize ORM engine: %v", err) + if err := initDBEngine(); err == nil { + log.Info("ORM engine initialization successful!") + } else { + log.Fatal(4, "ORM engine initialization failed: %v", err) } - models.HasEngine = true + if err := models.InitOAuth2(); err != nil { log.Fatal(4, "Failed to initialize OAuth2 support: %v", err) } |