feat: update task routing and coze handling

This commit is contained in:
super
2026-06-03 17:14:50 +08:00
parent 7310abd769
commit e540d8e765
37 changed files with 1883 additions and 254 deletions
@@ -1,8 +1,13 @@
package com.nanri.aiimage.common.exception;
import com.nanri.aiimage.common.api.ApiResponse;
import com.nanri.aiimage.common.service.TaskOwnerForwardService;
import com.nanri.aiimage.config.TaskOperationLockConfig;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.ConstraintViolationException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@@ -11,9 +16,32 @@ import org.springframework.web.context.request.async.AsyncRequestNotUsableExcept
import java.io.IOException;
@Slf4j
@RequiredArgsConstructor
@RestControllerAdvice
public class GlobalExceptionHandler {
private final TaskOwnerForwardService taskOwnerForwardService;
@ExceptionHandler(TaskOwnerMismatchException.class)
public Object handleTaskOwnerMismatchException(TaskOwnerMismatchException ex, HttpServletRequest request) {
TaskOperationLockConfig.releaseRequestLock(request);
try {
ResponseEntity<byte[]> response = taskOwnerForwardService.forwardCurrentRequest(ex, request);
return ResponseEntity.status(response.getStatusCode())
.headers(response.getHeaders())
.body(response.getBody());
} catch (BusinessException forwardEx) {
return forwardEx.getCode() == null
? ApiResponse.fail(forwardEx.getMessage())
: ApiResponse.fail(forwardEx.getCode(), forwardEx.getMessage());
} catch (Exception forwardEx) {
log.warn("[instance-routing] forward failed taskId={} operation={} owner={} current={} msg={}",
ex.getTaskId(), ex.getOperation(), ex.getOwnerInstanceId(), ex.getCurrentInstanceId(),
forwardEx.getMessage(), forwardEx);
return ApiResponse.fail(40903, "任务归属实例转发失败: " + forwardEx.getMessage());
}
}
@ExceptionHandler(BusinessException.class)
public ApiResponse<Void> handleBusinessException(BusinessException ex) {
if (Integer.valueOf(40901).equals(ex.getCode())) {
@@ -0,0 +1,36 @@
package com.nanri.aiimage.common.exception;
public class TaskOwnerMismatchException extends BusinessException {
private final Long taskId;
private final String operation;
private final String ownerInstanceId;
private final String currentInstanceId;
public TaskOwnerMismatchException(Long taskId,
String operation,
String ownerInstanceId,
String currentInstanceId) {
super(40903, "该任务已绑定到另一台服务实例处理,请通过原实例继续处理");
this.taskId = taskId;
this.operation = operation;
this.ownerInstanceId = ownerInstanceId;
this.currentInstanceId = currentInstanceId;
}
public Long getTaskId() {
return taskId;
}
public String getOperation() {
return operation;
}
public String getOwnerInstanceId() {
return ownerInstanceId;
}
public String getCurrentInstanceId() {
return currentInstanceId;
}
}