BUGFIX: 创建超过3分钟的搭配请求设置为失败
All checks were successful
git commit 控制 连卡佛 back-java prod 分支构建部署 / build_and_deploy (push) Has been skipped
All checks were successful
git commit 控制 连卡佛 back-java prod 分支构建部署 / build_and_deploy (push) Has been skipped
This commit is contained in:
@@ -21,7 +21,10 @@ public enum StatusEnum {
|
||||
FAILED(2),
|
||||
|
||||
@Schema(description = "运行中")
|
||||
RUNNING(3);
|
||||
RUNNING(3),
|
||||
|
||||
@Schema(description = "重试中")
|
||||
RETRYING(4);
|
||||
|
||||
private int code;
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ public class OutfitCallbackDTO {
|
||||
|
||||
private String outfit_id;
|
||||
|
||||
// 取值范围:ok || failed || stop
|
||||
// 取值范围:ok || failed || stop || retrying
|
||||
private String status;
|
||||
|
||||
private String path;
|
||||
|
||||
@@ -101,6 +101,7 @@ public class StyleServiceImpl extends ServiceImpl<StyleMapper, Style> implements
|
||||
|
||||
String key = RedisURIConstants.outfitResultCache + requestId;
|
||||
OutfitResultVO outfitResultVO = new OutfitResultVO(style.getId(), requestId, StatusEnum.PENDING.name());
|
||||
outfitResultVO.setCreateTimeStamp(System.currentTimeMillis());
|
||||
cacheUtil.setCache(key, outfitResultVO, RedisURIConstants.verifyCodeTimeout);
|
||||
}
|
||||
return requestIds;
|
||||
@@ -133,9 +134,10 @@ public class StyleServiceImpl extends ServiceImpl<StyleMapper, Style> implements
|
||||
return;
|
||||
}
|
||||
|
||||
if ("ok".equals(callbackDTO.getStatus()) || "stop".equals(callbackDTO.getStatus())) {
|
||||
// 1. 判断path是否为空,是 -> 不做任何处理
|
||||
if (StringUtil.isNullOrEmpty(callbackDTO.getPath())) {
|
||||
if (!"retrying".equals(callbackDTO.getStatus())
|
||||
&& !"failed".equals(callbackDTO.getStatus())
|
||||
&& StringUtil.isNullOrEmpty(callbackDTO.getPath())) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -158,15 +160,34 @@ public class StyleServiceImpl extends ServiceImpl<StyleMapper, Style> implements
|
||||
// 由于数据变化较频繁,考虑存到redis
|
||||
if (outfitResult instanceof OutfitResultVO) {
|
||||
((OutfitResultVO) outfitResult).setPath(minioUtil.getPresignedUrl(callbackDTO.getPath(), CommonConstants.MINIO_PATH_TIMEOUT));
|
||||
String status = "ok".equals(callbackDTO.getStatus()) ? StatusEnum.RUNNING.name() : StatusEnum.SUCCEEDED.name();
|
||||
String status;
|
||||
switch(callbackDTO.getStatus()) {
|
||||
case "ok":
|
||||
status = StatusEnum.RUNNING.name();
|
||||
break;
|
||||
case "stop":
|
||||
status = StatusEnum.SUCCEEDED.name();
|
||||
break;
|
||||
case "failed":
|
||||
status = StatusEnum.FAILED.name();
|
||||
break;
|
||||
case "retrying":
|
||||
status = StatusEnum.PENDING.name();
|
||||
((OutfitResultVO) outfitResult).setCreateTimeStamp(System.currentTimeMillis());
|
||||
((OutfitResultVO) outfitResult).setPath(null);
|
||||
break;
|
||||
default:
|
||||
log.error("outfit_id为{},回调状态未知{}", requestId, callbackDTO.getStatus());
|
||||
status = "failed";
|
||||
}
|
||||
|
||||
((OutfitResultVO) outfitResult).setStatus(status);
|
||||
cacheUtil.setCache(key, outfitResult, RedisURIConstants.outfitResultTimeout);
|
||||
}
|
||||
}
|
||||
|
||||
// 生成结束或失败时更新数据库
|
||||
if ("stop".equals(callbackDTO.getStatus()) || "failed".equals(callbackDTO.getStatus())) {
|
||||
String requestId = callbackDTO.getOutfit_id();
|
||||
// String requestId = callbackDTO.getOutfit_id();
|
||||
|
||||
LambdaQueryWrapper<Style> queryWrapper = new LambdaQueryWrapper<>();
|
||||
queryWrapper.eq(Style::getPythonRequestId, requestId);
|
||||
@@ -198,6 +219,7 @@ public class StyleServiceImpl extends ServiceImpl<StyleMapper, Style> implements
|
||||
public List<OutfitResultVO> getOutfitResult(List<String> requestIDs) {
|
||||
ArrayList<OutfitResultVO> resultVOS = new ArrayList<>();
|
||||
ArrayList<String> reQueryIds = new ArrayList<>();
|
||||
List<String> expiredIds = new ArrayList<>();
|
||||
// 优先从redis中获取结果,没有再从数据库中查询
|
||||
for (String requestID : requestIDs) {
|
||||
String key = RedisURIConstants.outfitResultCache + requestID;
|
||||
@@ -207,8 +229,25 @@ public class StyleServiceImpl extends ServiceImpl<StyleMapper, Style> implements
|
||||
reQueryIds.add(requestID);
|
||||
continue;
|
||||
}
|
||||
|
||||
// 判断这条记录的状态是否为成功或者失败,否,判断这条记录的创建时间是否超过3分钟,否,继续往后,是,设置为失败并更新数据库
|
||||
if (outfit instanceof OutfitResultVO) {
|
||||
if ((((OutfitResultVO) outfit).getStatus().equals(StatusEnum.PENDING.name())
|
||||
|| ((OutfitResultVO) outfit).getStatus().equals(StatusEnum.RUNNING.name()))
|
||||
&& isExpired(((OutfitResultVO) outfit).getCreateTimeStamp())) {
|
||||
// 设置状态为失败
|
||||
((OutfitResultVO) outfit).setStatus(StatusEnum.FAILED.name());
|
||||
// 更新redis和数据库状态
|
||||
expiredIds.add(requestID);
|
||||
cacheUtil.setCache(key, outfit, RedisURIConstants.outfitResultTimeout);
|
||||
}
|
||||
resultVOS.add((OutfitResultVO) outfit);
|
||||
}
|
||||
}
|
||||
|
||||
if (!expiredIds.isEmpty()) {
|
||||
batchUpdateExpiredRecords(expiredIds);
|
||||
}
|
||||
|
||||
if (!reQueryIds.isEmpty()) {
|
||||
LambdaQueryWrapper<Style> queryWrapper = new LambdaQueryWrapper<>();
|
||||
@@ -216,6 +255,7 @@ public class StyleServiceImpl extends ServiceImpl<StyleMapper, Style> implements
|
||||
|
||||
List<Style> list = list(queryWrapper);
|
||||
if (!list.isEmpty()) {
|
||||
// 在数据库中的数据,都是处于成功或失败的状态
|
||||
for (Style style : list) {
|
||||
OutfitResultVO outfitResultVO = new OutfitResultVO();
|
||||
outfitResultVO.setId(style.getId());
|
||||
@@ -232,6 +272,34 @@ public class StyleServiceImpl extends ServiceImpl<StyleMapper, Style> implements
|
||||
return resultVOS;
|
||||
}
|
||||
|
||||
// 3分钟的毫秒数
|
||||
private static final long THREE_MINUTES_IN_MILLIS = 3 * 60 * 1000;
|
||||
|
||||
private boolean isExpired(long createTimeMillis) {
|
||||
long currentTimeMillis = System.currentTimeMillis();
|
||||
return (currentTimeMillis - createTimeMillis) >= THREE_MINUTES_IN_MILLIS;
|
||||
}
|
||||
|
||||
private void batchUpdateExpiredRecords(List<String> expiredIds) {
|
||||
try {
|
||||
if (CollectionUtils.isEmpty(expiredIds)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 批量更新,减少数据库压力
|
||||
lambdaUpdate()
|
||||
.in(Style::getPythonRequestId, expiredIds)
|
||||
.set(Style::getGenerationStatus, StatusEnum.FAILED.getCode())
|
||||
.set(Style::getUpdatedTime, LocalDateTime.now())
|
||||
.update();
|
||||
|
||||
log.info("批量更新过期记录为失败状态,数量:{}", expiredIds.size());
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("批量更新过期记录失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setFavoriteStyle(Long styleId) {
|
||||
if (styleId == null) {
|
||||
|
||||
@@ -26,6 +26,8 @@ public class OutfitResultVO {
|
||||
)
|
||||
private String status;
|
||||
|
||||
private Long createTimeStamp;
|
||||
|
||||
public OutfitResultVO(Long id, String requestId, String status) {
|
||||
this.id = id;
|
||||
this.requestId = requestId;
|
||||
|
||||
Reference in New Issue
Block a user