generate 记录取消操作

This commit is contained in:
2024-01-26 13:12:04 +08:00
parent aa270b5f7d
commit 4c0dd27332
5 changed files with 73 additions and 9 deletions

View File

@@ -61,8 +61,10 @@ public class GenerateController {
@ApiOperation(value = "取消继续生成")
@GetMapping("/stopWaiting")
public Response<String> stopWaiting(@RequestParam("uniqueId") String uniqueId) {
generateService.cancelGenerate(uniqueId);
public Response<String> stopWaiting(@RequestParam("userId") Long userId,
@RequestParam("uniqueId") String uniqueId,
@RequestParam("timeZone") String timeZone) {
generateService.cancelGenerate(userId, uniqueId, timeZone);
return Response.success("stop waiting successfully");
}

View File

@@ -0,0 +1,7 @@
package com.ai.da.mapper;
import com.ai.da.common.config.mybatis.plus.CommonMapper;
import com.ai.da.mapper.entity.GenerateCancel;
public interface GenerateCancelMapper extends CommonMapper<GenerateCancel> {
}

View File

@@ -0,0 +1,44 @@
package com.ai.da.mapper.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import java.util.Date;
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("t_generate_cancel")
public class GenerateCancel {
/**
* ID
*/
@TableId(value = "id", type = IdType.AUTO)
private Long id;
/**
* 用户ID
*/
private Long accountId;
/**
* 唯一id(任务id)
*/
private String uniqueId;
/**
* 创建时间
*/
private Date createDate;
public GenerateCancel(Long accountId, String uniqueId, Date createDate) {
this.accountId = accountId;
this.uniqueId = uniqueId;
this.createDate = createDate;
}
}

View File

@@ -31,6 +31,6 @@ public interface GenerateService extends IService<Generate> {
Long getRankPosition(String uniqueId);
void cancelGenerate(String uniqueId);
void cancelGenerate(Long userId, String uniqueId, String timeZone);
}

View File

@@ -6,6 +6,7 @@ import com.ai.da.common.enums.GenerateModeEnum;
import com.ai.da.common.enums.ModelNameEnum;
import com.ai.da.common.utils.*;
import com.ai.da.mapper.CollectionElementMapper;
import com.ai.da.mapper.GenerateCancelMapper;
import com.ai.da.mapper.GenerateDetailMapper;
import com.ai.da.mapper.GenerateMapper;
import com.ai.da.mapper.entity.*;
@@ -64,6 +65,9 @@ public class GenerateServiceImpl extends ServiceImpl<GenerateMapper, Generate> i
@Resource
private RedisUtil redisUtil;
@Resource
private GenerateCancelMapper generateCancelMapper;
@Value("${redis.key.consumptionOrder}")
private String consumptionOrderKey;
@@ -330,8 +334,10 @@ public class GenerateServiceImpl extends ServiceImpl<GenerateMapper, Generate> i
// long snowflakeId = idWorker.nextId();
int num = 1;
// 判断已经正常生成结果的uuid有相同的id
while (redisUtil.isElementExistsInMap(resultMapKey, uuid) && num < 10) {
// 判断已经正常生成结果的uuid或正在排队的uuid中是否有相同的id
while ((redisUtil.isElementExistsInMap(resultMapKey, uuid) ||
redisUtil.isElementExistsInZSet(consumptionOrderKey, uuid))
&& num < 10) {
uuid = UUID.randomUUID().toString();
num++;
}
@@ -378,7 +384,7 @@ public class GenerateServiceImpl extends ServiceImpl<GenerateMapper, Generate> i
// 排队中,给出当前排序位置,rank从0开始
Long rankPosition = getRankPosition(uniqueId);
// 有9个消费者所以当rank>8即当前请求至少排在第九位时其实际排队位置为9-8+1当rank <=8请求均在处理中
return new GenerateCollectionVO( rankPosition > 8L ? rankPosition - 8 + 1 : 1L);
return new GenerateCollectionVO(rankPosition > 8L ? rankPosition - 8 + 1 : 1L);
}
// 3、判断redis中有没有
@@ -393,7 +399,7 @@ public class GenerateServiceImpl extends ServiceImpl<GenerateMapper, Generate> i
Generate generate = selectByUniqueId(uniqueId);
if (Objects.isNull(generate)) {
// 3.3 还没执行完,给出当前位置
return new GenerateCollectionVO(0L);
return new GenerateCollectionVO(1L);
}
Long generateId = generate.getId();
QueryWrapper<GenerateDetail> qw = new QueryWrapper<>();
@@ -401,7 +407,7 @@ public class GenerateServiceImpl extends ServiceImpl<GenerateMapper, Generate> i
List<GenerateDetail> generateDetails = generateDetailMapper.selectList(qw);
if (CollectionUtils.isEmpty(generateDetails)) {
// 会有这种情况吗存到generate中但是还没存到generateDetail中
return new GenerateCollectionVO(0L);
return new GenerateCollectionVO(1L);
}
List<GenerateCollectionItemVO> generatedCollectionItems = new ArrayList<>();
@@ -423,7 +429,8 @@ public class GenerateServiceImpl extends ServiceImpl<GenerateMapper, Generate> i
}
@Override
public void cancelGenerate(String uniqueId) {
@Transactional(rollbackFor = Exception.class)
public void cancelGenerate(Long userId, String uniqueId, String timeZone) {
// 1、确认当前消息是否还在排队中
Boolean exists = redisUtil.isElementExistsInZSet(consumptionOrderKey, uniqueId);
Boolean flag = Boolean.FALSE;
@@ -446,5 +453,9 @@ public class GenerateServiceImpl extends ServiceImpl<GenerateMapper, Generate> i
pythonService.cancelGenerateTask(uniqueId);
}
}
// 3、考虑加一张表专门用于记录哪些用户在什么时间进行了取消操作
GenerateCancel generateCancel = new GenerateCancel(userId, uniqueId, DateUtil.getByTimeZone(timeZone));
generateCancelMapper.insert(generateCancel);
}
}