Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
19 changes: 2 additions & 17 deletions internal/controller_admin/plugin_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
package controller_admin

import (
"encoding/json"

"github.com/apache/answer/internal/base/handler"
"github.com/apache/answer/internal/schema"
"github.com/apache/answer/internal/service/plugin_common"
Expand Down Expand Up @@ -148,8 +146,7 @@ func (pc *PluginController) UpdatePluginStatus(ctx *gin.Context) {
return
}

plugin.StatusManager.Enable(req.PluginSlugName, req.Enabled)
err := pc.pluginCommonService.UpdatePluginStatus(ctx)
err := pc.pluginCommonService.UpdatePluginStatus(ctx, req.PluginSlugName, req.Enabled)
handler.HandleResponse(ctx, err, nil)
}

Expand Down Expand Up @@ -207,18 +204,6 @@ func (pc *PluginController) UpdatePluginConfig(ctx *gin.Context) {
return
}

configFields, _ := json.Marshal(req.ConfigFields)
err := plugin.CallConfig(func(fn plugin.Config) error {
if fn.Info().SlugName == req.PluginSlugName {
return fn.ConfigReceiver(configFields)
}
return nil
})
if err != nil {
handler.HandleResponse(ctx, err, nil)
return
}

err = pc.pluginCommonService.UpdatePluginConfig(ctx, req)
err := pc.pluginCommonService.UpdatePluginConfig(ctx, req)
handler.HandleResponse(ctx, err, nil)
}
41 changes: 41 additions & 0 deletions internal/repo/config/config_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,44 @@ func (cr configRepo) UpdateConfig(ctx context.Context, key string, value string)
}
return
}

func (cr configRepo) CompareAndSwapConfig(
ctx context.Context, key, oldValue, newValue string,
) (swapped bool, err error) {
oldConfig := &entity.Config{Key: key}
exist, err := cr.data.DB.Context(ctx).Get(oldConfig)
if err != nil {
return false, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}
if !exist {
return false, errors.BadRequest(reason.ObjectNotFound)
}
if oldConfig.Value != oldValue {
return false, nil
}

affected, err := cr.data.DB.Context(ctx).ID(oldConfig.ID).Where("value = ?", oldValue).
Update(&entity.Config{Value: newValue})
if err != nil {
return false, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}
if affected == 0 {
if oldValue != newValue {
return false, nil
}
current, err := cr.GetConfigByKeyFromDB(ctx, key)
if err != nil {
return false, err
}
return current.Value == newValue, nil
}

// Invalidate both cached forms. The next reader will load the latest DB value.
if err := cr.data.Cache.Del(ctx, constant.ConfigKEY2ContentCacheKeyPrefix+key); err != nil {
log.Error(err)
}
if err := cr.data.Cache.Del(ctx, fmt.Sprintf("%s%d", constant.ConfigID2KEYCacheKeyPrefix, oldConfig.ID)); err != nil {
log.Error(err)
}
return true, nil
}
9 changes: 9 additions & 0 deletions internal/service/config/config_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type ConfigRepo interface {
GetConfigByKey(ctx context.Context, key string) (c *entity.Config, err error)
GetConfigByKeyFromDB(ctx context.Context, key string) (c *entity.Config, err error)
UpdateConfig(ctx context.Context, key, value string) (err error)
CompareAndSwapConfig(ctx context.Context, key, oldValue, newValue string) (swapped bool, err error)
}

// ConfigService user service
Expand Down Expand Up @@ -116,3 +117,11 @@ func (cs *ConfigService) GetIDByKey(ctx context.Context, key string) (id int, er
func (cs *ConfigService) UpdateConfig(ctx context.Context, key, value string) (err error) {
return cs.configRepo.UpdateConfig(ctx, key, value)
}

// CompareAndSwapConfig updates a config only when its current value still
// matches oldValue.
func (cs *ConfigService) CompareAndSwapConfig(
ctx context.Context, key, oldValue, newValue string,
) (swapped bool, err error) {
return cs.configRepo.CompareAndSwapConfig(ctx, key, oldValue, newValue)
}
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,12 @@ func (newQuestionNotificationTestConfigRepo) UpdateConfig(context.Context, strin
return nil
}

func (newQuestionNotificationTestConfigRepo) CompareAndSwapConfig(
context.Context, string, string, string,
) (bool, error) {
return false, nil
}

type newQuestionNotificationTestEmailRepo struct {
codesByUserID map[string][]string
}
Expand Down
Loading