fix: 解决 Redis rbd 规则设置报错的问题 (#3776)

This commit is contained in:
ssongliu 2024-02-01 18:04:02 +08:00 committed by GitHub
parent edc3540e67
commit d82086ce4a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 25 additions and 6 deletions

View file

@ -44,7 +44,7 @@ func (u *RedisService) UpdateConf(req dto.RedisConfUpdate) error {
confs = append(confs, redisConfig{key: "timeout", value: req.Timeout}) confs = append(confs, redisConfig{key: "timeout", value: req.Timeout})
confs = append(confs, redisConfig{key: "maxclients", value: req.Maxclients}) confs = append(confs, redisConfig{key: "maxclients", value: req.Maxclients})
confs = append(confs, redisConfig{key: "maxmemory", value: req.Maxmemory}) confs = append(confs, redisConfig{key: "maxmemory", value: req.Maxmemory})
if err := confSet(redisInfo.Name, confs); err != nil { if err := confSet(redisInfo.Name, "", confs); err != nil {
return err return err
} }
if _, err := compose.Restart(fmt.Sprintf("%s/redis/%s/docker-compose.yml", constant.AppInstallDir, redisInfo.Name)); err != nil { if _, err := compose.Restart(fmt.Sprintf("%s/redis/%s/docker-compose.yml", constant.AppInstallDir, redisInfo.Name)); err != nil {
@ -78,7 +78,7 @@ func (u *RedisService) UpdatePersistenceConf(req dto.RedisConfPersistenceUpdate)
confs = append(confs, redisConfig{key: "appendonly", value: req.Appendonly}) confs = append(confs, redisConfig{key: "appendonly", value: req.Appendonly})
confs = append(confs, redisConfig{key: "appendfsync", value: req.Appendfsync}) confs = append(confs, redisConfig{key: "appendfsync", value: req.Appendfsync})
} }
if err := confSet(redisInfo.Name, confs); err != nil { if err := confSet(redisInfo.Name, req.Type, confs); err != nil {
return err return err
} }
if _, err := compose.Restart(fmt.Sprintf("%s/redis/%s/docker-compose.yml", constant.AppInstallDir, redisInfo.Name)); err != nil { if _, err := compose.Restart(fmt.Sprintf("%s/redis/%s/docker-compose.yml", constant.AppInstallDir, redisInfo.Name)); err != nil {
@ -213,7 +213,7 @@ type redisConfig struct {
value string value string
} }
func confSet(redisName string, changeConf []redisConfig) error { func confSet(redisName string, updateType string, changeConf []redisConfig) error {
path := fmt.Sprintf("%s/redis/%s/conf/redis.conf", constant.AppInstallDir, redisName) path := fmt.Sprintf("%s/redis/%s/conf/redis.conf", constant.AppInstallDir, redisName)
lineBytes, err := os.ReadFile(path) lineBytes, err := os.ReadFile(path)
if err != nil { if err != nil {
@ -221,19 +221,38 @@ func confSet(redisName string, changeConf []redisConfig) error {
} }
files := strings.Split(string(lineBytes), "\n") files := strings.Split(string(lineBytes), "\n")
startIndex, endIndex := 0, 0 startIndex, endIndex, emptyLine := 0, 0, 0
var newFiles []string var newFiles []string
for i := 0; i < len(files); i++ { for i := 0; i < len(files); i++ {
if files[i] == "# Redis configuration rewrite by 1Panel" { if files[i] == "# Redis configuration rewrite by 1Panel" {
startIndex = i startIndex = i
newFiles = append(newFiles, files[i])
continue
} }
if files[i] == "# End Redis configuration rewrite by 1Panel" { if files[i] == "# End Redis configuration rewrite by 1Panel" {
endIndex = i endIndex = i
break break
} }
if startIndex == 0 && strings.HasPrefix(files[i], "save ") {
newFiles = append(newFiles, "# "+files[i])
continue
}
if startIndex != 0 && endIndex == 0 && (len(files[i]) == 0 || (updateType == "rbd" && strings.HasPrefix(files[i], "save "))) {
emptyLine++
continue
}
newFiles = append(newFiles, files[i]) newFiles = append(newFiles, files[i])
} }
endIndex = endIndex - emptyLine
for _, item := range changeConf { for _, item := range changeConf {
if item.key == "save" {
saveVal := strings.Split(item.value, ",")
for i := 0; i < len(saveVal); i++ {
newFiles = append(newFiles, "save "+saveVal[i])
}
continue
}
isExist := false isExist := false
for i := startIndex; i < endIndex; i++ { for i := startIndex; i < endIndex; i++ {
if strings.HasPrefix(newFiles[i], item.key) || strings.HasPrefix(newFiles[i], "# "+item.key) { if strings.HasPrefix(newFiles[i], item.key) || strings.HasPrefix(newFiles[i], "# "+item.key) {

View file

@ -292,10 +292,10 @@ const onSave = async (formEl: FormInstance | undefined, type: string) => {
MsgInfo(i18n.global.t('database.rdbInfo')); MsgInfo(i18n.global.t('database.rdbInfo'));
return; return;
} }
itemSaves.push(item.second + '', item.count + ''); itemSaves.push(item.second + ' ' + item.count);
} }
param.type = type; param.type = type;
param.save = itemSaves.join(' '); param.save = itemSaves.join(',');
emit('loading', true); emit('loading', true);
await updateRedisPersistenceConf(param) await updateRedisPersistenceConf(param)
.then(() => { .then(() => {