feat: add persistent async task mock

This commit is contained in:
wlx committed 2026-09-07 17:42:48 +08:00
1 parent 7949f29f1a
commit cbc7317aa8
6 files changed
+186 -3

No files matched your search

@@ -2,8 +2,10 @@ package com.moyi.controlplane;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication @SpringBootApplication
@EnableScheduling
public class MoyiControlPlaneApplication { public class MoyiControlPlaneApplication {
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(MoyiControlPlaneApplication.class, args); SpringApplication.run(MoyiControlPlaneApplication.class, args);
@@ -213,6 +213,11 @@ public class CatalogService {
if (key != null && key.get("id") != null) jdbc.update("UPDATE platform_api_keys SET last_used_at = ? WHERE id = ?", now(), UUID.fromString(String.valueOf(key.get("id")))); if (key != null && key.get("id") != null) jdbc.update("UPDATE platform_api_keys SET last_used_at = ? WHERE id = ?", now(), UUID.fromString(String.valueOf(key.get("id"))));
} }
public UUID keyOwnerId(UUID apiKeyId) {
try { return jdbc.queryForObject("SELECT user_id FROM platform_api_keys WHERE id = ? AND status = 'active'", UUID.class, apiKeyId); }
catch (EmptyResultDataAccessException exception) { throw new ApiException("INVALID_API_KEY", "API Key 无效或已停用", 401); }
}
public String providerIdForModel(String modelId) { public String providerIdForModel(String modelId) {
try { try {
return jdbc.queryForObject("SELECT provider_id FROM model_catalog WHERE id = ? AND status = 'active'", String.class, modelId); return jdbc.queryForObject("SELECT provider_id FROM model_catalog WHERE id = ? AND status = 'active'", String.class, modelId);
@@ -0,0 +1,105 @@
package com.moyi.controlplane.task;
import com.fasterxml.jackson.core.JsonProcessingException;
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 java.sql.ResultSet;
import java.sql.SQLException;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class AsyncTaskService {
private final JdbcTemplate jdbc;
private final ObjectMapper objectMapper;
private final CatalogService catalogService;
public AsyncTaskService(JdbcTemplate jdbc, ObjectMapper objectMapper, CatalogService catalogService) {
this.jdbc = jdbc;
this.objectMapper = objectMapper;
this.catalogService = catalogService;
}
@Transactional
public Map<String, Object> create(String authorization, String taskType, Map<String, Object> input) {
Map<String, Object> key = requireKey(authorization);
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);
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);
UUID apiKeyId = UUID.fromString(String.valueOf(key.get("id")));
UUID userId = catalogService.keyOwnerId(apiKeyId);
OffsetDateTime now = now();
jdbc.update(
"INSERT INTO model_tasks (id, user_id, api_key_id, model_id, task_type, provider_task_id, status, progress, prompt, created_at, updated_at) VALUES (?, ?, ?, ?, ?, 'mock', 'queued', 0, ?, ?, ?)",
id, userId, apiKeyId, modelId, taskType, prompt, now, now
);
catalogService.touchKey(key);
return taskView(id);
}
public Map<String, Object> get(String authorization, String taskId) {
Map<String, Object> key = requireKey(authorization);
UUID userId = catalogService.keyOwnerId(UUID.fromString(String.valueOf(key.get("id"))));
try {
return jdbc.queryForObject("SELECT * FROM model_tasks WHERE id = ? AND user_id = ?", this::mapTask, taskId, userId);
} catch (org.springframework.dao.EmptyResultDataAccessException exception) {
throw new ApiException("TASK_NOT_FOUND", "任务不存在", 404);
}
}
@Scheduled(fixedDelayString = "${MOYI_TASK_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'",
"{\"format\":\"glb\",\"url\":\"https://mock.moyi.ai/tasks/result.glb\"}",
"{\"format\":\"mp4\",\"url\":\"https://mock.moyi.ai/tasks/result.mp4\"}", now(), now()
);
}
private Map<String, Object> requireKey(String authorization) {
if (authorization == null || !authorization.startsWith("Bearer ")) throw new ApiException("UNAUTHENTICATED", "请提供有效的 API Key", 401);
Map<String, Object> key = catalogService.authenticateKey(authorization.substring(7));
if (key == null) throw new ApiException("INVALID_API_KEY", "API Key 无效或已停用", 401);
return key;
}
private Map<String, Object> taskView(String id) { return jdbc.queryForObject("SELECT * FROM model_tasks WHERE id = ?", this::mapTask, id); }
private Map<String, Object> mapTask(ResultSet resultSet, int rowNumber) throws SQLException {
Map<String, Object> task = new LinkedHashMap<>();
task.put("id", resultSet.getString("id"));
task.put("provider", "mock");
task.put("model", resultSet.getString("model_id"));
task.put("status", resultSet.getString("status"));
task.put("progress", resultSet.getInt("progress"));
task.put("createdAt", resultSet.getObject("created_at", OffsetDateTime.class));
task.put("updatedAt", resultSet.getObject("updated_at", OffsetDateTime.class));
String result = resultSet.getString("result");
if (result != null) task.put("result", readMap(result));
String error = resultSet.getString("error");
if (error != null) task.put("error", readMap(error));
return task;
}
private Map<String, Object> readMap(String value) {
try { return objectMapper.readValue(value, new TypeReference<>() {}); }
catch (JsonProcessingException exception) { return Map.of(); }
}
private OffsetDateTime now() { return OffsetDateTime.now(ZoneOffset.UTC); }
}
@@ -0,0 +1,46 @@
package com.moyi.controlplane.task;
import java.util.Map;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/v1")
public class TaskController {
private final AsyncTaskService tasks;
public TaskController(AsyncTaskService tasks) {
this.tasks = tasks;
}
@PostMapping("/3d/tasks")
public ResponseEntity<Map<String, Object>> create3d(
@RequestHeader(value = "Authorization", required = false) String authorization,
@RequestBody Map<String, Object> input
) {
return ResponseEntity.status(HttpStatus.ACCEPTED).body(Map.of("task", tasks.create(authorization, "3d", input)));
}
@PostMapping("/video/tasks")
public ResponseEntity<Map<String, Object>> createVideo(
@RequestHeader(value = "Authorization", required = false) String authorization,
@RequestBody Map<String, Object> input
) {
return ResponseEntity.status(HttpStatus.ACCEPTED).body(Map.of("task", tasks.create(authorization, "video", input)));
}
@GetMapping("/tasks/{id}")
public Map<String, Object> get(
@RequestHeader(value = "Authorization", required = false) String authorization,
@PathVariable String id
) {
return Map.of("task", tasks.get(authorization, id));
}
}
@@ -0,0 +1,19 @@
CREATE TABLE IF NOT EXISTS model_tasks (
id varchar(160) PRIMARY KEY,
user_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
api_key_id uuid NOT NULL REFERENCES platform_api_keys(id) ON DELETE CASCADE,
model_id varchar(64) NOT NULL REFERENCES model_catalog(id),
task_type varchar(16) NOT NULL CHECK (task_type IN ('3d', 'video')),
provider_task_id varchar(160),
status varchar(16) NOT NULL CHECK (status IN ('queued', 'running', 'succeeded', 'failed', 'cancelled')),
progress integer NOT NULL DEFAULT 0 CHECK (progress >= 0 AND progress <= 100),
prompt text NOT NULL DEFAULT '',
result jsonb,
error jsonb,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now(),
completed_at timestamptz
);
CREATE INDEX IF NOT EXISTS model_tasks_user_created_idx ON model_tasks(user_id, created_at DESC);
CREATE INDEX IF NOT EXISTS model_tasks_status_created_idx ON model_tasks(status, created_at);
+9 -3
View File
@@ -9,7 +9,8 @@
- 供应商和模型目录读取; - 供应商和模型目录读取;
- 项目创建与查询; - 项目创建与查询;
- API Key 创建、明文一次性返回、撤销和模型权限配置; - API Key 创建、明文一次性返回、撤销和模型权限配置;
- 最小 OpenAI 兼容 `GET /v1/models` 与 `POST /v1/chat/completions` 接口;DeepSeek、Kimi、GLM 配置服务端 Key 后走真实上游,未配置时保留 Mock。 - 最小 OpenAI 兼容 `GET /v1/models` 与 `POST /v1/chat/completions` 接口;DeepSeek、Kimi、GLM 配置服务端 Key 后走真实上游,未配置时保留 Mock;
- 持久化的 3D/视频 Mock 异步任务、轮询和结果结构,保证 Java Compose 下任务中心可联调。
## 已落地接口 ## 已落地接口
@@ -31,6 +32,9 @@
| `GET /api/admin/audit` | 管理员 | 审计记录 | | `GET /api/admin/audit` | 管理员 | 审计记录 |
| `GET /v1/models` | 平台 API Key | 返回该 Key 有权限的模型 | | `GET /v1/models` | 平台 API Key | 返回该 Key 有权限的模型 |
| `POST /v1/chat/completions` | 平台 API Key | 同步聊天调用(已配置供应商 Key 时转发真实上游) | | `POST /v1/chat/completions` | 平台 API Key | 同步聊天调用(已配置供应商 Key 时转发真实上游) |
| `POST /v1/3d/tasks` | 平台 API Key | 创建 3D 异步任务(当前 Mock 执行器) |
| `POST /v1/video/tasks` | 平台 API Key | 创建视频异步任务(当前 Mock 执行器) |
| `GET /v1/tasks/{id}` | 平台 API Key | 查询任务状态和结果 |
## 数据库迁移 ## 数据库迁移
@@ -40,6 +44,7 @@
- `V4__catalog_channels.sql`:供应商凭证元数据、模型来源分组、价格和并发字段; - `V4__catalog_channels.sql`:供应商凭证元数据、模型来源分组、价格和并发字段;
- `V5__usage_events.sql`:用量事件基础表; - `V5__usage_events.sql`:用量事件基础表;
- `V6__seed_catalog.sql`:本地首批模型目录初始数据,可由管理员继续增删改造。 - `V6__seed_catalog.sql`:本地首批模型目录初始数据,可由管理员继续增删改造。
- `V7__async_tasks.sql`:3D/视频异步任务、状态和结果元数据。
Java 版本沿用现有 TypeScript 服务的表命名,便于后续迁移 RDS;供应商密钥只返回掩码,数据库保存 AES-GCM 密文,主密钥由 `CREDENTIAL_ENCRYPTION_KEY` 注入。生产环境应进一步使用 KMS/RAM 托管该主密钥。 Java 版本沿用现有 TypeScript 服务的表命名,便于后续迁移 RDS;供应商密钥只返回掩码,数据库保存 AES-GCM 密文,主密钥由 `CREDENTIAL_ENCRYPTION_KEY` 注入。生产环境应进一步使用 KMS/RAM 托管该主密钥。
@@ -61,6 +66,7 @@ Invoke-WebRequest http://localhost:8080/health
- 数据已写入 PostgreSQL,Redis 连接配置已预留,但限流、队列和分布式幂等尚未接入; - 数据已写入 PostgreSQL,Redis 连接配置已预留,但限流、队列和分布式幂等尚未接入;
- `/v1/chat/completions` 已支持 DeepSeek、Kimi、GLM 的 OpenAI 兼容转发;管理员在目录中新增带凭证的兼容供应商后,也可按模型绑定动态渠道;未配置 Key 时返回 Mock,流式响应暂未开放; - `/v1/chat/completions` 已支持 DeepSeek、Kimi、GLM 的 OpenAI 兼容转发;管理员在目录中新增带凭证的兼容供应商后,也可按模型绑定动态渠道;未配置 Key 时返回 Mock,流式响应暂未开放;
- `/v1/3d/tasks` 和 `/v1/video/tasks` 已提供数据库持久化 Mock worker,真实供应商执行器尚未替换;
- Seedance、Tripo/VAST 的异步任务和 OSS 结果存储尚未迁移到 Java; - Seedance、Tripo/VAST 的异步任务和 OSS 结果存储尚未迁移到 Java;
- 价格、额度扣减、计费账单和并发排队仍沿用下一节点计划; - 价格、额度扣减、计费账单和并发排队仍沿用下一节点计划;
- Docker 镜像需要在具备 Maven 和 Docker daemon 的环境中构建验证。 - Docker 镜像需要在具备 Maven 和 Docker daemon 的环境中构建验证。
@@ -68,6 +74,6 @@ Invoke-WebRequest http://localhost:8080/health
## 下一节点 ## 下一节点
1. 为真实同步转发补充超时、重试、熔断、响应脱敏和用量事件记录; 1. 为真实同步转发补充超时、重试、熔断、响应脱敏和用量事件记录;
2. 接入 Redis 限流、并发计数、幂等键和异步任务状态; 2. 接入 Redis 限流、并发计数、幂等键和分布式异步任务状态;
3. 接入 Tripo/Seedance 异步任务与 OSS 私有对象; 3. 用 Tripo/Seedance 替换 Mock 执行器,并接入 OSS 私有对象;
4. 用 RDS、Redis、OSS 的测试资源完成云上验收,再切换前端的 Java API 地址。 4. 用 RDS、Redis、OSS 的测试资源完成云上验收,再切换前端的 Java API 地址。