Skip to content

Commit

Permalink
优化 error-code 加载逻辑,失败不影响启动
Browse files Browse the repository at this point in the history
  • Loading branch information
YunaiV committed Nov 25, 2023
1 parent 6d7491f commit f2ff30e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,12 @@ public void execute() {
log.info("[execute][解析到错误码数量为 ({}) 个]", autoGenerateDTOs.size());

// 第二步,写入到 system 服务
errorCodeApi.autoGenerateErrorCodeList(autoGenerateDTOs);
log.info("[execute][写入到 system 组件完成]");
try {
errorCodeApi.autoGenerateErrorCodeList(autoGenerateDTOs);
log.info("[execute][写入到 system 组件完成]");
} catch (Exception ex) {
log.error("[execute][写入到 system 组件失败({})]", ExceptionUtil.getRootCauseMessage(ex));
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,14 @@ default void putErrorCode(Integer code, String msg) {
ServiceExceptionUtil.put(code, msg);
}

/**
* 刷新错误码
*/
void refreshErrorCodes();

/**
* 加载错误码
*/
void loadErrorCodes();

}
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package cn.iocoder.yudao.framework.errorcode.core.loader;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.exceptions.ExceptionUtil;
import cn.iocoder.yudao.framework.common.util.date.DateUtils;
import cn.iocoder.yudao.module.system.api.errorcode.ErrorCodeApi;
import cn.iocoder.yudao.module.system.api.errorcode.dto.ErrorCodeRespDTO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;

import java.time.LocalDateTime;
Expand Down Expand Up @@ -43,31 +45,38 @@ public class ErrorCodeLoaderImpl implements ErrorCodeLoader {
*/
private LocalDateTime maxUpdateTime;

@Override
@EventListener(ApplicationReadyEvent.class)
@Async // 异步,保证项目的启动过程,毕竟非关键流程
public void loadErrorCodes() {
this.loadErrorCodes0();
loadErrorCodes0();
}

@Override
@Scheduled(fixedDelay = REFRESH_ERROR_CODE_PERIOD, initialDelay = REFRESH_ERROR_CODE_PERIOD)
public void refreshErrorCodes() {
this.loadErrorCodes0();
loadErrorCodes0();
}

private void loadErrorCodes0() {
// 加载错误码
List<ErrorCodeRespDTO> errorCodeRespDTOs = errorCodeApi.getErrorCodeList(applicationName, maxUpdateTime);
if (CollUtil.isEmpty(errorCodeRespDTOs)) {
return;
}
log.info("[loadErrorCodes0][加载到 ({}) 个错误码]", errorCodeRespDTOs.size());
try {
// 加载错误码
List<ErrorCodeRespDTO> errorCodeRespDTOs = errorCodeApi.getErrorCodeList(applicationName, maxUpdateTime);
if (CollUtil.isEmpty(errorCodeRespDTOs)) {
return;
}
log.info("[loadErrorCodes0][加载到 ({}) 个错误码]", errorCodeRespDTOs.size());

// 刷新错误码的缓存
errorCodeRespDTOs.forEach(errorCodeRespDTO -> {
// 写入到错误码的缓存
putErrorCode(errorCodeRespDTO.getCode(), errorCodeRespDTO.getMessage());
// 记录下更新时间,方便增量更新
maxUpdateTime = DateUtils.max(maxUpdateTime, errorCodeRespDTO.getUpdateTime());
});
// 刷新错误码的缓存
errorCodeRespDTOs.forEach(errorCodeRespDTO -> {
// 写入到错误码的缓存
putErrorCode(errorCodeRespDTO.getCode(), errorCodeRespDTO.getMessage());
// 记录下更新时间,方便增量更新
maxUpdateTime = DateUtils.max(maxUpdateTime, errorCodeRespDTO.getUpdateTime());
});
} catch (Exception ex) {
log.error("[loadErrorCodes0][加载错误码失败({})]", ExceptionUtil.getRootCauseMessage(ex));
}
}

}

0 comments on commit f2ff30e

Please sign in to comment.