Skip to content

Commit

Permalink
Avoid starting graceful for migrate commands and checkout
Browse files Browse the repository at this point in the history
  • Loading branch information
zeripath committed Dec 9, 2019
1 parent db022dd commit 1aafb56
Show file tree
Hide file tree
Showing 11 changed files with 24 additions and 20 deletions.
4 changes: 3 additions & 1 deletion cmd/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
package cmd

import (
"context"

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/migrations"
"code.gitea.io/gitea/modules/log"
Expand Down Expand Up @@ -32,7 +34,7 @@ func runMigrate(ctx *cli.Context) error {
log.Trace("Log path: %s", setting.LogRootPath)
setting.InitDBConfig()

if err := models.NewEngine(migrations.Migrate); err != nil {
if err := models.NewEngine(context.Background(), migrations.Migrate); err != nil {
log.Fatal("Failed to initialize ORM engine: %v", err)
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func runWeb(ctx *cli.Context) error {
}

// Perform global initialization
routers.GlobalInit()
routers.GlobalInit(graceful.GetManager().HammerContext())

// Set up Macaron
m := routes.NewMacaron()
Expand Down
3 changes: 2 additions & 1 deletion contrib/pr/checkout.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Checkout a PR and load the tests data into sqlite database
*/

import (
"context"
"flag"
"fmt"
"io/ioutil"
Expand Down Expand Up @@ -92,7 +93,7 @@ func runPR() {
//x, err = xorm.NewEngine("sqlite3", "file::memory:?cache=shared")

var helper testfixtures.Helper = &testfixtures.SQLite{}
models.NewEngine(func(_ *xorm.Engine) error {
models.NewEngine(context.Background(), func(_ *xorm.Engine) error {
return nil
})
models.HasEngine = true
Expand Down
2 changes: 1 addition & 1 deletion integrations/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func initIntegrationTest() {
}
defer db.Close()
}
routers.GlobalInit()
routers.GlobalInit(graceful.GetManager().HammerContext())
}

func prepareTestEnv(t testing.TB, skip ...int) func() {
Expand Down
3 changes: 2 additions & 1 deletion integrations/migration-test/migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package migrations

import (
"compress/gzip"
"context"
"database/sql"
"fmt"
"io/ioutil"
Expand Down Expand Up @@ -220,7 +221,7 @@ func doMigrationTest(t *testing.T, version string) {
err := models.SetEngine()
assert.NoError(t, err)

err = models.NewEngine(wrappedMigrate)
err = models.NewEngine(context.Background(), wrappedMigrate)
assert.NoError(t, err)
currentEngine.Close()
}
Expand Down
6 changes: 3 additions & 3 deletions models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
package models

import (
"context"
"database/sql"
"errors"
"fmt"

"code.gitea.io/gitea/modules/graceful"
"code.gitea.io/gitea/modules/setting"

// Needed for the MySQL driver
Expand Down Expand Up @@ -165,12 +165,12 @@ func SetEngine() (err error) {
}

// NewEngine initializes a new xorm.Engine
func NewEngine(migrateFunc func(*xorm.Engine) error) (err error) {
func NewEngine(ctx context.Context, migrateFunc func(*xorm.Engine) error) (err error) {
if err = SetEngine(); err != nil {
return err
}

x.SetDefaultContext(graceful.GetManager().HammerContext())
x.SetDefaultContext(ctx)

if err = x.Ping(); err != nil {
return err
Expand Down
3 changes: 2 additions & 1 deletion modules/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ func SetExecutablePath(path string) error {
}

// Init initializes git module
func Init() error {
func Init(ctx context.Context) error {
DefaultContext = ctx
// Git requires setting user.name and user.email in order to commit changes.
for configKey, defaultValue := range map[string]string{"user.name": "Gitea", "user.email": "gitea@fake.local"} {
if stdout, stderr, err := process.GetManager().Exec("git.Init(get setting)", GitExecutable, "config", "--get", configKey); err != nil || strings.TrimSpace(stdout) == "" {
Expand Down
3 changes: 2 additions & 1 deletion modules/git/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package git

import (
"context"
"fmt"
"os"
"testing"
Expand All @@ -16,7 +17,7 @@ func fatalTestError(fmtStr string, args ...interface{}) {
}

func TestMain(m *testing.M) {
if err := Init(); err != nil {
if err := Init(context.Background()); err != nil {
fatalTestError("Init failed: %v", err)
}

Expand Down
4 changes: 0 additions & 4 deletions modules/graceful/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"sync"
"time"

"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/process"
"code.gitea.io/gitea/modules/setting"
Expand Down Expand Up @@ -50,9 +49,6 @@ func InitManager(ctx context.Context) {
initOnce.Do(func() {
manager = newGracefulManager(ctx)

// Set the git default context to the HammerContext
git.DefaultContext = manager.HammerContext()

// Set the process default context to the HammerContext
process.DefaultContext = manager.HammerContext()
})
Expand Down
11 changes: 6 additions & 5 deletions routers/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package routers

import (
"context"
"strings"
"time"

Expand Down Expand Up @@ -53,11 +54,11 @@ func NewServices() {
}

// In case of problems connecting to DB, retry connection. Eg, PGSQL in Docker Container on Synology
func initDBEngine() (err error) {
func initDBEngine(ctx context.Context) (err error) {
log.Info("Beginning ORM engine initialization.")
for i := 0; i < setting.Database.DBConnectRetries; i++ {
log.Info("ORM engine initialization attempt #%d/%d...", i+1, setting.Database.DBConnectRetries)
if err = models.NewEngine(migrations.Migrate); err == nil {
if err = models.NewEngine(ctx, migrations.Migrate); err == nil {
break
} else if i == setting.Database.DBConnectRetries-1 {
return err
Expand All @@ -71,9 +72,9 @@ func initDBEngine() (err error) {
}

// GlobalInit is for global configuration reload-able.
func GlobalInit() {
func GlobalInit(ctx context.Context) {
setting.NewContext()
if err := git.Init(); err != nil {
if err := git.Init(ctx); err != nil {
log.Fatal("Git module init failed: %v", err)
}
setting.CheckLFSVersion()
Expand All @@ -88,7 +89,7 @@ func GlobalInit() {
highlight.NewContext()
external.RegisterParsers()
markup.Init()
if err := initDBEngine(); err == nil {
if err := initDBEngine(ctx); err == nil {
log.Info("ORM engine initialization successful!")
} else {
log.Fatal("ORM engine initialization failed: %v", err)
Expand Down
3 changes: 2 additions & 1 deletion routers/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/generate"
"code.gitea.io/gitea/modules/graceful"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/user"
Expand Down Expand Up @@ -351,7 +352,7 @@ func InstallPost(ctx *context.Context, form auth.InstallForm) {
return
}

GlobalInit()
GlobalInit(graceful.GetManager().HammerContext())

// Create admin account
if len(form.AdminName) > 0 {
Expand Down

0 comments on commit 1aafb56

Please sign in to comment.