82 lines
2.4 KiB
Go
82 lines
2.4 KiB
Go
package game_notify
|
||
|
||
import (
|
||
"context"
|
||
"encoding/json"
|
||
"github.com/robfig/cron/v3"
|
||
"github.com/zeromicro/go-zero/core/logx"
|
||
"nova_task/internal/model"
|
||
"nova_task/internal/svc"
|
||
)
|
||
|
||
type Cron struct {
|
||
ctx context.Context
|
||
svcCtx *svc.ServiceContext
|
||
}
|
||
|
||
func NewCron(ctx context.Context, svcCtx *svc.ServiceContext) cron.Job {
|
||
return &Cron{
|
||
ctx: ctx,
|
||
svcCtx: svcCtx,
|
||
}
|
||
}
|
||
|
||
func (c *Cron) Spec() string {
|
||
return "@every 30s"
|
||
}
|
||
|
||
func (c *Cron) Run() {
|
||
c.Process(model.NTF_STATUS_WAITING_PROCESS)
|
||
c.Process(model.NTF_STATUS_FAILED)
|
||
}
|
||
|
||
func (c *Cron) Process(status int8) {
|
||
nfs, err := c.svcCtx.GameServerNotifyModel.FindNotifyList(c.ctx, status)
|
||
if err != nil {
|
||
logx.Errorw("find notify list failed", logx.Field("err", err))
|
||
return
|
||
}
|
||
if len(nfs) == 0 {
|
||
logx.Debug("no notify")
|
||
return
|
||
}
|
||
for _, nf := range nfs {
|
||
var arg = map[string]any{}
|
||
if nf.Data.Valid {
|
||
err = json.Unmarshal([]byte(nf.Data.String), &arg)
|
||
if err != nil {
|
||
logx.Errorw("unmarshal notify data failed", logx.Field("err", err), logx.Field("id", nf.Id))
|
||
continue
|
||
}
|
||
}
|
||
|
||
err := c.svcCtx.GameServerNotifyModel.UpdateNotifyStatus(c.ctx, nf.Id, model.NTF_STATUS_PROCESSING)
|
||
if err != nil {
|
||
logx.Errorw("update notify status failed", logx.Field("err", err), logx.Field("id", nf.Id))
|
||
continue
|
||
}
|
||
_, err = c.svcCtx.GameAction(c.ctx, int64(nf.RoleId), nf.Action, arg)
|
||
if err != nil {
|
||
c.svcCtx.GameServerNotifyModel.UpdateNotifyStatus(c.ctx, nf.Id, model.NTF_STATUS_FAILED)
|
||
} else {
|
||
c.svcCtx.GameServerNotifyModel.UpdateNotifyStatus(c.ctx, nf.Id, model.NTF_STATUS_SUCCESS)
|
||
}
|
||
}
|
||
}
|
||
|
||
/*
|
||
CREATE TABLE `nh_game_server_notify`
|
||
(
|
||
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
|
||
`role_id` bigint(20) unsigned NOT NULL COMMENT '角色id',
|
||
`action` varchar(128) NOT NULL COMMENT '事件类型',
|
||
`data` text NULL DEFAULT NULL COMMENT '事件数据',
|
||
`status` tinyint NOT NULL DEFAULT 0 COMMENT '状态:1=待处理,2=处理中, 3=处理失败, 4=处理成功',
|
||
`retry_times` int NOT NULL DEFAULT 0 COMMENT '重试次数',
|
||
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
|
||
`updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
|
||
PRIMARY KEY (`id`),
|
||
INDEX (`status`)
|
||
) COMMENT ='游戏服通知事件';
|
||
*/
|