Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make db connect more robust #5738

Merged
merged 22 commits into from
Jan 19, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
a3937ad
Add retry loop to DB connect logic
Jan 14, 2019
461665d
make DB connect vars configurable
Jan 15, 2019
a2122a6
Merge branch 'master' into Make_DB_Connect_More_Robust
techknowlogick Jan 15, 2019
16db020
Update DB retry field names, add as setting subfield, add to US local…
Jan 15, 2019
b74d936
spaces->tabs
Jan 15, 2019
5dc2abc
fix new DB setting declaration
Jan 15, 2019
38dbb4d
Add retry loop to DB connect logic
Jan 14, 2019
f6fb9aa
Support CORS headers to git smart http protocol (#5719)
lunny Jan 14, 2019
aa178f7
make DB connect vars configurable
Jan 15, 2019
72c624f
Update DB retry field names, add as setting subfield, add to US local…
Jan 15, 2019
2f6f57a
spaces->tabs
Jan 15, 2019
8544971
fix new DB setting declaration
Jan 15, 2019
d57e3a4
Merge branch 'Make_DB_Connect_More_Robust' of github.com:pbrackin/git…
Jan 15, 2019
990691b
Moving retry logic to separate func. More spaces->tabs.
Jan 16, 2019
9113c75
spaces->tabs
Jan 16, 2019
602159c
fix spacing fmt gripe, I think.
Jan 16, 2019
c2060d0
add additional logging per connect attempt
Jan 18, 2019
eb90cc7
Merge branch 'master' into Make_DB_Connect_More_Robust
techknowlogick Jan 18, 2019
eff081e
Change backoff timer ini setting to a Duration vs constant
Jan 18, 2019
edb820d
Merge branch 'Make_DB_Connect_More_Robust' of github.com:pbrackin/git…
Jan 18, 2019
9b6c7ef
Update backoff timer to Duration in cheatsheet
Jan 18, 2019
b05babf
Merge branch 'master' into Make_DB_Connect_More_Robust
techknowlogick Jan 19, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions custom/conf/app.ini.sample
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,11 @@ SQLITE_TIMEOUT = 500
ITERATE_BUFFER_SIZE = 50
; Show the database generated SQL
LOG_SQL = true
; Maximum number of DB Connect retries
DB_RETRIES = 10
; Backoff time per DB retry (seconds)
DB_RETRY_BKOFF = 3
zeripath marked this conversation as resolved.
Show resolved Hide resolved


[indexer]
ISSUE_INDEXER_PATH = indexers/issues.bleve
Expand Down
17 changes: 14 additions & 3 deletions routers/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package routers
import (
"path"
"strings"
"time"

"code.gitea.io/git"
"code.gitea.io/gitea/models"
Expand Down Expand Up @@ -55,10 +56,20 @@ 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)
maxRetries := setting.Cfg.Section("database").Key("DB_RETRIES").MustInt(10)
retryWait := setting.Cfg.Section("database").Key("DB_RETRY_BKOFF").MustDuration(3 * time.Second)
zeripath marked this conversation as resolved.
Show resolved Hide resolved
// In case of delays connecting to DB, retry connection. Eg, PGSQL in Docker Container on Synolgy
log.Info("Beginning ORM engine init...")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK I think we should strip this out to a separate function:

func initDBEngine() (err error) {
	// With the contents of lines 61 - 72 (as amended)
	// return the last error which you then log.Fatal(4, "Failed to initialize ORM engine: %v", err)
}

Be consistent with the log message. So make this line:

log.Info("Beginning ORM engine initialization...")

for i := 0; i < maxRetries; i++ {
if err := models.NewEngine(migrations.Migrate); err == nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Log at the start of the loop:

log.Info("ORM engine initialization (Attempt: #%d/%d).", i, setting.DBConnectRetries)

in the function turn the := into =

break
zeripath marked this conversation as resolved.
Show resolved Hide resolved
} else if i == maxRetries-1 {
log.Fatal(4, "Failed to initialize ORM engine: %v", err)
zeripath marked this conversation as resolved.
Show resolved Hide resolved
}
time.Sleep(retryWait)
log.Info("ORM engine initialization failed. Retry #%d/10...", i+1)
zeripath marked this conversation as resolved.
Show resolved Hide resolved
}
log.Info("ORM engine init success!")
zeripath marked this conversation as resolved.
Show resolved Hide resolved
models.HasEngine = true
if err := models.InitOAuth2(); err != nil {
log.Fatal(4, "Failed to initialize OAuth2 support: %v", err)
Expand Down