Skip to content

Commit 85a59ee

Browse files
committed
feature: 增加对redis未部署时异常捕获
1 parent 0c1543a commit 85a59ee

File tree

7 files changed

+96
-35
lines changed

7 files changed

+96
-35
lines changed

backend/services/data-management-service/src/main/java/com/datamate/datamanagement/application/DatasetApplicationService.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.baomidou.mybatisplus.core.metadata.IPage;
44
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
55
import com.datamate.common.domain.utils.ChunksSaver;
6+
import com.datamate.common.setting.application.SysParamApplicationService;
67
import com.datamate.datamanagement.interfaces.dto.*;
78
import com.datamate.common.infrastructure.exception.BusinessAssert;
89
import com.datamate.common.interfaces.PagedResponse;
@@ -21,7 +22,6 @@
2122
import lombok.extern.slf4j.Slf4j;
2223
import org.apache.commons.collections4.CollectionUtils;
2324
import org.springframework.beans.factory.annotation.Value;
24-
import org.springframework.data.redis.core.StringRedisTemplate;
2525
import org.springframework.scheduling.annotation.Async;
2626
import org.springframework.stereotype.Service;
2727
import org.springframework.transaction.annotation.Transactional;
@@ -49,7 +49,7 @@ public class DatasetApplicationService {
4949
private final DatasetFileRepository datasetFileRepository;
5050
private final CollectionTaskClient collectionTaskClient;
5151
private final DatasetFileApplicationService datasetFileApplicationService;
52-
private final StringRedisTemplate redisTemplate;
52+
private final SysParamApplicationService sysParamService;
5353

5454
@Value("${datamate.data-management.base-path:/dataset}")
5555
private String datasetBasePath;
@@ -80,7 +80,7 @@ public Dataset createDataset(CreateDatasetRequest createDatasetRequest) {
8080
}
8181

8282
public String getDatasetPvcName() {
83-
return redisTemplate.opsForValue().get(DATASET_PVC_NAME);
83+
return sysParamService.getParamByKey(DATASET_PVC_NAME);
8484
}
8585

8686
public Dataset updateDataset(String datasetId, UpdateDatasetRequest updateDatasetRequest) {

backend/shared/domain-common/src/main/java/com/datamate/common/setting/application/SysParamApplicationService.java

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
import com.datamate.common.infrastructure.exception.SystemErrorCode;
55
import com.datamate.common.setting.domain.entity.SysParam;
66
import com.datamate.common.setting.domain.repository.SysParamRepository;
7+
import com.datamate.common.setting.infrastructure.client.RedisClient;
78
import jakarta.annotation.PostConstruct;
89
import lombok.RequiredArgsConstructor;
910
import lombok.extern.slf4j.Slf4j;
10-
import org.springframework.data.redis.core.StringRedisTemplate;
1111
import org.springframework.stereotype.Service;
1212

1313
import java.util.Comparator;
@@ -24,7 +24,7 @@
2424
@RequiredArgsConstructor
2525
public class SysParamApplicationService {
2626
private final SysParamRepository sysParamRepository;
27-
private final StringRedisTemplate redisTemplate;
27+
private final RedisClient redisClient;
2828

2929
/**
3030
* 列表查询系统参数
@@ -48,14 +48,25 @@ public void updateParamValueById(String paramId, String paramValue) {
4848
BusinessAssert.notNull(sysParam, SystemErrorCode.RESOURCE_NOT_FOUND);
4949
sysParam.setParamValue(paramValue);
5050
sysParamRepository.updateById(sysParam);
51-
redisTemplate.opsForValue().set(sysParam.getParamKey(), paramValue);
51+
redisClient.setParam(sysParam.getId(), paramValue);
5252
}
5353

54-
public void deleteParamById(String paramId) {
55-
SysParam sysParam = sysParamRepository.getById(paramId);
54+
public void deleteParamById(String paramKey) {
55+
SysParam sysParam = sysParamRepository.getById(paramKey);
5656
BusinessAssert.notNull(sysParam, SystemErrorCode.RESOURCE_NOT_FOUND);
57-
sysParamRepository.removeById(paramId);
58-
redisTemplate.delete(sysParam.getParamKey());
57+
sysParamRepository.removeById(paramKey);
58+
redisClient.delParam(sysParam.getId());
59+
}
60+
61+
public String getParamByKey(String paramId) {
62+
String value = redisClient.getParam(paramId);
63+
if (value == null) {
64+
SysParam sysParam = sysParamRepository.getById(paramId);
65+
if (sysParam != null) {
66+
value = sysParam.getParamValue();
67+
}
68+
}
69+
return value;
5970
}
6071

6172
/**
@@ -65,7 +76,7 @@ public void deleteParamById(String paramId) {
6576
public void init() {
6677
try {
6778
List<SysParam> sysParams = sysParamRepository.list();
68-
sysParams.forEach(sysParam -> redisTemplate.opsForValue().set(sysParam.getParamKey(), sysParam.getParamValue()));
79+
sysParams.forEach(sysParam -> redisClient.setParam(sysParam.getId(), sysParam.getParamValue()));
6980
} catch (Exception e) {
7081
log.error("Init sys params to redis error", e);
7182
}

backend/shared/domain-common/src/main/java/com/datamate/common/setting/domain/entity/SysParam.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,6 @@
1515
@Getter
1616
@TableName("t_sys_param")
1717
public class SysParam extends BaseEntity<String> {
18-
/**
19-
* 设置项键(唯一)
20-
*/
21-
private String paramKey;
22-
2318
/**
2419
* 设置项值
2520
*/
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.datamate.common.setting.infrastructure.client;
2+
3+
import com.datamate.common.setting.infrastructure.utils.FunctionUtil;
4+
import lombok.AllArgsConstructor;
5+
import lombok.extern.slf4j.Slf4j;
6+
import org.springframework.data.redis.core.StringRedisTemplate;
7+
import org.springframework.stereotype.Component;
8+
9+
@Slf4j
10+
@Component
11+
@AllArgsConstructor
12+
public class RedisClient {
13+
private final StringRedisTemplate redisTemplate;
14+
15+
public void setParam(String key, String value) {
16+
FunctionUtil.doWithoutThrow((k, v) -> redisTemplate.opsForValue().set(k, v), key, value);
17+
}
18+
19+
public String getParam(String key) {
20+
return FunctionUtil.getWithoutThrow((k) -> redisTemplate.opsForValue().get(k), key);
21+
}
22+
23+
public void delParam(String key) {
24+
FunctionUtil.doWithoutThrow(redisTemplate::delete, key);
25+
}
26+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.datamate.common.setting.infrastructure.utils;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
5+
import java.util.function.BiConsumer;
6+
import java.util.function.Consumer;
7+
import java.util.function.Function;
8+
9+
@Slf4j
10+
public class FunctionUtil {
11+
public static <T, R> R getWithoutThrow(Function<T, R> action, T key) {
12+
try {
13+
return action.apply(key);
14+
} catch (Exception e) {
15+
log.warn(e.getMessage());
16+
return null;
17+
}
18+
}
19+
20+
public static <T> void doWithoutThrow(Consumer<T> action, T key) {
21+
try {
22+
action.accept(key);
23+
} catch (Exception e) {
24+
log.warn(e.getMessage());
25+
}
26+
}
27+
28+
public static <T, R> void doWithoutThrow(BiConsumer<T, R> action, T t, R r) {
29+
try {
30+
action.accept(t, r);
31+
} catch (Exception e) {
32+
log.warn(e.getMessage());
33+
}
34+
}
35+
}

frontend/src/pages/SettingsPage/SystemConfig.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import { Divider, Input, Select, Switch, Button, Table, Spin } from "antd";
1+
import { Input, Select, Switch, Button, Table, Spin } from "antd";
22
import { useEffect, useState } from "react";
33
import { getSysParamList, updateSysParamValue } from './settings.apis';
44

55
interface SystemParam {
66
id: string;
7-
paramKey: string;
87
paramValue: string;
98
description: string;
109
isEnabled: boolean;
@@ -120,10 +119,9 @@ export default function SystemConfig() {
120119
const columns = [
121120
{
122121
title: "参数名",
123-
dataIndex: "paramKey",
124-
key: "paramKey",
122+
dataIndex: "id",
123+
key: "id",
125124
width: 180,
126-
127125
},
128126
{
129127
title: "参数值",

scripts/db/setting-management-init.sql

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ CREATE TABLE IF NOT EXISTS t_model_config
2020

2121
CREATE TABLE IF NOT EXISTS t_sys_param
2222
(
23-
id VARCHAR(36) PRIMARY KEY COMMENT '主键ID',
24-
param_key VARCHAR(100) NOT NULL COMMENT '设置项键',
23+
id VARCHAR(100) PRIMARY KEY COMMENT '主键ID,设置项键',
2524
param_value TEXT NOT NULL COMMENT '设置项值',
2625
param_type VARCHAR(50) DEFAULT 'string' COMMENT '设置项类型(仅 string、number、boolean 三种类型)',
2726
option_list TEXT COMMENT '选项列表(逗号分隔,仅对 enum 类型有效)',
@@ -32,20 +31,17 @@ CREATE TABLE IF NOT EXISTS t_sys_param
3231
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
3332
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
3433
created_by VARCHAR(255) COMMENT '创建者',
35-
updated_by VARCHAR(255) COMMENT '更新者',
36-
UNIQUE KEY uk_sys_param (param_key) COMMENT '避免设置项键重复'
34+
updated_by VARCHAR(255) COMMENT '更新者'
3735
) ENGINE = InnoDB
3836
DEFAULT CHARSET = utf8mb4 COMMENT ='设置管理表';
3937

40-
insert ignore into t_sys_param (id, param_key, param_value, param_type, option_list, description, is_built_in,
38+
insert ignore into t_sys_param (id, param_value, param_type, option_list, description, is_built_in,
4139
can_modify,
4240
is_enabled, created_by, updated_by)
43-
values ('1', 'sys.knowledge.base.count', '200', 'number', '10,200,500', '知识库最大数量', 1, 1, 1, 'system', 'system'),
44-
('2', 'SEARCH_API', 'tavily', 'string', '', 'deer-flow使用的搜索引擎', 1, 1, 1, 'system', 'system'),
45-
('3', 'TAVILY_API_KEY', 'tvly-dev-xxx', 'string', '', 'deer-flow使用的搜索引擎所需的apiKey', 1, 1, 1, 'system',
46-
'system'),
47-
('4', 'BRAVE_SEARCH_API_KEY', 'api-xxx', 'string', '', 'deer-flow使用的搜索引擎所需的apiKey', 1, 1, 1, 'system',
48-
'system'),
49-
('5', 'JINA_API_KEY', '', 'string', '', 'deer-flow使用的JINA搜索引擎所需的apiKey', 1, 1, 1, 'system', 'system'),
50-
('6', 'sys.management.dataset.pvc.name', 'dataset-pvc', 'string', '', '数据集所在pvc名称', 1, 0, 1, 'system', 'system'),
51-
('7', 'test_bool', 'true', 'boolean', '', '测试布尔值', 1, 1, 1, 'system', 'system');
41+
values ('sys.knowledge.base.count', '200', 'number', '10,200,500', '知识库最大数量', 1, 1, 1, 'system', 'system'),
42+
('SEARCH_API', 'tavily', 'string', 'tavily,infoquest,duckduckgo,brave_search,arxiv', 'deer-flow使用的搜索引擎', 1, 1, 1, 'system', 'system'),
43+
('TAVILY_API_KEY', 'tvly-dev-xxx', 'string', '', 'deer-flow使用的搜索引擎所需的apiKey', 1, 1, 1, 'system', 'system'),
44+
('BRAVE_SEARCH_API_KEY', 'api-xxx', 'string', '', 'deer-flow使用的搜索引擎所需的apiKey', 1, 1, 1, 'system', 'system'),
45+
('JINA_API_KEY', '', 'string', '', 'deer-flow使用的JINA搜索引擎所需的apiKey', 1, 1, 1, 'system', 'system'),
46+
('sys.management.dataset.pvc.name', 'dataset-pvc', 'string', '', '数据集所在pvc名称', 1, 0, 1, 'system', 'system'),
47+
('test_bool', 'true', 'boolean', '', '测试布尔值', 1, 1, 1, 'system', 'system');

0 commit comments

Comments
 (0)