fix(任务兜底拉取): 候选 cutoff 显式用北京时间 —— 容器默认 UTC, LocalDateTime.now() 与 DB 北京时间字面值比较错位 8 小时, 候选查询恒空(内部认领上线实测踩中)

This commit is contained in:
2026-09-30 11:40:00 +08:00
parent 881d35acfa
commit 279c1641ea
@@ -13,6 +13,7 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.LinkedHashMap;
@@ -39,6 +40,12 @@ public class TaskClientPullService {
private static final String STATUS_RUNNING = "RUNNING";
private static final String STATUS_FAILED = "FAILED";
private static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
/**
* 任务时间口径统一用北京时间:容器默认 UTC 时区,LocalDateTime.now() 与
* DB(datetime 列存北京时间字面值)直接比较会错位 8 小时 —— 表现为候选查询
* 永远为空(created_at 恒大于 UTC 口径的 cutoff)。2026-09-30 上线内部认领时实测踩中。
*/
private static final ZoneId DB_ZONE = ZoneId.of("Asia/Shanghai");
/** 单次拉取上限,防止配置写大后一次认领过多把客户端队列压满。 */
private static final int MAX_LIMIT = 20;
@@ -94,8 +101,8 @@ public class TaskClientPullService {
*/
@PostConstruct
public void logEffectiveConfig() {
log.info("[client-task-pull] 生效配置 enabled={} moduleTypes={} minPendingMinutes={} limit={} handlers={}",
enabled, moduleTypes, minPendingMinutes, Math.max(1, Math.min(limit, MAX_LIMIT)), pullHandlers.keySet());
log.info("[client-task-pull] 生效配置 enabled={} internalEnabled={} moduleTypes={} minPendingMinutes={} limit={} handlers={}",
enabled, internalEnabled, moduleTypes, minPendingMinutes, Math.max(1, Math.min(limit, MAX_LIMIT)), pullHandlers.keySet());
}
/** 拉取并认领该用户"长期未被领取"的任务,按创建时间升序返回,最多 limit 条。 */
@@ -112,7 +119,7 @@ public class TaskClientPullService {
return List.of();
}
long startedAt = System.currentTimeMillis();
LocalDateTime cutoff = LocalDateTime.now().minusMinutes(Math.max(1L, minPendingMinutes));
LocalDateTime cutoff = LocalDateTime.now(DB_ZONE).minusMinutes(Math.max(1L, minPendingMinutes));
int safeLimit = Math.max(1, Math.min(limit, MAX_LIMIT));
List<FileTaskEntity> candidates = fileTaskMapper.selectList(new LambdaQueryWrapper<FileTaskEntity>()
.select(FileTaskEntity::getId, FileTaskEntity::getModuleType, FileTaskEntity::getCreatedAt)
@@ -137,6 +144,7 @@ public class TaskClientPullService {
*/
public List<TaskClientPullVo> pullPendingTasksInternal(List<String> moduleTypes) {
if (!internalEnabled) {
log.info("[client-task-pull] 内部认领端点被调用但开关关闭 types={}", moduleTypes);
return List.of();
}
List<String> types = normalizeModuleTypes(moduleTypes);
@@ -145,7 +153,7 @@ public class TaskClientPullService {
return List.of();
}
long startedAt = System.currentTimeMillis();
LocalDateTime cutoff = LocalDateTime.now().minusMinutes(Math.max(1L, minPendingMinutes));
LocalDateTime cutoff = LocalDateTime.now(DB_ZONE).minusMinutes(Math.max(1L, minPendingMinutes));
int safeLimit = Math.max(1, Math.min(limit, MAX_LIMIT));
List<FileTaskEntity> candidates = fileTaskMapper.selectList(new LambdaQueryWrapper<FileTaskEntity>()
.select(FileTaskEntity::getId, FileTaskEntity::getModuleType, FileTaskEntity::getCreatedAt)