refactor: 第二批收敛——公共 Excel 写出器 + 任务收尾零件下沉 + 前端上传区/任务面板抽取

This commit is contained in:
2026-10-01 15:00:52 +08:00
parent 4b29a150f3
commit fa4bd57016
32 changed files with 2467 additions and 2826 deletions
@@ -0,0 +1,190 @@
package com.nanri.aiimage.modules.task.service;
import com.nanri.aiimage.common.exception.BusinessCodes;
import com.nanri.aiimage.common.exception.BusinessException;
import com.nanri.aiimage.config.TaskPressureProperties;
import com.nanri.aiimage.modules.task.mapper.FileResultMapper;
import com.nanri.aiimage.modules.task.mapper.FileTaskMapper;
import com.nanri.aiimage.modules.task.model.entity.FileResultEntity;
import com.nanri.aiimage.modules.task.model.entity.FileTaskEntity;
import lombok.extern.slf4j.Slf4j;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
/**
* 业务模块 *TaskService 收尾/执行骨架的公共零件(无状态静态工具):
* 任务实体加载(模块缓存优先、DB 兜底)、分批任务加载、结果行失败落库、任务分布式锁获取。
*
* <p>此前这些零件在 8 个业务模块的 *TaskService 里逐行同构复制(loadTaskForExecution /
* loadTaskMapByIds / markResultFailed / acquireTaskLock / acquireTaskLockOrThrow /
* selectTasksByIdsInBatches),差异点只有三类,均已参数化:
* <ul>
* <li>模块标识 {@code moduleType}(数据隔离与锁键);</li>
* <li>模块任务缓存服务 {@link AbstractModuleTaskCacheService};</li>
* <li>锁竞争日志标签 {@code logTag}(如 {@code [price-track]})。</li>
* </ul>
*
* <p>已知的非同构变体不强制收敛,调用方保留本地实现:
* PriceTrack 的 loadTaskForExecution 要求缓存行 requestJson 非空(用带 Predicate 的重载);
* ShopMatch 的 loadTaskMapByIds 带缓存/DB 新旧复活防护且缓存 RUNNING+SCHEDULED;
* Withdraw 的 loadTaskMapByIds 先按 {@code limit(50)} 归一化任务 id(本地先归一化再传入即可);
* Brand/AppearancePatent/CollectData/DeleteBrand 等的锁方法带自定义 TTL、等待或日志语义。
*
* <p>依赖仅限 task 模块内部(mapper/entity/锁服务)与 config 包(TaskPressureProperties),
* 不触碰任何业务模块,符合 ArchUnit 边界(task→业务 依赖保持为 0)。
*/
@Slf4j
public final class TaskFinalizeSupport {
private TaskFinalizeSupport() {
}
// ------------------------------------------------------------------
// 任务实体加载(执行前)
// ------------------------------------------------------------------
/** 标准变体:模块缓存命中即直接返回,否则读 DB 并按 moduleType 匹配回填缓存。 */
public static FileTaskEntity loadTaskForExecution(FileTaskMapper fileTaskMapper,
AbstractModuleTaskCacheService<?> cacheService,
String moduleType,
Long taskId) {
return loadTaskForExecution(fileTaskMapper, cacheService, moduleType, taskId, cached -> cached != null);
}
/**
* 带「缓存行可用性」判定的变体:缓存命中但 cachedUsable 判定不可用(如 requestJson 为空)
* 时继续走 DB。cachedUsable 收到的入参可能为 null。
*/
public static FileTaskEntity loadTaskForExecution(FileTaskMapper fileTaskMapper,
AbstractModuleTaskCacheService<?> cacheService,
String moduleType,
Long taskId,
Predicate<FileTaskEntity> cachedUsable) {
Map<Long, FileTaskEntity> cachedTasks = cacheService.getTaskCacheBatch(List.of(taskId));
FileTaskEntity cached = cachedTasks.get(taskId);
if (cachedUsable.test(cached)) {
return cached;
}
FileTaskEntity dbTask = fileTaskMapper.selectById(taskId);
if (dbTask != null && moduleType.equals(dbTask.getModuleType())) {
cacheService.saveTaskCache(dbTask);
}
return dbTask;
}
// ------------------------------------------------------------------
// 批量任务加载
// ------------------------------------------------------------------
/** 标准变体:归一化任务 id → 缓存批量查 → 缺失的按批读 DB(moduleType 匹配),RUNNING 行回填缓存。 */
public static Map<Long, FileTaskEntity> loadTaskMapByIds(FileTaskMapper fileTaskMapper,
TaskPressureProperties taskPressureProperties,
AbstractModuleTaskCacheService<?> cacheService,
String moduleType,
List<Long> taskIds) {
Map<Long, FileTaskEntity> result = new LinkedHashMap<>();
List<Long> normalizedTaskIds = taskIds == null ? List.of() : taskIds.stream()
.filter(taskId -> taskId != null && taskId > 0)
.distinct()
.toList();
if (normalizedTaskIds.isEmpty()) {
return result;
}
Map<Long, FileTaskEntity> cachedTasks = cacheService.getTaskCacheBatch(normalizedTaskIds);
result.putAll(cachedTasks);
List<Long> missingTaskIds = normalizedTaskIds.stream()
.filter(taskId -> !cachedTasks.containsKey(taskId))
.toList();
for (FileTaskEntity dbTask : selectTasksByIdsInBatches(fileTaskMapper, taskPressureProperties, missingTaskIds)) {
if (dbTask == null || !moduleType.equals(dbTask.getModuleType())) {
continue;
}
result.put(dbTask.getId(), dbTask);
if ("RUNNING".equals(dbTask.getStatus())) {
cacheService.saveTaskCache(dbTask);
}
}
return result;
}
/** 按任务压力配置的批量大小分批 selectBatchIds(各模块历史实现逐行一致)。 */
public static List<FileTaskEntity> selectTasksByIdsInBatches(FileTaskMapper fileTaskMapper,
TaskPressureProperties taskPressureProperties,
List<Long> taskIds) {
List<FileTaskEntity> tasks = new ArrayList<>();
if (taskIds == null || taskIds.isEmpty()) {
return tasks;
}
int batchSize = Math.max(1, taskPressureProperties.getDbSelectBatchSize());
for (int start = 0; start < taskIds.size(); start += batchSize) {
int end = Math.min(start + batchSize, taskIds.size());
tasks.addAll(fileTaskMapper.selectBatchIds(taskIds.subList(start, end)));
}
return tasks;
}
// ------------------------------------------------------------------
// 结果行失败落库
// ------------------------------------------------------------------
/**
* 结果行置为失败并清空文件字段后落库(success=0、rowSize=0、文件名/URL/ContentType 置空)。
* errorMessage 原样写入;历史上先把空串归一化为 null 的模块(PatrolDelete/ShopDataCrawl/
* QueryAsin/Withdraw)在调用前自行 {@code blankToNull(message)},保持原行为。
*/
public static void markResultFailed(FileResultMapper fileResultMapper, FileResultEntity row, String message) {
row.setSuccess(0);
row.setErrorMessage(message);
row.setResultFilename(null);
row.setResultFileUrl(null);
row.setResultFileSize(0L);
row.setRowCount(0);
row.setResultContentType(null);
fileResultMapper.updateById(row);
}
// ------------------------------------------------------------------
// 任务分布式锁
// ------------------------------------------------------------------
/** 标准变体:taskId 非法返回 null;否则按模块标识获取锁(默认等待时长)。 */
public static TaskDistributedLockService.LockHandle acquireTaskLock(TaskDistributedLockService taskDistributedLockService,
String moduleType,
Long taskId) {
if (taskId == null || taskId <= 0) {
return null;
}
return taskDistributedLockService.acquire(moduleType, taskId);
}
/** 带 waitMillis 的变体(ShopDataCrawl 等使用)。 */
public static TaskDistributedLockService.LockHandle acquireTaskLock(TaskDistributedLockService taskDistributedLockService,
String moduleType,
Long taskId,
long waitMillis) {
if (taskId == null || taskId <= 0) {
return null;
}
return taskDistributedLockService.acquire(moduleType, taskId, waitMillis);
}
/**
* 锁竞争即拒绝提交并抛 TASK_BUSY(提交路径用)。logTag 传模块标签(如 {@code price-track}),
* 仅用于 warn 日志文案,不影响行为。
*/
public static TaskDistributedLockService.LockHandle acquireTaskLockOrThrow(TaskDistributedLockService taskDistributedLockService,
String moduleType,
Long taskId,
String logTag) {
TaskDistributedLockService.LockHandle lockHandle = acquireTaskLock(taskDistributedLockService, moduleType, taskId);
if (lockHandle == null) {
log.warn("[{}] 任务锁竞争,拒绝本次提交 taskId={}", logTag, taskId);
throw new BusinessException(BusinessCodes.TASK_BUSY, "任务正在处理中,请稍后再试");
}
return lockHandle;
}
}