refactor: 第一批低风险收敛——任务缓存服务泛型化 + 前端死代码清理与状态映射统一
This commit is contained in:
+15
-127
@@ -1,23 +1,23 @@
|
||||
package com.nanri.aiimage.modules.appearancepatent.service;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.nanri.aiimage.modules.appearancepatent.model.dto.AppearancePatentResultRowDto;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import com.nanri.aiimage.modules.task.service.AbstractPendingRowTaskCacheService;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 外观专利模块任务缓存服务:待处理行队列 + 心跳复用通用基类
|
||||
* {@link AbstractPendingRowTaskCacheService};本模块额外的行级两态标记
|
||||
* (处理中 / 已完成)与轮次计数留在本类。
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class AppearancePatentTaskCacheService {
|
||||
|
||||
private static final long TTL_HOURS = 24;
|
||||
public class AppearancePatentTaskCacheService extends AbstractPendingRowTaskCacheService<AppearancePatentResultRowDto> {
|
||||
|
||||
/**
|
||||
* 处理中标记 TTL。取 15 分钟:合批后单批实测约 30–120 秒(合批前 10 行要 220 秒),
|
||||
@@ -26,117 +26,16 @@ public class AppearancePatentTaskCacheService {
|
||||
*/
|
||||
private static final Duration IN_FLIGHT_TTL = Duration.ofMinutes(15);
|
||||
|
||||
private final StringRedisTemplate stringRedisTemplate;
|
||||
private final ObjectMapper objectMapper;
|
||||
private static final long TTL_HOURS = 24;
|
||||
|
||||
public void appendPendingRow(Long taskId, String scopeHash, Integer chunkIndex, AppearancePatentResultRowDto row) {
|
||||
if (taskId == null || taskId <= 0 || scopeHash == null || scopeHash.isBlank() || chunkIndex == null || row == null) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
stringRedisTemplate.opsForList().rightPush(
|
||||
pendingRowsKey(taskId),
|
||||
objectMapper.writeValueAsString(new PendingRow(scopeHash, chunkIndex, row))
|
||||
);
|
||||
stringRedisTemplate.expire(pendingRowsKey(taskId), Duration.ofHours(TTL_HOURS));
|
||||
touchTaskHeartbeat(taskId);
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
public AppearancePatentTaskCacheService(StringRedisTemplate stringRedisTemplate,
|
||||
ObjectMapper objectMapper) {
|
||||
super("appearance-patent", AppearancePatentResultRowDto.class, stringRedisTemplate, objectMapper);
|
||||
}
|
||||
|
||||
public long pendingRowCount(Long taskId) {
|
||||
Long size;
|
||||
try {
|
||||
size = stringRedisTemplate.opsForList().size(pendingRowsKey(taskId));
|
||||
} catch (Exception ex) {
|
||||
log.warn("[appearance-patent-cache] pending row count degraded taskId={} msg={}", taskId, ex.getMessage());
|
||||
return 0L;
|
||||
}
|
||||
return size == null ? 0L : size;
|
||||
}
|
||||
|
||||
public List<PendingRow> drainPendingRows(Long taskId, int limit) {
|
||||
if (taskId == null || taskId <= 0 || limit <= 0) {
|
||||
return List.of();
|
||||
}
|
||||
String key = pendingRowsKey(taskId);
|
||||
List<String> values;
|
||||
try {
|
||||
values = stringRedisTemplate.opsForList().range(key, 0, limit - 1L);
|
||||
} catch (Exception ex) {
|
||||
log.warn("[appearance-patent-cache] drain range degraded taskId={} msg={}", taskId, ex.getMessage());
|
||||
return List.of();
|
||||
}
|
||||
if (values == null || values.isEmpty()) {
|
||||
return List.of();
|
||||
}
|
||||
try {
|
||||
stringRedisTemplate.opsForList().trim(key, values.size(), -1);
|
||||
} catch (Exception ex) {
|
||||
log.warn("[appearance-patent-cache] drain trim degraded taskId={} msg={}", taskId, ex.getMessage());
|
||||
return List.of();
|
||||
}
|
||||
List<PendingRow> rows = new ArrayList<>();
|
||||
for (String value : values) {
|
||||
if (value == null || value.isBlank()) {
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
rows.add(objectMapper.readValue(value, PendingRow.class));
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
}
|
||||
touchTaskHeartbeat(taskId);
|
||||
return rows;
|
||||
}
|
||||
|
||||
public void touchTaskHeartbeat(Long taskId) {
|
||||
if (taskId == null || taskId <= 0) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
stringRedisTemplate.opsForValue().set(
|
||||
heartbeatKey(taskId),
|
||||
String.valueOf(Instant.now().toEpochMilli()),
|
||||
Duration.ofHours(TTL_HOURS)
|
||||
);
|
||||
} catch (Exception ex) {
|
||||
log.warn("[appearance-patent-cache] touch heartbeat degraded taskId={} msg={}", taskId, ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public long getTaskHeartbeatMillis(Long taskId) {
|
||||
if (taskId == null || taskId <= 0) {
|
||||
return 0L;
|
||||
}
|
||||
String raw;
|
||||
try {
|
||||
raw = stringRedisTemplate.opsForValue().get(heartbeatKey(taskId));
|
||||
} catch (Exception ex) {
|
||||
log.warn("[appearance-patent-cache] get heartbeat degraded taskId={} msg={}", taskId, ex.getMessage());
|
||||
return 0L;
|
||||
}
|
||||
if (raw == null || raw.isBlank()) {
|
||||
return 0L;
|
||||
}
|
||||
try {
|
||||
return Long.parseLong(raw);
|
||||
} catch (NumberFormatException ignored) {
|
||||
return 0L;
|
||||
}
|
||||
}
|
||||
|
||||
public void deleteTaskCache(Long taskId) {
|
||||
if (taskId == null || taskId <= 0) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
stringRedisTemplate.delete(pendingRowsKey(taskId));
|
||||
stringRedisTemplate.delete(heartbeatKey(taskId));
|
||||
stringRedisTemplate.delete(processedRowsKey(taskId));
|
||||
} catch (Exception ex) {
|
||||
log.warn("[appearance-patent-cache] delete degraded taskId={} msg={}", taskId, ex.getMessage());
|
||||
}
|
||||
@Override
|
||||
protected void deleteExtraTaskCacheKeys(Long taskId) {
|
||||
stringRedisTemplate.delete(processedRowsKey(taskId));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -277,15 +176,4 @@ public class AppearancePatentTaskCacheService {
|
||||
private String processedRowKey(Long taskId, String rowKey) {
|
||||
return processedRowsKey(taskId) + ":" + rowKey;
|
||||
}
|
||||
|
||||
private String pendingRowsKey(Long taskId) {
|
||||
return "appearance-patent:task:pending-rows:" + taskId;
|
||||
}
|
||||
|
||||
private String heartbeatKey(Long taskId) {
|
||||
return "appearance-patent:task:heartbeat:" + taskId;
|
||||
}
|
||||
|
||||
public record PendingRow(String scopeHash, Integer chunkIndex, AppearancePatentResultRowDto row) {
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user