- 新增 contentstore 统一存放版本内容并支持定期 GC - 新增 jsexec 沙箱执行动态接口代码(带超时保护) - 补丁版本自动合并完整包,筛选仅含完整包的版本 - 创建版本写库失败时记录日志并返回具体错误
55 lines
1.7 KiB
Go
55 lines
1.7 KiB
Go
package scheduler
|
|
|
|
import (
|
|
"log"
|
|
"sync/atomic"
|
|
"time"
|
|
|
|
"verification-platform-backend/internal/database"
|
|
"verification-platform-backend/internal/model"
|
|
"verification-platform-backend/internal/utils/contentstore"
|
|
)
|
|
|
|
// contentStoreGrace 内容文件写入后的保护期,GC 不碰这段时间内的文件。
|
|
// 只需要覆盖一次异步拆包的耗时:内容有没有人引用由版本清单决定,
|
|
// 这段保护期防的是拆包中断留下的半截临时文件被误删。
|
|
const contentStoreGrace = time.Hour
|
|
|
|
// gcRunning 标记回收是否正在执行,避免多个触发点并发扫描同一个目录
|
|
var gcRunning atomic.Bool
|
|
|
|
// TriggerContentStoreGC 异步回收一次无引用内容,已有回收在跑时直接跳过
|
|
//
|
|
// 供删除版本、更换压缩包等会产生孤儿内容的动作调用
|
|
func TriggerContentStoreGC() {
|
|
go RunContentStoreGC()
|
|
}
|
|
|
|
// RunContentStoreGC 回收不再被任何版本清单引用的内容文件
|
|
func RunContentStoreGC() {
|
|
if !gcRunning.CompareAndSwap(false, true) {
|
|
return // 上一轮还在跑,跳过
|
|
}
|
|
defer gcRunning.Store(false)
|
|
|
|
var hashes []string
|
|
if err := database.DB.
|
|
Model(&model.VersionFile{}).
|
|
Where("file_hash <> ''").
|
|
Distinct().
|
|
Pluck("file_hash", &hashes).Error; err != nil {
|
|
log.Printf("[GC] 读取已引用文件哈希失败: %v\n", err)
|
|
return
|
|
}
|
|
|
|
referenced := make(map[string]bool, len(hashes))
|
|
for _, h := range hashes {
|
|
referenced[h] = true
|
|
}
|
|
|
|
removed, freed := contentstore.GC(func(hash string) bool { return referenced[hash] }, contentStoreGrace)
|
|
if removed > 0 {
|
|
log.Printf("[GC] 清理无引用内容文件 %d 个, 释放 %.2f MB\n", removed, float64(freed)/(1<<20))
|
|
}
|
|
}
|