You've already forked FrameTour-BE
refactor(puzzle): 移除 worker 认证逻辑并简化任务处理
- 删除 PuzzleEdgeWorkerAuthRequest 认证请求类 - 移除 Controller 中的 accessKey 参数验证 - 删除 RenderWorkerEntity 和 RenderWorkerRepository 相关依赖 - 使用默认 workerId 替代动态 worker 验证逻辑 - 将 IP 验证职责移至拦截器层 - 简化客户端状态上报处理逻辑 - 统一任务处理流程中的 workerId 使用方式
This commit is contained in:
@@ -4,7 +4,6 @@ import com.ycwl.basic.annotation.IgnoreToken;
|
|||||||
import com.ycwl.basic.puzzle.edge.dto.PuzzleEdgeTaskFailRequest;
|
import com.ycwl.basic.puzzle.edge.dto.PuzzleEdgeTaskFailRequest;
|
||||||
import com.ycwl.basic.puzzle.edge.dto.PuzzleEdgeTaskSuccessRequest;
|
import com.ycwl.basic.puzzle.edge.dto.PuzzleEdgeTaskSuccessRequest;
|
||||||
import com.ycwl.basic.puzzle.edge.dto.PuzzleEdgeUploadUrlsResponse;
|
import com.ycwl.basic.puzzle.edge.dto.PuzzleEdgeUploadUrlsResponse;
|
||||||
import com.ycwl.basic.puzzle.edge.dto.PuzzleEdgeWorkerAuthRequest;
|
|
||||||
import com.ycwl.basic.puzzle.edge.dto.PuzzleEdgeWorkerSyncRequest;
|
import com.ycwl.basic.puzzle.edge.dto.PuzzleEdgeWorkerSyncRequest;
|
||||||
import com.ycwl.basic.puzzle.edge.dto.PuzzleEdgeWorkerSyncResponse;
|
import com.ycwl.basic.puzzle.edge.dto.PuzzleEdgeWorkerSyncResponse;
|
||||||
import com.ycwl.basic.puzzle.edge.task.PuzzleEdgeRenderTaskService;
|
import com.ycwl.basic.puzzle.edge.task.PuzzleEdgeRenderTaskService;
|
||||||
@@ -36,9 +35,8 @@ public class PuzzleEdgeRenderTaskController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/task/{taskId}/uploadUrls")
|
@PostMapping("/task/{taskId}/uploadUrls")
|
||||||
public ApiResponse<PuzzleEdgeUploadUrlsResponse> uploadUrls(@PathVariable Long taskId,
|
public ApiResponse<PuzzleEdgeUploadUrlsResponse> uploadUrls(@PathVariable Long taskId) {
|
||||||
@RequestBody PuzzleEdgeWorkerAuthRequest req) {
|
return ApiResponse.success(puzzleEdgeRenderTaskService.getUploadUrls(taskId));
|
||||||
return ApiResponse.success(puzzleEdgeRenderTaskService.getUploadUrls(taskId, req != null ? req.getAccessKey() : null));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/task/{taskId}/success")
|
@PostMapping("/task/{taskId}/success")
|
||||||
|
|||||||
@@ -1,9 +0,0 @@
|
|||||||
package com.ycwl.basic.puzzle.edge.dto;
|
|
||||||
|
|
||||||
import lombok.Data;
|
|
||||||
|
|
||||||
@Data
|
|
||||||
public class PuzzleEdgeWorkerAuthRequest {
|
|
||||||
private String accessKey;
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -4,7 +4,6 @@ import cn.hutool.core.util.StrUtil;
|
|||||||
import com.github.benmanes.caffeine.cache.Cache;
|
import com.github.benmanes.caffeine.cache.Cache;
|
||||||
import com.github.benmanes.caffeine.cache.Caffeine;
|
import com.github.benmanes.caffeine.cache.Caffeine;
|
||||||
import com.fasterxml.jackson.core.type.TypeReference;
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
import com.ycwl.basic.model.pc.renderWorker.entity.RenderWorkerEntity;
|
|
||||||
import com.ycwl.basic.model.task.req.ClientStatusReqVo;
|
import com.ycwl.basic.model.task.req.ClientStatusReqVo;
|
||||||
import com.ycwl.basic.puzzle.edge.dto.PuzzleEdgeRenderTaskDTO;
|
import com.ycwl.basic.puzzle.edge.dto.PuzzleEdgeRenderTaskDTO;
|
||||||
import com.ycwl.basic.puzzle.edge.dto.PuzzleEdgeTaskFailRequest;
|
import com.ycwl.basic.puzzle.edge.dto.PuzzleEdgeTaskFailRequest;
|
||||||
@@ -18,7 +17,6 @@ import com.ycwl.basic.puzzle.entity.PuzzleGenerationRecordEntity;
|
|||||||
import com.ycwl.basic.puzzle.entity.PuzzleTemplateEntity;
|
import com.ycwl.basic.puzzle.entity.PuzzleTemplateEntity;
|
||||||
import com.ycwl.basic.puzzle.mapper.PuzzleGenerationRecordMapper;
|
import com.ycwl.basic.puzzle.mapper.PuzzleGenerationRecordMapper;
|
||||||
import com.ycwl.basic.puzzle.repository.PuzzleRepository;
|
import com.ycwl.basic.puzzle.repository.PuzzleRepository;
|
||||||
import com.ycwl.basic.repository.RenderWorkerRepository;
|
|
||||||
import com.ycwl.basic.service.printer.PrinterService;
|
import com.ycwl.basic.service.printer.PrinterService;
|
||||||
import com.ycwl.basic.storage.StorageFactory;
|
import com.ycwl.basic.storage.StorageFactory;
|
||||||
import com.ycwl.basic.storage.adapters.IStorageAdapter;
|
import com.ycwl.basic.storage.adapters.IStorageAdapter;
|
||||||
@@ -136,14 +134,21 @@ public class PuzzleEdgeRenderTaskService {
|
|||||||
private final PuzzleGenerationRecordMapper recordMapper;
|
private final PuzzleGenerationRecordMapper recordMapper;
|
||||||
private final PuzzleRepository puzzleRepository;
|
private final PuzzleRepository puzzleRepository;
|
||||||
private final PrinterService printerService;
|
private final PrinterService printerService;
|
||||||
private final RenderWorkerRepository renderWorkerRepository;
|
|
||||||
|
/**
|
||||||
|
* 固定的 workerId,用于标识通过 IP CIDR 验证的 worker
|
||||||
|
* 由于 IP 验证已在拦截器层完成,此处不再区分具体 worker
|
||||||
|
*/
|
||||||
|
private static final Long DEFAULT_WORKER_ID = 1L;
|
||||||
|
|
||||||
public PuzzleEdgeWorkerSyncResponse sync(PuzzleEdgeWorkerSyncRequest req) {
|
public PuzzleEdgeWorkerSyncResponse sync(PuzzleEdgeWorkerSyncRequest req) {
|
||||||
RenderWorkerEntity worker = requireWorker(req != null ? req.getAccessKey() : null);
|
// IP 验证已在拦截器层完成,此处无需验证 accessKey
|
||||||
|
Long workerId = DEFAULT_WORKER_ID;
|
||||||
|
|
||||||
|
// 客户端状态上报(可选,不再关联具体 worker)
|
||||||
ClientStatusReqVo clientStatus = req != null ? req.getClientStatus() : null;
|
ClientStatusReqVo clientStatus = req != null ? req.getClientStatus() : null;
|
||||||
if (clientStatus != null) {
|
if (clientStatus != null) {
|
||||||
renderWorkerRepository.setWorkerHostStatus(worker.getId(), clientStatus);
|
log.debug("收到客户端状态上报: {}", clientStatus);
|
||||||
}
|
}
|
||||||
|
|
||||||
int maxTasks = req != null && req.getMaxTasks() != null ? req.getMaxTasks() : 1;
|
int maxTasks = req != null && req.getMaxTasks() != null ? req.getMaxTasks() : 1;
|
||||||
@@ -156,12 +161,12 @@ public class PuzzleEdgeRenderTaskService {
|
|||||||
|
|
||||||
PuzzleEdgeWorkerSyncResponse resp = new PuzzleEdgeWorkerSyncResponse();
|
PuzzleEdgeWorkerSyncResponse resp = new PuzzleEdgeWorkerSyncResponse();
|
||||||
for (int i = 0; i < maxTasks; i++) {
|
for (int i = 0; i < maxTasks; i++) {
|
||||||
PuzzleEdgeRenderTaskEntity task = claimOne(worker.getId());
|
PuzzleEdgeRenderTaskEntity task = claimOne(workerId);
|
||||||
if (task == null) {
|
if (task == null) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
PuzzleEdgeRenderTaskDTO dto = toTaskDTOOrFail(task, worker.getId());
|
PuzzleEdgeRenderTaskDTO dto = toTaskDTOOrFail(task, workerId);
|
||||||
if (dto != null) {
|
if (dto != null) {
|
||||||
resp.getTasks().add(dto);
|
resp.getTasks().add(dto);
|
||||||
}
|
}
|
||||||
@@ -170,15 +175,17 @@ public class PuzzleEdgeRenderTaskService {
|
|||||||
return resp;
|
return resp;
|
||||||
}
|
}
|
||||||
|
|
||||||
public PuzzleEdgeUploadUrlsResponse getUploadUrls(Long taskId, String accessKey) {
|
public PuzzleEdgeUploadUrlsResponse getUploadUrls(Long taskId) {
|
||||||
RenderWorkerEntity worker = requireWorker(accessKey);
|
// IP 验证已在拦截器层完成,此处无需验证 accessKey
|
||||||
PuzzleEdgeRenderTaskEntity task = getAndCheckRunningTask(taskId, worker.getId());
|
Long workerId = DEFAULT_WORKER_ID;
|
||||||
|
PuzzleEdgeRenderTaskEntity task = getAndCheckRunningTask(taskId, workerId);
|
||||||
return buildUploadUrls(task);
|
return buildUploadUrls(task);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void taskSuccess(Long taskId, PuzzleEdgeTaskSuccessRequest req) {
|
public void taskSuccess(Long taskId, PuzzleEdgeTaskSuccessRequest req) {
|
||||||
RenderWorkerEntity worker = requireWorker(req != null ? req.getAccessKey() : null);
|
// IP 验证已在拦截器层完成,此处无需验证 accessKey
|
||||||
PuzzleEdgeRenderTaskEntity task = getAndCheckTaskOwned(taskId, worker.getId());
|
Long workerId = DEFAULT_WORKER_ID;
|
||||||
|
PuzzleEdgeRenderTaskEntity task = getAndCheckTaskOwned(taskId, workerId);
|
||||||
if (task.getStatus() != null && task.getStatus() == STATUS_SUCCESS) {
|
if (task.getStatus() != null && task.getStatus() == STATUS_SUCCESS) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -186,7 +193,7 @@ public class PuzzleEdgeRenderTaskService {
|
|||||||
throw new IllegalArgumentException("任务状态非法");
|
throw new IllegalArgumentException("任务状态非法");
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean updated = tryMarkSuccess(task, worker.getId());
|
boolean updated = tryMarkSuccess(task, workerId);
|
||||||
if (!updated) {
|
if (!updated) {
|
||||||
throw new IllegalStateException("任务状态更新失败");
|
throw new IllegalStateException("任务状态更新失败");
|
||||||
}
|
}
|
||||||
@@ -242,8 +249,9 @@ public class PuzzleEdgeRenderTaskService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void taskFail(Long taskId, PuzzleEdgeTaskFailRequest req) {
|
public void taskFail(Long taskId, PuzzleEdgeTaskFailRequest req) {
|
||||||
RenderWorkerEntity worker = requireWorker(req != null ? req.getAccessKey() : null);
|
// IP 验证已在拦截器层完成,此处无需验证 accessKey
|
||||||
PuzzleEdgeRenderTaskEntity task = getAndCheckTaskOwned(taskId, worker.getId());
|
Long workerId = DEFAULT_WORKER_ID;
|
||||||
|
PuzzleEdgeRenderTaskEntity task = getAndCheckTaskOwned(taskId, workerId);
|
||||||
if (task.getStatus() != null && task.getStatus() == STATUS_FAIL) {
|
if (task.getStatus() != null && task.getStatus() == STATUS_FAIL) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -255,7 +263,7 @@ public class PuzzleEdgeRenderTaskService {
|
|||||||
? req.getErrorMessage()
|
? req.getErrorMessage()
|
||||||
: "边缘渲染失败";
|
: "边缘渲染失败";
|
||||||
|
|
||||||
boolean updated = tryMarkFail(task, worker.getId(), errorMessage);
|
boolean updated = tryMarkFail(task, workerId, errorMessage);
|
||||||
if (!updated) {
|
if (!updated) {
|
||||||
throw new IllegalStateException("任务状态更新失败");
|
throw new IllegalStateException("任务状态更新失败");
|
||||||
}
|
}
|
||||||
@@ -760,20 +768,6 @@ public class PuzzleEdgeRenderTaskService {
|
|||||||
return attemptCount < MAX_RETRY_ATTEMPTS;
|
return attemptCount < MAX_RETRY_ATTEMPTS;
|
||||||
}
|
}
|
||||||
|
|
||||||
private RenderWorkerEntity requireWorker(String accessKey) {
|
|
||||||
if (StrUtil.isBlank(accessKey)) {
|
|
||||||
throw new IllegalArgumentException("accessKey不能为空");
|
|
||||||
}
|
|
||||||
RenderWorkerEntity worker = renderWorkerRepository.getWorkerByAccessKey(accessKey);
|
|
||||||
if (worker == null) {
|
|
||||||
throw new IllegalArgumentException("worker不存在");
|
|
||||||
}
|
|
||||||
if (worker.getStatus() == null || worker.getStatus() != 1) {
|
|
||||||
throw new IllegalArgumentException("worker未启用");
|
|
||||||
}
|
|
||||||
return worker;
|
|
||||||
}
|
|
||||||
|
|
||||||
private String normalizeOutputFormat(String format) {
|
private String normalizeOutputFormat(String format) {
|
||||||
String outputFormat = StrUtil.isNotBlank(format) ? format.toUpperCase() : "PNG";
|
String outputFormat = StrUtil.isNotBlank(format) ? format.toUpperCase() : "PNG";
|
||||||
if ("JPG".equals(outputFormat)) {
|
if ("JPG".equals(outputFormat)) {
|
||||||
|
|||||||
Reference in New Issue
Block a user