feat: 添加异步数据入库任务系统,支持全量/增量更新与实时进度展示
新增完整的异步任务管理体系: 1. 新增Redis缓存层实现任务状态实时存储与降级机制 2. 实现并发拉取+批量写入的高性能入库流程 3. 添加任务管理HTTP接口与路由 4. 重构行情控制器使用共享SQLite存储 5. 配套添加Docker Compose Redis启动配置 6. 前端新增任务管理面板支持操作与进度查看
This commit is contained in:
74
internal/cache/cache.go
vendored
Normal file
74
internal/cache/cache.go
vendored
Normal file
@@ -0,0 +1,74 @@
|
||||
// Package cache 定义任务状态缓存接口,支持 Redis 实时状态 + 内存降级。
|
||||
// Redis 不可用时降级到 NoopCache,任务系统仍可运行(仅失去实时状态能力,回退到 SQLite)。
|
||||
package cache
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
)
|
||||
|
||||
// TaskType 任务类型。
|
||||
type TaskType string
|
||||
|
||||
const (
|
||||
TaskTypeFull TaskType = "full" // 全量入库
|
||||
TaskTypeIncremental TaskType = "incremental" // 增量同步
|
||||
)
|
||||
|
||||
// TaskStatus 任务状态。
|
||||
type TaskStatus string
|
||||
|
||||
const (
|
||||
TaskStatusPending TaskStatus = "pending" // 已创建未启动
|
||||
TaskStatusRunning TaskStatus = "running" // 执行中
|
||||
TaskStatusSuccess TaskStatus = "success" // 成功
|
||||
TaskStatusFailed TaskStatus = "failed" // 失败
|
||||
TaskStatusCancelled TaskStatus = "cancelled" // 已取消
|
||||
)
|
||||
|
||||
// Task 任务运行态快照,Redis 实时状态与 SQLite 持久化共用同一结构。
|
||||
type Task struct {
|
||||
ID string `json:"id"`
|
||||
Type TaskType `json:"type"`
|
||||
Status TaskStatus `json:"status"`
|
||||
Total int `json:"total"`
|
||||
Done int `json:"done"`
|
||||
Success int `json:"success"`
|
||||
Failed int `json:"failed"`
|
||||
FailedCodes []string `json:"failedCodes,omitempty"`
|
||||
Params string `json:"params"`
|
||||
Error string `json:"error,omitempty"`
|
||||
StartedAt string `json:"startedAt"`
|
||||
FinishedAt string `json:"finishedAt,omitempty"`
|
||||
}
|
||||
|
||||
// Cache 任务状态缓存接口。
|
||||
// Redis 实现用于实时状态查询;NoopCache 用于 Redis 不可用时降级。
|
||||
type Cache interface {
|
||||
// GetTask 读取任务状态,不存在返回 nil,nil。
|
||||
GetTask(ctx context.Context, id string) (*Task, error)
|
||||
// SetTask 写入任务状态,ttl 为过期时间。
|
||||
SetTask(ctx context.Context, t *Task, ttl time.Duration) error
|
||||
// DeleteTask 删除任务状态。
|
||||
DeleteTask(ctx context.Context, id string) error
|
||||
// ListActiveTaskIDs 返回运行中任务的 ID 列表。
|
||||
ListActiveTaskIDs(ctx context.Context) ([]string, error)
|
||||
// AddActiveTask 将任务 ID 加入活跃集合。
|
||||
AddActiveTask(ctx context.Context, id string) error
|
||||
// RemoveActiveTask 从活跃集合移除任务 ID。
|
||||
RemoveActiveTask(ctx context.Context, id string) error
|
||||
// Ping 探测缓存可用性。
|
||||
Ping(ctx context.Context) error
|
||||
}
|
||||
|
||||
// NoopCache Cache 的空实现,Redis 不可用时降级使用。
|
||||
// 所有操作均为空操作,任务状态仅依赖 SQLite 持久化。
|
||||
type NoopCache struct{}
|
||||
|
||||
func (NoopCache) GetTask(ctx context.Context, id string) (*Task, error) { return nil, nil }
|
||||
func (NoopCache) SetTask(ctx context.Context, t *Task, ttl time.Duration) error { return nil }
|
||||
func (NoopCache) DeleteTask(ctx context.Context, id string) error { return nil }
|
||||
func (NoopCache) ListActiveTaskIDs(ctx context.Context) ([]string, error) { return nil, nil }
|
||||
func (NoopCache) AddActiveTask(ctx context.Context, id string) error { return nil }
|
||||
func (NoopCache) RemoveActiveTask(ctx context.Context, id string) error { return nil }
|
||||
func (NoopCache) Ping(ctx context.Context) error { return nil }
|
||||
Reference in New Issue
Block a user