perf(storage): 拦截任务二进制并本地暂存结果
原因:任务标准结果中的 Base64、Data URI 和 Buffer 会进入 PostgreSQL JSON,导致 TOAST 与备份体积快速增长。 影响:新增统一 JSON 持久化关口;upload_none 将二进制原子写入本地结果目录,数据库仅保存带 SHA-256 的有界占位符;任务详情、同步响应、异步查询和兼容协议按需校验恢复。补充 24 小时清理、容量上限、历史小批量治理命令及管理端说明。 风险:本地结果超过 TTL、丢失或损坏时分别返回明确的 410/500;空间不足时返回 503 且不重试上游。未自动执行历史治理。 验证:三种真实图片模型同步/异步与幂等重放通过;Go vet/全量测试、前端 111 测试、lint/typecheck/build、OpenAPI、迁移安全、govulncheck、依赖审计、手工发布测试及 Linux amd64 构建通过。
This commit is contained in:
@@ -45,6 +45,58 @@ func (s *Server) cleanupExpiredLocalTempAssets(ctx context.Context, now time.Tim
|
||||
for _, target := range targets {
|
||||
deleted += s.cleanupExpiredLocalTempAssetsInDir(ctx, now, target)
|
||||
}
|
||||
deleted += s.cleanupExpiredLocalBinaryResults(now)
|
||||
return deleted
|
||||
}
|
||||
|
||||
func (s *Server) cleanupExpiredLocalBinaryResults(now time.Time) int {
|
||||
storageDir := strings.TrimSpace(s.cfg.LocalGeneratedStorageDir)
|
||||
if storageDir == "" {
|
||||
storageDir = config.DefaultLocalGeneratedStorageDir
|
||||
}
|
||||
root := filepath.Join(storageDir, "results")
|
||||
ttlHours := s.cfg.LocalResultTTLHours
|
||||
if ttlHours <= 0 {
|
||||
ttlHours = 24
|
||||
}
|
||||
expiredBefore := now.Add(-time.Duration(ttlHours) * time.Hour)
|
||||
deleted := 0
|
||||
var directories []string
|
||||
err := filepath.WalkDir(root, func(path string, entry os.DirEntry, walkErr error) error {
|
||||
if walkErr != nil {
|
||||
if !errors.Is(walkErr, os.ErrNotExist) && s.logger != nil {
|
||||
s.logger.Warn("walk local binary result failed", "path", path, "error", walkErr)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if entry.IsDir() {
|
||||
if path != root {
|
||||
directories = append(directories, path)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if entry.Type()&os.ModeSymlink != 0 {
|
||||
return nil
|
||||
}
|
||||
info, infoErr := entry.Info()
|
||||
if infoErr != nil || info.ModTime().After(expiredBefore) {
|
||||
return nil
|
||||
}
|
||||
if removeErr := os.Remove(path); removeErr != nil && !errors.Is(removeErr, os.ErrNotExist) {
|
||||
if s.logger != nil {
|
||||
s.logger.Warn("remove local binary result failed", "path", path, "error", removeErr)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
deleted++
|
||||
return nil
|
||||
})
|
||||
if err != nil && !errors.Is(err, os.ErrNotExist) && s.logger != nil {
|
||||
s.logger.Warn("scan local binary result root failed", "dir", root, "error", err)
|
||||
}
|
||||
for index := len(directories) - 1; index >= 0; index-- {
|
||||
_ = os.Remove(directories[index])
|
||||
}
|
||||
return deleted
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user