50 lines
1.4 KiB
Go
50 lines
1.4 KiB
Go
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(config *config.Config, ctx context.Context, svcCtx *svc.ServiceContext) *CronJob {
|
|
return &CronJob{
|
|
config: config,
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
}
|
|
}
|
|
|
|
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)
|
|
|
|
// 根据配置决定是否启动清理任务
|
|
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("清理数据定时任务已启动")
|
|
}
|
|
|
|
mux := asynq.NewServeMux()
|
|
mux.Handle(types.MsgPaySuccessQuery, NewPaySuccessNotifyUserHandler(l.svcCtx))
|
|
mux.Handle(types.MsgCleanQueryData, NewCleanQueryDataHandler(l.config, l.svcCtx))
|
|
mux.Handle(types.MsgCarMaintenanceQuery, NewCarMaintenanceQueryHandler(l.svcCtx))
|
|
|
|
return mux
|
|
}
|