TASK: 1、对于生成失败情况,在数据库添加状态记录,并在getModuleContent时不返回该条记录

This commit is contained in:
2025-07-11 17:56:15 +08:00
parent d0a9c05d4b
commit e361960e51
6 changed files with 90 additions and 4 deletions

View File

@@ -189,6 +189,7 @@ public class GenerateConsumer {
} else {
// 修改redis中的数据状态为exception
String key = toProductImageResultKey + ":" + generateResult.get("tasks_id");
generateService.updateToProductTaskStatus(generateResult.get("tasks_id"), "Fail");
redisUtil.addToString(key, new Gson().toJson(new GenerateResultVO(generateResult.get("tasks_id"), null, null, "Fail")), CommonConstant.GENERATE_RESULT_EXPIRE_TIME);
// 将异常信息存到exception中
HashMap<String, String> exceptionInfo = new HashMap<>();
@@ -239,6 +240,7 @@ public class GenerateConsumer {
} else {
// 修改redis中的数据状态为exception
String key = relightResultKey + ":" + generateResult.get("tasks_id");
generateService.updateToProductTaskStatus(generateResult.get("tasks_id"), "Fail");
redisUtil.addToString(key, new Gson().toJson(new GenerateResultVO(generateResult.get("tasks_id"), null, null, "Fail")), CommonConstant.GENERATE_RESULT_EXPIRE_TIME);
// 将异常信息存到exception中
HashMap<String, String> exceptionInfo = new HashMap<>();
@@ -287,6 +289,7 @@ public class GenerateConsumer {
} else {
// 修改redis中的数据状态为exception
String key = generateResultKey + ":" + generateResult.get("tasks_id");
generateService.updatePoseTransferStatus(generateResult.get("tasks_id"), "Fail");
redisUtil.addToString(key, new Gson().toJson(new PoseTransformationVO(null, generateResult.get("tasks_id"),null, null, null, (byte)0, "Fail")), CommonConstant.GENERATE_RESULT_EXPIRE_TIME);
// 将异常信息存到exception中
HashMap<String, String> exceptionInfo = new HashMap<>();
@@ -402,6 +405,7 @@ public class GenerateConsumer {
} else {
// 修改redis中的数据状态为exception
String key = toProductImageResultKey + ":" + generateResult.get("task_id");
generateService.updateToProductTaskStatus(generateResult.getString("task_id"), "Fail");
redisUtil.addToString(key, new Gson().toJson(new GenerateResultVO(generateResult.getString("tasks_id"), null, null, "Fail")), CommonConstant.GENERATE_RESULT_EXPIRE_TIME);
// 将异常信息存到exception中
HashMap<String, String> exceptionInfo = new HashMap<>();
@@ -465,6 +469,7 @@ public class GenerateConsumer {
} else {
// 修改redis中的数据状态为exception
String key = relightResultKey + ":" + generateResult.get("task_id");
generateService.updateToProductTaskStatus(generateResult.getString("task_id"), "Fail");
redisUtil.addToString(key, new Gson().toJson(new GenerateResultVO(generateResult.getString("tasks_id"), null, null, "Fail")), CommonConstant.GENERATE_RESULT_EXPIRE_TIME);
// 将异常信息存到exception中
HashMap<String, String> exceptionInfo = new HashMap<>();
@@ -520,6 +525,7 @@ public class GenerateConsumer {
} else {
// 修改redis中的数据状态为exception
String key = generateResultKey + ":" + generateResult.getString("task_id");
generateService.updatePoseTransferStatus(generateResult.getString("task_id"), "Fail");
redisUtil.addToString(key, new Gson().toJson(new PoseTransformationVO(null, generateResult.getString("task_id"),null, null, null, (byte)0, "Fail")), CommonConstant.GENERATE_RESULT_EXPIRE_TIME);
// 将异常信息存到exception中
HashMap<String, String> exceptionInfo = new HashMap<>();

View File

@@ -33,6 +33,8 @@ public class PoseTransformation extends BaseEntity {
private String modelName;
private String taskStatus;
public PoseTransformation() {
}

View File

@@ -60,4 +60,6 @@ public class ToProductImageResult implements Serializable {
private String taskIdBatch;
private String modelName;
private String taskStatus;
}

View File

@@ -22,6 +22,8 @@ public interface GenerateService extends IService<Generate> {
void processToProductImageResult(String taskId, String url, String category);
void updateToProductTaskStatus(String taskId, String status);
GenerateLikeVO generateLike(GenerateLikeDTO generateLikeDTO);
Boolean generateDislike(Long generateDetailId, String timeZone);
@@ -56,6 +58,8 @@ public interface GenerateService extends IService<Generate> {
List<PoseTransformationVO> getPoseTransformationResult(List<String> taskIdList);
void updatePoseTransferStatus(String taskId, String status);
List<PoseTransformationVO> getPoseTransformationResultList(Long projectId, boolean like);
CollectionSort disOrLikePose(Long transformedId, String likeOrDislike, Long projectId, Long sortLikeParentId);

View File

@@ -318,6 +318,7 @@ public class GenerateServiceImpl extends ServiceImpl<GenerateMapper, Generate> i
}
ToProductImageResult toProductImageResult = toProductImageResults.get(0);
toProductImageResult.setUrl(url);
toProductImageResult.setResultType("Success");
// toProductImageResult.setResultType("ToProductImage");
toProductImageResultMapper.updateById(toProductImageResult);
@@ -339,6 +340,19 @@ public class GenerateServiceImpl extends ServiceImpl<GenerateMapper, Generate> i
}
}
public void updateToProductTaskStatus(String taskId, String status){
QueryWrapper<ToProductImageResult> qw = new QueryWrapper<>();
qw.lambda().eq(ToProductImageResult::getTaskId, taskId);
List<ToProductImageResult> toProductImageResults = toProductImageResultMapper.selectList(qw);
if (!CollectionUtils.isEmpty(toProductImageResults)) {
ToProductImageResult toProductImageResult = toProductImageResults.get(0);
if (StringUtil.isNullOrEmpty(toProductImageResult.getTaskStatus()) || !toProductImageResult.getTaskStatus().equals(status)){
toProductImageResult.setTaskStatus(status);
toProductImageResultMapper.updateById(toProductImageResult);
}
}
}
private void validateGeneraType(Generate generate, String text, Long elementId) {
String generateType = "";
if (StringUtil.isNullOrEmpty(text.trim()) && Objects.isNull(elementId)) {
@@ -832,6 +846,7 @@ public class GenerateServiceImpl extends ServiceImpl<GenerateMapper, Generate> i
pythonService.bright(url, toProductImageResult.getBrightenValue());
}
toProductImageResult.setUrl(url);
toProductImageResult.setTaskStatus("Success");
// toProductImageResult.setResultType("Relight");
toProductImageResultMapper.updateById(toProductImageResult);
@@ -1221,6 +1236,8 @@ public class GenerateServiceImpl extends ServiceImpl<GenerateMapper, Generate> i
poseTransformation.setProductImage(productImage);
poseTransformation.setPoseId(poseId);
poseTransformation.setIsLiked((byte) 1);
String taskStatus = flag ? "Executing" : "Fail";
poseTransformation.setTaskStatus(taskStatus);
poseTransformation.setCreateTime(LocalDateTime.now());
poseTransformationMapper.insert(poseTransformation);
// 当需要默认like
@@ -1280,6 +1297,7 @@ public class GenerateServiceImpl extends ServiceImpl<GenerateMapper, Generate> i
poseTransformation.setGifUrl(gifUrl);
poseTransformation.setVideoUrl(videoUrl);
poseTransformation.setFirstFrameUrl(imageUrl);
poseTransformation.setTaskStatus("Success");
poseTransformation.setUpdateTime(LocalDateTime.now());
poseTransformationMapper.updateById(poseTransformation);
@@ -1336,6 +1354,16 @@ public class GenerateServiceImpl extends ServiceImpl<GenerateMapper, Generate> i
return poseTransformationVOS;
}
public void updatePoseTransferStatus(String taskId, String status){
PoseTransformation poseTransformation = poseTransformationMapper.selectOne(new QueryWrapper<PoseTransformation>().eq("unique_id", taskId));
if (StringUtil.isNullOrEmpty(poseTransformation.getTaskStatus()) || !status.equals(poseTransformation.getTaskStatus())){
poseTransformation.setTaskStatus(status);
poseTransformation.setUpdateTime(LocalDateTime.now());
poseTransformationMapper.updateById(poseTransformation);
}
}
private ToProductImageResult getProductResultByPath(String minioPath) {
QueryWrapper<ToProductImageResult> qw = new QueryWrapper<>();
qw.lambda().eq(ToProductImageResult::getUrl, minioPath);
@@ -1597,6 +1625,7 @@ public class GenerateServiceImpl extends ServiceImpl<GenerateMapper, Generate> i
poseTransformation.setGifUrl(gifUrl);
poseTransformation.setVideoUrl(videoUrl);
poseTransformation.setFirstFrameUrl(imageUrl);
poseTransformation.setTaskStatus("Success");
poseTransformation.setUpdateTime(LocalDateTime.now());
poseTransformationMapper.updateById(poseTransformation);
@@ -2319,7 +2348,8 @@ public class GenerateServiceImpl extends ServiceImpl<GenerateMapper, Generate> i
log.info("flux 发起生成请求返回结果: {}", respObj);
String taskId = respObj.getStr("id");
if (StringUtil.isNullOrEmpty(taskId)) {
return null;
log.error("flux生成任务创建失败func {} requestBody:{}", func.getName(), requestBody);
throw new BusinessException("Failed to generate task. Please retry later.");
}
String pollingUrl = respObj.getStr("polling_url");
@@ -2348,10 +2378,11 @@ public class GenerateServiceImpl extends ServiceImpl<GenerateMapper, Generate> i
case "Task not found":
return "Fail";
case "Pending":
return "Pending";
case "Request Moderated":
case "Content Moderated":
// 处理中
return "Pending";
// 审核没过
return "Fail";
case "Ready":
// 已完成 获取结果
String fluxResult = JSONUtil.parseObj(respObj.getStr("result")).getStr("sample");

View File

@@ -49,6 +49,7 @@ import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;
@@ -487,6 +488,7 @@ public class UserLikeGroupServiceImpl extends ServiceImpl<UserLikeGroupMapper, U
}
toProductImageResult.setImageStrength(toProductImageDTO.getImageStrength());
toProductImageResult.setResultType(CollectionType.TO_PRODUCT_IMAGE.getValue());
toProductImageResult.setTaskStatus("Pending");
toProductImageResultMapper.insert(toProductImageResult);
// toProductImageResult.setUrl(minioUtil.getPresignedUrl(toProductImageResult.getUrl(), 24 * 60));
// 先判断是否需要默认like
@@ -541,6 +543,7 @@ public class UserLikeGroupServiceImpl extends ServiceImpl<UserLikeGroupMapper, U
}
toProductImageResult.setImageStrength(toProductImageDTO.getImageStrength());
toProductImageResult.setResultType(CollectionType.TO_PRODUCT_IMAGE.getValue());
toProductImageResult.setTaskStatus("Pending");
toProductImageResultMapper.insert(toProductImageResult);
// toProductImageResult.setUrl(minioUtil.getPresignedUrl(toProductImageResult.getUrl(), 24 * 60));
// 先判断是否需要默认like
@@ -754,8 +757,12 @@ public class UserLikeGroupServiceImpl extends ServiceImpl<UserLikeGroupMapper, U
String objectName = project.getAccountId() + "/product_image/" + taskId + ".png";
String fluxResult = generateService.getFluxResult(taskId, objectName);
if (StringUtil.isNullOrEmpty(fluxResult)){
toProductImageResult.setTaskStatus("Fail");
toProductImageResultMapper.updateById(toProductImageResult);
results.add(new MagicToolResultVO(taskId, "Fail"));
} else if (fluxResult.equals("Fail") || fluxResult.equals("Pending")) {
toProductImageResult.setTaskStatus(fluxResult);
toProductImageResultMapper.updateById(toProductImageResult);
results.add(new MagicToolResultVO(taskId, fluxResult));
} else {
results.add(processFluxResult(fluxResult, toProductImageResult, taskId));
@@ -838,6 +845,7 @@ public class UserLikeGroupServiceImpl extends ServiceImpl<UserLikeGroupMapper, U
}
private MagicToolResultVO processFluxResult(String fluxImgMinioPath, ToProductImageResult toProductImageResult, String taskId){
toProductImageResult.setTaskStatus("Success");
toProductImageResult.setUrl(fluxImgMinioPath);
toProductImageResultMapper.updateById(toProductImageResult);
@@ -1172,6 +1180,7 @@ public class UserLikeGroupServiceImpl extends ServiceImpl<UserLikeGroupMapper, U
}
toProductImageResult.setDirection(toProductImageDTO.getDirection());
toProductImageResult.setResultType(CollectionType.RELIGHT.getValue());
toProductImageResult.setTaskStatus("Pending");
toProductImageResultMapper.insert(toProductImageResult);
// toProductImageResult.setUrl(minioUtil.getPresignedUrl(toProductImageResult.getUrl(), 24 * 60));
// 先判断是否需要默认like
@@ -1216,6 +1225,7 @@ public class UserLikeGroupServiceImpl extends ServiceImpl<UserLikeGroupMapper, U
}
toProductImageResult.setDirection(toProductImageDTO.getDirection());
toProductImageResult.setResultType(CollectionType.RELIGHT.getValue());
toProductImageResult.setTaskStatus("Pending");
toProductImageResultMapper.insert(toProductImageResult);
// toProductImageResult.setUrl(minioUtil.getPresignedUrl(toProductImageResult.getUrl(), 24 * 60));
// 先判断是否需要默认like
@@ -1266,8 +1276,12 @@ public class UserLikeGroupServiceImpl extends ServiceImpl<UserLikeGroupMapper, U
String fluxResult = generateService.getFluxResult(taskId, objectName);
if (StringUtil.isNullOrEmpty(fluxResult)){
toProductImageResult.setTaskStatus("Fail");
toProductImageResultMapper.updateById(toProductImageResult);
results.add(new MagicToolResultVO(taskId, "Fail"));
} else if (fluxResult.equals("Fail") || fluxResult.equals("Pending")) {
toProductImageResult.setTaskStatus(fluxResult);
toProductImageResultMapper.updateById(toProductImageResult);
results.add(new MagicToolResultVO(taskId, fluxResult));
} else {
results.add(processFluxResult(fluxResult, toProductImageResult, taskId));
@@ -1901,6 +1915,9 @@ public class UserLikeGroupServiceImpl extends ServiceImpl<UserLikeGroupMapper, U
for (CollectionSort userLikeSort : childSortList) {
if (userLikeSort.getRelationType().equals(CollectionType.TO_PRODUCT_IMAGE.getValue())) {
ToProductImageResult toProductImageResult = toProductImageResultMapper.selectById(userLikeSort.getRelationId());
if (isGenerateTaskFailed(toProductImageResult.getTaskStatus(), toProductImageResult.getCreateTime())){
continue;
}
toProductImageResult.setUrl(getMinioUrl(toProductImageResult.getUrl()));
ToProductImageResultVO toProductImageResultVO = CopyUtil.copyObject(toProductImageResult, ToProductImageResultVO.class);
@@ -1922,6 +1939,9 @@ public class UserLikeGroupServiceImpl extends ServiceImpl<UserLikeGroupMapper, U
childList.add(toProductImageResultVO);
} else if (userLikeSort.getRelationType().equals(CollectionType.RELIGHT.getValue())) {
ToProductImageResult toProductImageResult = toProductImageResultMapper.selectById(userLikeSort.getRelationId());
if (isGenerateTaskFailed(toProductImageResult.getTaskStatus(), toProductImageResult.getCreateTime())){
continue;
}
toProductImageResult.setUrl(getMinioUrl(toProductImageResult.getUrl()));
ToProductImageResultVO toProductImageResultVO = CopyUtil.copyObject(toProductImageResult, ToProductImageResultVO.class);
@@ -1943,6 +1963,9 @@ public class UserLikeGroupServiceImpl extends ServiceImpl<UserLikeGroupMapper, U
childList.add(toProductImageResultVO);
} else if (userLikeSort.getRelationType().equals(CollectionType.POSE_TRANSFORM.getValue())) {
PoseTransformation item = poseTransformationMapper.selectById(userLikeSort.getRelationId());
if (isGenerateTaskFailed(item.getTaskStatus(), item.getCreateTime())){
continue;
}
PoseTransformationVO poseTransformationVO = new PoseTransformationVO();
poseTransformationVO.setId(item.getId());
poseTransformationVO.setProductImage(getMinioUrl(item.getProductImage()));
@@ -2131,6 +2154,22 @@ public class UserLikeGroupServiceImpl extends ServiceImpl<UserLikeGroupMapper, U
return collectionElementRelModel;
}
private boolean isGenerateTaskFailed(String taskStatus, LocalDateTime createTime) {
// 1. 先判断TaskStatus字段是否有值若有值且为Fail则失败
if (!StringUtil.isNullOrEmpty(taskStatus)) {
return "Fail".equals(taskStatus);
}
// 2. 若taskStatus无值判断记录创建时间距离现在是否超过1小时
if (createTime == null) {
// 如果创建时间为null视为失败根据业务需求决定
return true;
}
// 计算当前时间与创建时间的差值
Duration duration = Duration.between(createTime, LocalDateTime.now());
// 判断是否超过1小时3600秒
return duration.toHours() >= 1;
}
@Override
@Transactional
public ModuleChooseVO saveModuleContent(ModuleSaveDTO moduleSaveDTO) {
@@ -2787,6 +2826,7 @@ public class UserLikeGroupServiceImpl extends ServiceImpl<UserLikeGroupMapper, U
ToProductImageResult toProductImageResult = toProductImageResultMapper.selectOne(qw);
if (Objects.nonNull(toProductImageResult)) {
toProductImageResult.setUrl(url);
toProductImageResult.setTaskStatus("Success");
toProductImageResultMapper.updateById(toProductImageResult);
String taskIdBatch = toProductImageResult.getTaskIdBatch();
log.info("toProductImage云生成 batchTaskId:" + taskIdBatch + " 完成数+1");
@@ -2824,7 +2864,8 @@ public class UserLikeGroupServiceImpl extends ServiceImpl<UserLikeGroupMapper, U
pythonService.bright(url, toProductImageResult.getBrightenValue());
}
toProductImageResult.setUrl(url);
toProductImageResult.setResultType("Relight");
// toProductImageResult.setResultType("Relight");
toProductImageResult.setTaskStatus("Success");
toProductImageResultMapper.updateById(toProductImageResult);
String taskIdBatch = toProductImageResult.getTaskIdBatch();