计划任务修改,act与kv使用协程方式执行,不影响其他任务

This commit is contained in:
ayflying
2025-09-01 18:12:58 +08:00
parent 95539038c0
commit 50cfc23ad2
6 changed files with 31 additions and 12 deletions

View File

@@ -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)
//初始化自启动方法

View File

@@ -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%vadd写入数据: %v,内容:%v", vTemp.ActId, vTemp.Uid, vTemp.Action)
}
return
}
//g.Log().Debugf(ctx, "当前 %v 写入数据库: %v 条", actId, count)
}
for _, v := range delKey {

View File

@@ -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数据保存")

View File

@@ -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