feat(cron): 添加角色配装信息刷新任务并优化神器数据同步功能

- 新增每5天执行一次的角色配装信息刷新任务
- 重构神器数据同步功能,优化数据处理和保存逻辑- 添加神器图片URL获取和上传逻辑
- 更新相关测试用例
This commit is contained in:
hxt
2025-07-26 16:22:12 +08:00
parent fc41c5ca73
commit 707b3cb347
6 changed files with 145 additions and 69 deletions

View File

@@ -8,7 +8,6 @@ import (
"epic/internal/service"
"epic/internal/util"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gcron"
"github.com/gogf/gf/v2/os/gtime"
"strings"
@@ -37,7 +36,7 @@ func init() {
// StartAllJobs 启动所有定时任务
func (l *Logic) StartAllJobs(ctx context.Context) error {
g.Log().Info(ctx, "Starting all cron jobs...")
util.Info(ctx, "Starting all cron jobs...")
// 启动定时任务调度器
l.cron.Start()
@@ -47,13 +46,13 @@ func (l *Logic) StartAllJobs(ctx context.Context) error {
return err
}
g.Log().Info(ctx, "All cron jobs started successfully")
util.Info(ctx, "All cron jobs started successfully")
return nil
}
// StopAllJobs 停止所有定时任务
func (l *Logic) StopAllJobs(ctx context.Context) error {
g.Log().Info(ctx, "Stopping all cron jobs...")
util.Info(ctx, "Stopping all cron jobs...")
l.jobsMux.Lock()
defer l.jobsMux.Unlock()
@@ -62,13 +61,13 @@ func (l *Logic) StopAllJobs(ctx context.Context) error {
for name, entry := range l.jobs {
l.cron.Remove(entry.Name)
delete(l.jobs, name)
g.Log().Infof(ctx, "Stopped job: %s", name)
util.Infof(ctx, "Stopped job: %s", name)
}
// 停止调度器
l.cron.Stop()
g.Log().Info(ctx, "All cron jobs stopped successfully")
util.Info(ctx, "All cron jobs stopped successfully")
return nil
}
@@ -85,11 +84,11 @@ func (l *Logic) AddJob(ctx context.Context, name, cron string, job func()) error
// 添加任务到调度器
entry, err := l.cron.Add(ctx, cron, func(ctx context.Context) {
startTime := gtime.Now()
g.Log().Infof(ctx, "Starting job: %s at %s", name, startTime.String())
util.Infof(ctx, "Starting job: %s at %s", name, startTime.String())
job()
endTime := gtime.Now()
duration := endTime.Sub(startTime)
g.Log().Infof(ctx, "Completed job: %s, duration: %v", name, duration)
util.Infof(ctx, "Completed job: %s, duration: %v", name, duration)
}, name)
if err != nil {
@@ -98,7 +97,7 @@ func (l *Logic) AddJob(ctx context.Context, name, cron string, job func()) error
// 保存任务引用
l.jobs[name] = entry
g.Log().Infof(ctx, "Added job: %s with cron: %s", name, cron)
util.Infof(ctx, "Added job: %s with cron: %s", name, cron)
return nil
}
@@ -117,7 +116,7 @@ func (l *Logic) RemoveJob(ctx context.Context, name string) error {
l.cron.Remove(entry.Name)
delete(l.jobs, name)
g.Log().Infof(ctx, "Removed job: %s", name)
util.Infof(ctx, "Removed job: %s", name)
return nil
}
@@ -183,11 +182,11 @@ func (l *Logic) registerDefaultJobs(ctx context.Context) error {
// 每天凌晨2点同步i18n远程翻译数据
if err := l.AddJob(ctx, "i18n_remote_sync", "0 0 2 * * *", func() {
g.Log().Info(ctx, "开始同步i18n远程翻译数据...")
util.Info(ctx, "开始同步i18n远程翻译数据...")
if err := i18n.GetI18nLogic().SyncI18nFromRemote(ctx); err != nil {
g.Log().Error(ctx, "i18n远程翻译数据同步失败:", err)
util.Error(ctx, "i18n远程翻译数据同步失败:", err)
} else {
g.Log().Info(ctx, "i18n远程翻译数据同步完成")
util.Info(ctx, "i18n远程翻译数据同步完成")
}
}); err != nil {
return err
@@ -198,44 +197,44 @@ func (l *Logic) registerDefaultJobs(ctx context.Context) error {
// syncDataFromThirdParty 从第三方网站同步数据
func (l *Logic) syncDataFromThirdParty(ctx context.Context) {
g.Log().Info(ctx, "Starting data sync from third party...")
util.Info(ctx, "Starting data sync from third party...")
// 使用第三方数据同步器
if err := l.sync.SyncAllData(ctx); err != nil {
g.Log().Error(ctx, "Data sync failed:", err)
util.Error(ctx, "Data sync failed:", err)
return
}
g.Log().Info(ctx, "Data sync completed")
util.Info(ctx, "Data sync completed")
}
// 同步英雄数据
func (l *Logic) syncHeroData(ctx context.Context) {
g.Log().Info(ctx, "Starting hero data sync...")
util.Info(ctx, "Starting hero data sync...")
if err := l.sync.SyncHeroData(ctx); err != nil {
g.Log().Error(ctx, "Hero data sync failed:", err)
util.Error(ctx, "Hero data sync failed:", err)
return
}
g.Log().Info(ctx, "Hero data sync completed")
util.Info(ctx, "Hero data sync completed")
}
// 同步神器数据
func (l *Logic) syncArtifactData(ctx context.Context) {
g.Log().Info(ctx, "Starting artifact data sync...")
util.Info(ctx, "Starting artifact data sync...")
if err := l.sync.SyncArtifactData(ctx); err != nil {
g.Log().Error(ctx, "Artifact data sync failed:", err)
util.Error(ctx, "Artifact data sync failed:", err)
return
}
g.Log().Info(ctx, "Artifact data sync completed")
util.Info(ctx, "Artifact data sync completed")
}
// healthCheck 健康检查
func (l *Logic) healthCheck(ctx context.Context) {
g.Log().Debug(ctx, "Performing health check...")
util.Debug(ctx, "Performing health check...")
// TODO: 实现健康检查逻辑
// 1. 检查数据库连接
@@ -243,30 +242,30 @@ func (l *Logic) healthCheck(ctx context.Context) {
// 3. 检查第三方API可用性
// 4. 记录系统状态
g.Log().Debug(ctx, "Health check completed")
util.Debug(ctx, "Health check completed")
}
// refreshCache 刷新缓存
func (l *Logic) refreshCache(ctx context.Context) {
g.Log().Info(ctx, "Starting cache refresh...")
util.Info(ctx, "Starting cache refresh...")
// TODO: 实现缓存刷新逻辑
// 1. 刷新英雄数据缓存
// 2. 刷新神器数据缓存
// 3. 刷新其他业务缓存
g.Log().Info(ctx, "Cache refresh completed")
util.Info(ctx, "Cache refresh completed")
}
// 刷新OSS图片预签名URL缓存的定时任务
func (l *Logic) refreshOssPresignUrlCacheJob(ctx context.Context) {
g.Log().Info(ctx, "Starting OSS presigned URL cache refresh...")
util.Info(ctx, "Starting OSS presigned URL cache refresh...")
// 1. 从数据库读取所有英雄图片地址
var dbHeroes []*entity.EpicHeroInfo
err := dao.EpicHeroInfo.Ctx(ctx).Scan(&dbHeroes)
if err != nil {
g.Log().Error(ctx, "Failed to query hero info for OSS presign refresh:", err)
util.Error(ctx, "Failed to query hero info for OSS presign refresh:", err)
return
}
@@ -292,18 +291,18 @@ func (l *Logic) refreshOssPresignUrlCacheJob(ctx context.Context) {
expire := 1 * time.Hour // 预签名URL有效期
err = util.RefreshOssPresignedUrlCache(ctx, keys, expire)
if err != nil {
g.Log().Error(ctx, "OSS presigned URL cache refresh failed:", err)
util.Error(ctx, "OSS presigned URL cache refresh failed:", err)
} else {
g.Log().Info(ctx, "OSS presigned URL cache refresh completed")
util.Info(ctx, "OSS presigned URL cache refresh completed")
}
}
// 新增:定时刷新角色配装信息
func (l *Logic) refreshHeroSetContent(ctx context.Context) {
g.Log().Info(ctx, "Starting hero set content refresh...")
util.Info(ctx, "Starting hero set content refresh...")
if err := l.sync.RefreshAllHeroSetContent(ctx); err != nil {
g.Log().Error(ctx, "Hero set content refresh failed:", err)
util.Error(ctx, "Hero set content refresh failed:", err)
return
}
g.Log().Info(ctx, "Hero set content refresh completed")
util.Info(ctx, "Hero set content refresh completed")
}