This commit is contained in:
2025-05-27 18:35:01 +08:00
parent ca66cc91d4
commit 79e1ba2616
115 changed files with 22039 additions and 1045 deletions

View File

@@ -3,19 +3,23 @@ package queue
import (
"context"
"fmt"
"tyc-server/app/main/api/internal/config"
"tyc-server/app/main/api/internal/svc"
"tyc-server/app/main/api/internal/types"
"github.com/hibiken/asynq"
"github.com/zeromicro/go-zero/core/logx"
)
type CronJob struct {
config *config.Config
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewCronJob(ctx context.Context, svcCtx *svc.ServiceContext) *CronJob {
func NewCronJob(config *config.Config, ctx context.Context, svcCtx *svc.ServiceContext) *CronJob {
return &CronJob{
config: config,
ctx: ctx,
svcCtx: svcCtx,
}
@@ -24,17 +28,21 @@ func NewCronJob(ctx context.Context, svcCtx *svc.ServiceContext) *CronJob {
func (l *CronJob) Register() *asynq.ServeMux {
redisClientOpt := asynq.RedisClientOpt{Addr: l.svcCtx.Config.CacheRedis[0].Host, Password: l.svcCtx.Config.CacheRedis[0].Pass}
scheduler := asynq.NewScheduler(redisClientOpt, nil)
task := asynq.NewTask(types.MsgCleanQueryData, nil, nil)
_, err := scheduler.Register(TASKTIME, task)
if err != nil {
panic(fmt.Sprintf("定时任务注册失败:%v", err))
// 根据配置决定是否启动清理任务
if l.config.CleanTask.Enabled {
task := asynq.NewTask(types.MsgCleanQueryData, nil, nil)
_, err := scheduler.Register(l.config.CleanTask.Time, task)
if err != nil {
panic(fmt.Sprintf("定时任务注册失败:%v", err))
}
scheduler.Start()
logx.Infof("清理数据定时任务已启动")
}
scheduler.Start()
fmt.Println("定时任务启动!!!")
mux := asynq.NewServeMux()
mux.Handle(types.MsgPaySuccessQuery, NewPaySuccessNotifyUserHandler(l.svcCtx))
mux.Handle(types.MsgCleanQueryData, NewCleanQueryDataHandler(l.svcCtx))
mux.Handle(types.MsgCleanQueryData, NewCleanQueryDataHandler(l.config, l.svcCtx))
mux.Handle(types.MsgCarMaintenanceQuery, NewCarMaintenanceQueryHandler(l.svcCtx))
return mux