service/library/contentaudit/video_moderation/taskscheduler.go

60 lines
1.6 KiB
Go

package videomoderation
import (
"fmt"
"service/library/configcenter"
"service/library/logger"
"time"
)
var defaultVideoModerationTaskScheduler *VideoModerationTaskScheduler
// 图像审核任务调度器
type VideoModerationTaskScheduler struct {
// 状态记录
batchId string // 当前批次号
// 批次审核任务控制块
btcbMp map[string]*VideoModerationTaskBatchControlBlock
}
func initScheduler(cfg *configcenter.VideoModerationConfig) {
defaultVideoModerationTaskScheduler = &VideoModerationTaskScheduler{
batchId: genereteBatchId(),
btcbMp: make(map[string]*VideoModerationTaskBatchControlBlock),
}
}
// 生成批次号
func genereteBatchId() string {
now := time.Now()
y, m, d := now.Date()
h, mi, s := now.Clock()
return fmt.Sprintf("%d%02d%02d%02d%02d%02d", y, m, d, h, mi, s)
}
// 获取批次审核任务控制块
func getBtcb(batchId string) (*VideoModerationTaskBatchControlBlock, bool) {
for k, v := range defaultVideoModerationTaskScheduler.btcbMp {
logger.Info("batch_id: %v, btcb: %v", k, v)
}
logger.Info("now batch_id: %v", batchId)
btcb, ok := defaultVideoModerationTaskScheduler.btcbMp[batchId]
return btcb, ok
}
// 移除批次审核任务控制块
func removeBtcb(batchId string) {
delete(defaultVideoModerationTaskScheduler.btcbMp, batchId)
}
// 清理过期的批次审核任务控制块
func ClearExpiredBtcb() {
nowTime := time.Now().Unix()
for _, v := range defaultVideoModerationTaskScheduler.btcbMp {
if nowTime-v.Ct > SecElapsedBeforeTaskExpires {
delete(defaultVideoModerationTaskScheduler.btcbMp, v.BatchId)
}
}
}