feat: 添加异步数据入库任务系统,支持全量/增量更新与实时进度展示

新增完整的异步任务管理体系:
1. 新增Redis缓存层实现任务状态实时存储与降级机制
2. 实现并发拉取+批量写入的高性能入库流程
3. 添加任务管理HTTP接口与路由
4. 重构行情控制器使用共享SQLite存储
5. 配套添加Docker Compose Redis启动配置
6. 前端新增任务管理面板支持操作与进度查看
This commit is contained in:
2026-08-18 10:09:52 +08:00
parent e28bfec957
commit 273fc90a95
18 changed files with 1682 additions and 80 deletions

37
internal/job/store.go Normal file
View File

@@ -0,0 +1,37 @@
// Package job 实现异步任务执行系统:并发拉取 + 批量写入 + 进度上报。
//
// 核心设计:
// - Manager 管理任务注册表和全局限速器,允许多任务并发
// - Runner 是单任务执行器:N worker 并发拉取,单 collector 串行写 SQLite
// - Redis 存实时进度,SQLite 存持久化任务记录
package job
import (
"context"
"stock-analysis/internal/cache"
"stock-analysis/internal/model"
)
// Store 定义 job 运行期需要的存储能力(K线 + 任务 CRUD)。
// *sqlite.Store 实现此接口(编译期断言)。
type Store interface {
// K线批量写入(单事务,collector 调用)
SaveKlinesBatch(ctx context.Context, batch map[string][]model.Kline) error
// 返回所有已入库 code(全量断点续跑跳过判断)
ListCodes(ctx context.Context) ([]string, error)
// 一次性返回 code -> 最后K线日期 映射(增量起点预加载)
LastKlineDates(ctx context.Context) (map[string]string, error)
// 任务持久化
SaveTask(ctx context.Context, t *cache.Task) error
UpdateTaskProgress(ctx context.Context, id string, total, done, success, failed int, failedCodes []string) error
FinishTask(ctx context.Context, id string, status cache.TaskStatus, errMsg, finishedAt string) error
GetTask(ctx context.Context, id string) (*cache.Task, error)
ListTasks(ctx context.Context, limit int) ([]*cache.Task, error)
}
// BondLister 全量转债列表获取接口(eastmoney.Client 实现)。
type BondLister interface {
FetchAllBondCodes(ctx context.Context) ([]model.ConvertibleBond, error)
}