aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2023-02-24 18:23:13 +0800
committerGitHub <noreply@github.com>2023-02-24 18:23:13 +0800
commit91fa0eb9d7b8c1bb5afac9d68161cf95ae0a02f8 (patch)
treee2e552ec7ec356af752411d8b595e81c1d28ed24
parentedf98a2dc30956c8e04b778bb7f1ce55c14ba963 (diff)
downloadgitea-91fa0eb9d7b8c1bb5afac9d68161cf95ae0a02f8.tar.gz
gitea-91fa0eb9d7b8c1bb5afac9d68161cf95ae0a02f8.zip
Avoid warning for system setting when start up (#23054)
Partially fix #23050 After #22294 merged, it always has a warning log like `cannot get context cache` when starting up. This should not affect any real life but it's annoying. This PR will fix the problem. That means when starting up, getting the system settings will not try from the cache but will read from the database directly. --------- Co-authored-by: Lauris BH <lauris@nix.lv>
-rw-r--r--models/avatars/avatar.go4
-rw-r--r--models/avatars/avatar_test.go2
-rw-r--r--models/repo.go4
-rw-r--r--models/system/setting.go49
-rw-r--r--models/system/setting_test.go6
-rw-r--r--models/unittest/testdb.go2
-rw-r--r--models/user/avatar.go2
-rw-r--r--modules/repository/commits_test.go2
-rw-r--r--modules/templates/helper.go2
-rw-r--r--routers/init.go2
-rw-r--r--routers/web/admin/config.go2
11 files changed, 43 insertions, 34 deletions
diff --git a/models/avatars/avatar.go b/models/avatars/avatar.go
index 6cf05dd284..265ee6428e 100644
--- a/models/avatars/avatar.go
+++ b/models/avatars/avatar.go
@@ -153,7 +153,7 @@ func generateEmailAvatarLink(ctx context.Context, email string, size int, final
return DefaultAvatarLink()
}
- enableFederatedAvatar := system_model.GetSettingBool(ctx, system_model.KeyPictureEnableFederatedAvatar)
+ enableFederatedAvatar := system_model.GetSettingWithCacheBool(ctx, system_model.KeyPictureEnableFederatedAvatar)
var err error
if enableFederatedAvatar && system_model.LibravatarService != nil {
@@ -174,7 +174,7 @@ func generateEmailAvatarLink(ctx context.Context, email string, size int, final
return urlStr
}
- disableGravatar := system_model.GetSettingBool(ctx, system_model.KeyPictureDisableGravatar)
+ disableGravatar := system_model.GetSettingWithCacheBool(ctx, system_model.KeyPictureDisableGravatar)
if !disableGravatar {
// copy GravatarSourceURL, because we will modify its Path.
avatarURLCopy := *system_model.GravatarSourceURL
diff --git a/models/avatars/avatar_test.go b/models/avatars/avatar_test.go
index a3cb36d0e1..59daaeb669 100644
--- a/models/avatars/avatar_test.go
+++ b/models/avatars/avatar_test.go
@@ -28,7 +28,7 @@ func enableGravatar(t *testing.T) {
err := system_model.SetSettingNoVersion(db.DefaultContext, system_model.KeyPictureDisableGravatar, "false")
assert.NoError(t, err)
setting.GravatarSource = gravatarSource
- err = system_model.Init()
+ err = system_model.Init(db.DefaultContext)
assert.NoError(t, err)
}
diff --git a/models/repo.go b/models/repo.go
index 38dc3f1ab1..5a1e2e028e 100644
--- a/models/repo.go
+++ b/models/repo.go
@@ -39,9 +39,9 @@ import (
var ItemsPerPage = 40
// Init initialize model
-func Init() error {
+func Init(ctx context.Context) error {
unit.LoadUnitConfig()
- return system_model.Init()
+ return system_model.Init(ctx)
}
// DeleteRepository deletes a repository for a user or organization.
diff --git a/models/system/setting.go b/models/system/setting.go
index 098d9a1832..6af759dc8a 100644
--- a/models/system/setting.go
+++ b/models/system/setting.go
@@ -79,8 +79,8 @@ func IsErrDataExpired(err error) bool {
return ok
}
-// GetSettingNoCache returns specific setting without using the cache
-func GetSettingNoCache(ctx context.Context, key string) (*Setting, error) {
+// GetSetting returns specific setting without using the cache
+func GetSetting(ctx context.Context, key string) (*Setting, error) {
v, err := GetSettings(ctx, []string{key})
if err != nil {
return nil, err
@@ -93,11 +93,11 @@ func GetSettingNoCache(ctx context.Context, key string) (*Setting, error) {
const contextCacheKey = "system_setting"
-// GetSetting returns the setting value via the key
-func GetSetting(ctx context.Context, key string) (string, error) {
+// GetSettingWithCache returns the setting value via the key
+func GetSettingWithCache(ctx context.Context, key string) (string, error) {
return cache.GetWithContextCache(ctx, contextCacheKey, key, func() (string, error) {
return cache.GetString(genSettingCacheKey(key), func() (string, error) {
- res, err := GetSettingNoCache(ctx, key)
+ res, err := GetSetting(ctx, key)
if err != nil {
return "", err
}
@@ -110,6 +110,15 @@ func GetSetting(ctx context.Context, key string) (string, error) {
// none existing keys and errors are ignored and result in false
func GetSettingBool(ctx context.Context, key string) bool {
s, _ := GetSetting(ctx, key)
+ if s == nil {
+ return false
+ }
+ v, _ := strconv.ParseBool(s.SettingValue)
+ return v
+}
+
+func GetSettingWithCacheBool(ctx context.Context, key string) bool {
+ s, _ := GetSettingWithCache(ctx, key)
v, _ := strconv.ParseBool(s)
return v
}
@@ -120,7 +129,7 @@ func GetSettings(ctx context.Context, keys []string) (map[string]*Setting, error
keys[i] = strings.ToLower(keys[i])
}
settings := make([]*Setting, 0, len(keys))
- if err := db.GetEngine(db.DefaultContext).
+ if err := db.GetEngine(ctx).
Where(builder.In("setting_key", keys)).
Find(&settings); err != nil {
return nil, err
@@ -151,9 +160,9 @@ func (settings AllSettings) GetVersion(key string) int {
}
// GetAllSettings returns all settings from user
-func GetAllSettings() (AllSettings, error) {
+func GetAllSettings(ctx context.Context) (AllSettings, error) {
settings := make([]*Setting, 0, 5)
- if err := db.GetEngine(db.DefaultContext).
+ if err := db.GetEngine(ctx).
Find(&settings); err != nil {
return nil, err
}
@@ -168,12 +177,12 @@ func GetAllSettings() (AllSettings, error) {
func DeleteSetting(ctx context.Context, setting *Setting) error {
cache.RemoveContextData(ctx, contextCacheKey, setting.SettingKey)
cache.Remove(genSettingCacheKey(setting.SettingKey))
- _, err := db.GetEngine(db.DefaultContext).Delete(setting)
+ _, err := db.GetEngine(ctx).Delete(setting)
return err
}
func SetSettingNoVersion(ctx context.Context, key, value string) error {
- s, err := GetSettingNoCache(ctx, key)
+ s, err := GetSetting(ctx, key)
if IsErrSettingIsNotExist(err) {
return SetSetting(ctx, &Setting{
SettingKey: key,
@@ -189,7 +198,7 @@ func SetSettingNoVersion(ctx context.Context, key, value string) error {
// SetSetting updates a users' setting for a specific key
func SetSetting(ctx context.Context, setting *Setting) error {
- if err := upsertSettingValue(strings.ToLower(setting.SettingKey), setting.SettingValue, setting.Version); err != nil {
+ if err := upsertSettingValue(ctx, strings.ToLower(setting.SettingKey), setting.SettingValue, setting.Version); err != nil {
return err
}
@@ -205,8 +214,8 @@ func SetSetting(ctx context.Context, setting *Setting) error {
return nil
}
-func upsertSettingValue(key, value string, version int) error {
- return db.WithTx(db.DefaultContext, func(ctx context.Context) error {
+func upsertSettingValue(parentCtx context.Context, key, value string, version int) error {
+ return db.WithTx(parentCtx, func(ctx context.Context) error {
e := db.GetEngine(ctx)
// here we use a general method to do a safe upsert for different databases (and most transaction levels)
@@ -249,9 +258,9 @@ var (
LibravatarService *libravatar.Libravatar
)
-func Init() error {
+func Init(ctx context.Context) error {
var disableGravatar bool
- disableGravatarSetting, err := GetSettingNoCache(db.DefaultContext, KeyPictureDisableGravatar)
+ disableGravatarSetting, err := GetSetting(ctx, KeyPictureDisableGravatar)
if IsErrSettingIsNotExist(err) {
disableGravatar = setting_module.GetDefaultDisableGravatar()
disableGravatarSetting = &Setting{SettingValue: strconv.FormatBool(disableGravatar)}
@@ -262,7 +271,7 @@ func Init() error {
}
var enableFederatedAvatar bool
- enableFederatedAvatarSetting, err := GetSettingNoCache(db.DefaultContext, KeyPictureEnableFederatedAvatar)
+ enableFederatedAvatarSetting, err := GetSetting(ctx, KeyPictureEnableFederatedAvatar)
if IsErrSettingIsNotExist(err) {
enableFederatedAvatar = setting_module.GetDefaultEnableFederatedAvatar(disableGravatar)
enableFederatedAvatarSetting = &Setting{SettingValue: strconv.FormatBool(enableFederatedAvatar)}
@@ -275,13 +284,13 @@ func Init() error {
if setting_module.OfflineMode {
disableGravatar = true
enableFederatedAvatar = false
- if !GetSettingBool(db.DefaultContext, KeyPictureDisableGravatar) {
- if err := SetSettingNoVersion(db.DefaultContext, KeyPictureDisableGravatar, "true"); err != nil {
+ if !GetSettingBool(ctx, KeyPictureDisableGravatar) {
+ if err := SetSettingNoVersion(ctx, KeyPictureDisableGravatar, "true"); err != nil {
return fmt.Errorf("Failed to set setting %q: %w", KeyPictureDisableGravatar, err)
}
}
- if GetSettingBool(db.DefaultContext, KeyPictureEnableFederatedAvatar) {
- if err := SetSettingNoVersion(db.DefaultContext, KeyPictureEnableFederatedAvatar, "false"); err != nil {
+ if GetSettingBool(ctx, KeyPictureEnableFederatedAvatar) {
+ if err := SetSettingNoVersion(ctx, KeyPictureEnableFederatedAvatar, "false"); err != nil {
return fmt.Errorf("Failed to set setting %q: %w", KeyPictureEnableFederatedAvatar, err)
}
}
diff --git a/models/system/setting_test.go b/models/system/setting_test.go
index fbd04088e6..e6997ad783 100644
--- a/models/system/setting_test.go
+++ b/models/system/setting_test.go
@@ -40,10 +40,10 @@ func TestSettings(t *testing.T) {
value, err := system.GetSetting(db.DefaultContext, keyName)
assert.NoError(t, err)
- assert.EqualValues(t, updatedSetting.SettingValue, value)
+ assert.EqualValues(t, updatedSetting.SettingValue, value.SettingValue)
// get all settings
- settings, err = system.GetAllSettings()
+ settings, err = system.GetAllSettings(db.DefaultContext)
assert.NoError(t, err)
assert.Len(t, settings, 3)
assert.EqualValues(t, updatedSetting.SettingValue, settings[strings.ToLower(updatedSetting.SettingKey)].SettingValue)
@@ -51,7 +51,7 @@ func TestSettings(t *testing.T) {
// delete setting
err = system.DeleteSetting(db.DefaultContext, &system.Setting{SettingKey: strings.ToLower(keyName)})
assert.NoError(t, err)
- settings, err = system.GetAllSettings()
+ settings, err = system.GetAllSettings(db.DefaultContext)
assert.NoError(t, err)
assert.Len(t, settings, 2)
}
diff --git a/models/unittest/testdb.go b/models/unittest/testdb.go
index 7e327f2bd2..2bc41084ba 100644
--- a/models/unittest/testdb.go
+++ b/models/unittest/testdb.go
@@ -113,7 +113,7 @@ func MainTest(m *testing.M, testOpts *TestOptions) {
if err = storage.Init(); err != nil {
fatalTestError("storage.Init: %v\n", err)
}
- if err = system_model.Init(); err != nil {
+ if err = system_model.Init(db.DefaultContext); err != nil {
fatalTestError("models.Init: %v\n", err)
}
diff --git a/models/user/avatar.go b/models/user/avatar.go
index 9af2a9acc2..3d1e2ed20b 100644
--- a/models/user/avatar.go
+++ b/models/user/avatar.go
@@ -67,7 +67,7 @@ func (u *User) AvatarLinkWithSize(ctx context.Context, size int) string {
useLocalAvatar := false
autoGenerateAvatar := false
- disableGravatar := system_model.GetSettingBool(ctx, system_model.KeyPictureDisableGravatar)
+ disableGravatar := system_model.GetSettingWithCacheBool(ctx, system_model.KeyPictureDisableGravatar)
switch {
case u.UseCustomAvatar:
diff --git a/modules/repository/commits_test.go b/modules/repository/commits_test.go
index 2bd8de38aa..a407083f3a 100644
--- a/modules/repository/commits_test.go
+++ b/modules/repository/commits_test.go
@@ -106,7 +106,7 @@ func enableGravatar(t *testing.T) {
err := system_model.SetSettingNoVersion(db.DefaultContext, system_model.KeyPictureDisableGravatar, "false")
assert.NoError(t, err)
setting.GravatarSource = "https://secure.gravatar.com/avatar"
- err = system_model.Init()
+ err = system_model.Init(db.DefaultContext)
assert.NoError(t, err)
}
diff --git a/modules/templates/helper.go b/modules/templates/helper.go
index b7bd07670c..17ac68dc6b 100644
--- a/modules/templates/helper.go
+++ b/modules/templates/helper.go
@@ -92,7 +92,7 @@ func NewFuncMap() []template.FuncMap {
return setting.AssetVersion
},
"DisableGravatar": func(ctx context.Context) bool {
- return system_model.GetSettingBool(ctx, system_model.KeyPictureDisableGravatar)
+ return system_model.GetSettingWithCacheBool(ctx, system_model.KeyPictureDisableGravatar)
},
"DefaultShowFullName": func() bool {
return setting.UI.DefaultShowFullName
diff --git a/routers/init.go b/routers/init.go
index 6a51de669a..d3f822dc83 100644
--- a/routers/init.go
+++ b/routers/init.go
@@ -150,7 +150,7 @@ func GlobalInitInstalled(ctx context.Context) {
mustInit(system.Init)
mustInit(oauth2.Init)
- mustInit(models.Init)
+ mustInitCtx(ctx, models.Init)
mustInit(repo_service.Init)
// Booting long running goroutines.
diff --git a/routers/web/admin/config.go b/routers/web/admin/config.go
index 2566cbe42b..075fb423dc 100644
--- a/routers/web/admin/config.go
+++ b/routers/web/admin/config.go
@@ -103,7 +103,7 @@ func Config(ctx *context.Context) {
ctx.Data["PageIsAdmin"] = true
ctx.Data["PageIsAdminConfig"] = true
- systemSettings, err := system_model.GetAllSettings()
+ systemSettings, err := system_model.GetAllSettings(ctx)
if err != nil {
ctx.ServerError("system_model.GetAllSettings", err)
return