feat: add redis gateway protection and task recovery

This commit is contained in:
wlx committed 2026-09-08 18:02:45 +08:00
1 parent cbc7317aa8
commit 473479fc4c
17 files changed
+272 -10

No files matched your search

@@ -5,6 +5,7 @@ import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.moyi.controlplane.catalog.CatalogService;
import com.moyi.controlplane.common.ApiException;
import com.moyi.controlplane.gateway.GatewayGuard;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.OffsetDateTime;
@@ -15,6 +16,7 @@ import java.util.Map;
import java.util.UUID;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -23,21 +25,35 @@ public class AsyncTaskService {
private final JdbcTemplate jdbc;
private final ObjectMapper objectMapper;
private final CatalogService catalogService;
private final int maxQueuedPerModel;
private final GatewayGuard gatewayGuard;
public AsyncTaskService(JdbcTemplate jdbc, ObjectMapper objectMapper, CatalogService catalogService) {
public AsyncTaskService(
JdbcTemplate jdbc,
ObjectMapper objectMapper,
CatalogService catalogService,
@Value("${moyi.tasks.max-queued-per-model:100}") int maxQueuedPerModel,
GatewayGuard gatewayGuard
) {
if (maxQueuedPerModel < 1) throw new IllegalArgumentException("Task queue limit must be positive");
this.jdbc = jdbc;
this.objectMapper = objectMapper;
this.catalogService = catalogService;
this.maxQueuedPerModel = maxQueuedPerModel;
this.gatewayGuard = gatewayGuard;
}
@Transactional
public Map<String, Object> create(String authorization, String taskType, Map<String, Object> input) {
Map<String, Object> key = requireKey(authorization);
gatewayGuard.checkRate(UUID.fromString(String.valueOf(key.get("id"))), false);
String modelId = String.valueOf(input.getOrDefault("model", ""));
Map<String, Object> model = catalogService.activeModel(modelId);
if (model == null) throw new ApiException("MODEL_NOT_FOUND", "模型不存在或已下线", 404);
if (!catalogService.keyCanInvoke(key, modelId)) throw new ApiException("MODEL_FORBIDDEN", "当前 Key 无权调用该模型", 403);
if (!taskType.equals(model.get("type")) || !"async".equals(model.get("mode"))) throw new ApiException("NOT_ASYNC_MEDIA_MODEL", "该模型不是对应的异步媒体模型", 400);
Integer queued = jdbc.queryForObject("SELECT COUNT(*) FROM model_tasks WHERE model_id = ? AND status IN ('queued', 'running')", Integer.class, modelId);
if (queued != null && queued >= maxQueuedPerModel) throw new ApiException("TASK_QUEUE_FULL", "该模型当前排队已满,请稍后重试", 429);
String prompt = String.valueOf(input.getOrDefault("prompt", "")).trim();
if (prompt.isBlank()) throw new ApiException("PROMPT_REQUIRED", "生成提示词不能为空");
String id = "task_" + UUID.randomUUID().toString().replace("-", "").substring(0, 24);
@@ -62,7 +78,20 @@ public class AsyncTaskService {
}
}
@Scheduled(fixedDelayString = "${MOYI_TASK_WORKER_INTERVAL_MS:1000}")
public List<Map<String, Object>> list(String authorization, int limit) {
Map<String, Object> key = requireKey(authorization);
UUID userId = catalogService.keyOwnerId(UUID.fromString(String.valueOf(key.get("id"))));
int boundedLimit = Math.max(1, Math.min(limit, 100));
return jdbc.query("SELECT * FROM model_tasks WHERE user_id = ? ORDER BY created_at DESC LIMIT ?", this::mapTask, userId, boundedLimit);
}
public List<Map<String, Object>> listForUser(String sessionToken, com.moyi.controlplane.auth.AuthService auth, int limit) {
UUID userId = auth.requireUser(sessionToken).id();
int boundedLimit = Math.max(1, Math.min(limit, 100));
return jdbc.query("SELECT * FROM model_tasks WHERE user_id = ? ORDER BY created_at DESC LIMIT ?", this::mapTask, userId, boundedLimit);
}
@Scheduled(fixedDelayString = "${moyi.tasks.worker-interval-ms:1000}")
public void completeMockTasks() {
jdbc.update(
"UPDATE model_tasks SET status = 'succeeded', progress = 100, result = CAST(CASE WHEN task_type = '3d' THEN ? ELSE ? END AS jsonb), updated_at = ?, completed_at = ? WHERE status IN ('queued', 'running') AND provider_task_id = 'mock' AND created_at < now() - INTERVAL '500 milliseconds'",