From 50cfc23ad28fa16fe02f957d92cbe13b54b313fc Mon Sep 17 00:00:00 2001 From: ayflying Date: Mon, 1 Sep 2025 18:12:58 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=A1=E5=88=92=E4=BB=BB=E5=8A=A1=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=EF=BC=8Cact=E4=B8=8Ekv=E4=BD=BF=E7=94=A8=E5=8D=8F?= =?UTF-8?q?=E7=A8=8B=E6=96=B9=E5=BC=8F=E6=89=A7=E8=A1=8C=EF=BC=8C=E4=B8=8D?= =?UTF-8?q?=E5=BD=B1=E5=93=8D=E5=85=B6=E4=BB=96=E4=BB=BB=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/boot/boot.go | 12 +++++++++--- internal/logic/gameAct/gameAct.go | 14 +++++++++++--- internal/logic/gameKv/gameKv.go | 3 ++- internal/logic/systemCron/systemCron.go | 4 ++-- service/game_kv.go | 6 +++++- service/system_cron.go | 4 ++-- 6 files changed, 31 insertions(+), 12 deletions(-) diff --git a/internal/boot/boot.go b/internal/boot/boot.go index ae06a91..25a4c32 100644 --- a/internal/boot/boot.go +++ b/internal/boot/boot.go @@ -5,6 +5,7 @@ import ( v1 "github.com/ayflying/utility_go/api/system/v1" "github.com/ayflying/utility_go/service" + "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/os/gctx" ) @@ -19,9 +20,14 @@ func Boot() (err error) { //用户活动持久化每小时执行一次 service.SystemCron().AddCronV2(v1.CronType_HOUR, func(ctx context.Context) error { - err = service.GameKv().SavesV1() - err = service.GameAct().Saves(ctx) - return err + go func() { + err = service.GameKv().SavesV1(ctx) + err = service.GameAct().Saves(ctx) + if err != nil { + g.Log().Error(ctx, err) + } + }() + return nil }, true) //初始化自启动方法 diff --git a/internal/logic/gameAct/gameAct.go b/internal/logic/gameAct/gameAct.go index bb54907..e72d384 100644 --- a/internal/logic/gameAct/gameAct.go +++ b/internal/logic/gameAct/gameAct.go @@ -212,7 +212,7 @@ func (s *sGameAct) Save(ctx context.Context, actId int) (err error) { //批量写入数据库 updateCount := 0 - if len(delKey) > 0 { + if len(delKey) > 200 { for _, v := range update { v.UpdatedAt = gtime.Now() updateRes, err2 := g.Model(Name).Where(do.GameAct{ @@ -235,7 +235,7 @@ func (s *sGameAct) Save(ctx context.Context, actId int) (err error) { var count int64 if len(add) > 0 { - dbRes, err2 := g.Model(Name).Batch(50).Data(add).Save() + dbRes, err2 := g.Model(Name).Data(add).Save() add = make([]*entity.GameAct, 0) err = err2 if err != nil { @@ -243,7 +243,15 @@ func (s *sGameAct) Save(ctx context.Context, actId int) (err error) { return } count, _ = dbRes.RowsAffected() - g.Log().Debugf(ctx, "当前 %v 写入数据库: %v 条", actId, count) + if count == 0 { + g.Log().Error(ctx, "当前 %v 写入数据库: %v 条", actId, count) + for _, vTemp := range add { + g.Log().Debugf(ctx, "当前act:%v,add写入数据: %v,内容:%v", vTemp.ActId, vTemp.Uid, vTemp.Action) + } + return + + } + //g.Log().Debugf(ctx, "当前 %v 写入数据库: %v 条", actId, count) } for _, v := range delKey { diff --git a/internal/logic/gameKv/gameKv.go b/internal/logic/gameKv/gameKv.go index d530d73..fdb5316 100644 --- a/internal/logic/gameKv/gameKv.go +++ b/internal/logic/gameKv/gameKv.go @@ -1,6 +1,7 @@ package gameKv import ( + "context" "errors" "fmt" "strconv" @@ -39,7 +40,7 @@ func init() { // @Description: 保存用户KV数据列表。 // @receiver s: sGameKv的实例。 // @return err: 错误信息,如果操作成功,则为nil。 -func (s *sGameKv) SavesV1() (err error) { +func (s *sGameKv) SavesV1(ctx context.Context) (err error) { // 最大允许执行时间 RunTimeMax = gtime.Now().Add(time.Minute * 30) g.Log().Debug(ctx, "开始执行游戏kv数据保存") diff --git a/internal/logic/systemCron/systemCron.go b/internal/logic/systemCron/systemCron.go index d4831b1..103ada1 100644 --- a/internal/logic/systemCron/systemCron.go +++ b/internal/logic/systemCron/systemCron.go @@ -60,7 +60,7 @@ type sSystemCron struct { func New() *sSystemCron { return &sSystemCron{ taskChan: make(chan func(context.Context) error, 2), - TaskTimeout: time.Minute * 30, + TaskTimeout: time.Minute * 60, } } @@ -333,7 +333,7 @@ func (s *sSystemCron) RunFuncChan() { //ctx := gctx.New() func() { //超时释放资源 - ctx, cancel := context.WithTimeout(context.Background(), s.TaskTimeout) + ctx, cancel := context.WithTimeout(gctx.New(), s.TaskTimeout) defer cancel() // 使用匿名函数包裹来捕获 panic diff --git a/service/game_kv.go b/service/game_kv.go index 6c3acd4..cac5a78 100644 --- a/service/game_kv.go +++ b/service/game_kv.go @@ -5,6 +5,10 @@ package service +import ( + "context" +) + type ( IGameKv interface { // SavesV1 方法 @@ -12,7 +16,7 @@ type ( // @Description: 保存用户KV数据列表。 // @receiver s: sGameKv的实例。 // @return err: 错误信息,如果操作成功,则为nil。 - SavesV1() (err error) + SavesV1(ctx context.Context) (err error) } ) diff --git a/service/system_cron.go b/service/system_cron.go index fdb1046..199fdd3 100644 --- a/service/system_cron.go +++ b/service/system_cron.go @@ -38,8 +38,8 @@ type ( // @receiver s: sSystemCron的实例,代表一个调度系统。 // @param typ: 任务的类型,决定该任务将被添加到哪个列表中。对应不同的时间间隔。 // @param _func: 要添加的任务函数,该函数执行时应该返回一个error。 - // @param unique: 是否只在唯一服务器上执行 - AddCronV2(typ v1.CronType, _func func(context.Context) error, unique ...bool) + // @param _onlyMain: 是否只在主服务器上执行一次,true 唯一执行,false 全局执行不判断唯一 + AddCronV2(typ v1.CronType, _func func(context.Context) error, _onlyMain ...bool) // StartCron 开始计划任务执行 // // @Description: