mirror of
https://git.fightbot.fun/hxuanyu/BingPaper.git
synced 2026-02-15 18:49:31 +08:00
优化按需抓取逻辑:改为异步处理以提升性能,并为相关接口新增 202 状态支持
This commit is contained in:
@@ -2,6 +2,7 @@ package image
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
@@ -15,6 +16,8 @@ import (
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
var ErrFetchStarted = errors.New("on-demand fetch started")
|
||||
|
||||
func CleanupOldImages(ctx context.Context) error {
|
||||
days := config.GetConfig().Retention.Days
|
||||
if days <= 0 {
|
||||
@@ -60,14 +63,13 @@ func GetTodayImage(mkt string) (*model.Image, error) {
|
||||
}
|
||||
err := tx.Preload("Variants").First(&img).Error
|
||||
if err != nil && mkt != "" && config.GetConfig().API.EnableOnDemandFetch && util.IsValidRegion(mkt) {
|
||||
// 如果没找到,尝试按需抓取该地区
|
||||
util.Logger.Info("Image not found in DB, attempting on-demand fetch", zap.String("mkt", mkt))
|
||||
// 如果没找到,尝试异步按需抓取该地区
|
||||
util.Logger.Info("Image not found in DB, starting asynchronous on-demand fetch", zap.String("mkt", mkt))
|
||||
f := fetcher.NewFetcher()
|
||||
_ = f.FetchRegion(context.Background(), mkt)
|
||||
|
||||
// 抓取后重新查询
|
||||
tx = repo.DB.Where("date = ?", today).Where("mkt = ?", mkt)
|
||||
err = tx.Preload("Variants").First(&img).Error
|
||||
go func() {
|
||||
_ = f.FetchRegion(context.Background(), mkt)
|
||||
}()
|
||||
return nil, ErrFetchStarted
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
@@ -118,14 +120,13 @@ func GetRandomImage(mkt string) (*model.Image, error) {
|
||||
}
|
||||
tx.Count(&count)
|
||||
if count == 0 && mkt != "" && config.GetConfig().API.EnableOnDemandFetch && util.IsValidRegion(mkt) {
|
||||
// 如果没找到,尝试按需抓取该地区
|
||||
util.Logger.Info("No images found in DB for region, attempting on-demand fetch", zap.String("mkt", mkt))
|
||||
// 如果没找到,尝试异步按需抓取该地区
|
||||
util.Logger.Info("No images found in DB for region, starting asynchronous on-demand fetch", zap.String("mkt", mkt))
|
||||
f := fetcher.NewFetcher()
|
||||
_ = f.FetchRegion(context.Background(), mkt)
|
||||
|
||||
// 抓取后重新计数
|
||||
tx = repo.DB.Model(&model.Image{}).Where("mkt = ?", mkt)
|
||||
tx.Count(&count)
|
||||
go func() {
|
||||
_ = f.FetchRegion(context.Background(), mkt)
|
||||
}()
|
||||
return nil, ErrFetchStarted
|
||||
}
|
||||
|
||||
if count == 0 {
|
||||
@@ -167,14 +168,13 @@ func GetImageByDate(date string, mkt string) (*model.Image, error) {
|
||||
}
|
||||
err := tx.Preload("Variants").First(&img).Error
|
||||
if err != nil && mkt != "" && config.GetConfig().API.EnableOnDemandFetch && util.IsValidRegion(mkt) {
|
||||
// 如果没找到,尝试按需抓取该地区
|
||||
util.Logger.Info("Image not found in DB for date, attempting on-demand fetch", zap.String("mkt", mkt), zap.String("date", date))
|
||||
// 如果没找到,尝试异步按需抓取该地区
|
||||
util.Logger.Info("Image not found in DB for date, starting asynchronous on-demand fetch", zap.String("mkt", mkt), zap.String("date", date))
|
||||
f := fetcher.NewFetcher()
|
||||
_ = f.FetchRegion(context.Background(), mkt)
|
||||
|
||||
// 抓取后重新查询
|
||||
tx = repo.DB.Where("date = ?", date).Where("mkt = ?", mkt)
|
||||
err = tx.Preload("Variants").First(&img).Error
|
||||
go func() {
|
||||
_ = f.FetchRegion(context.Background(), mkt)
|
||||
}()
|
||||
return nil, ErrFetchStarted
|
||||
}
|
||||
|
||||
// 兜底逻辑
|
||||
|
||||
Reference in New Issue
Block a user