generate 记录取消操作
This commit is contained in:
@@ -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);
|
||||
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user