service/app/mix/service/cronservice.go

101 lines
3.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package service
import (
"context"
"errors"
"service/app/mix/conf"
"service/library/logger"
xxl "github.com/xxl-job/xxl-job-executor-go"
)
// 20240117: 接入xxl-job定时任务作业系统
var (
DefaultCronService *CronService
)
type CronService struct {
fileAbsPath string
}
func NewCronService() *CronService {
return new(CronService)
}
func (s *CronService) Init(c any) (err error) {
cfg, ok := c.(*conf.ConfigSt)
if !ok {
err = errors.New("cfg struct type not expected")
logger.Error("service init, err: %v", err)
return
}
s.fileAbsPath = cfg.Log.FileAbsPath
exec := xxl.NewExecutor(
xxl.ServerAddr(cfg.XxlJob.ServerAddr), //xxl-job-admin部署地址
xxl.AccessToken(cfg.XxlJob.AccessToken), //请求令牌(默认为空)
xxl.ExecutorIp(cfg.XxlJob.ExecutorIp), //可自动获取
xxl.ExecutorPort(cfg.XxlJob.ExecutorPort), //默认9999非必填
xxl.RegistryKey(cfg.XxlJob.RegistryKey), //执行器名称
xxl.SetLogger(&XxlLogger{}), //自定义日志
)
exec.Init()
exec.Use(customMiddleware)
//设置日志查看handler
exec.RegTask("reload_recomm_list", s.ReloadRecommList)
exec.RegTask("clear_veri_code_send_times", s.ClearVeriCodeSendTimes)
exec.RegTask("clear_moment_create_times", s.ClearMomentCreateTimes)
exec.RegTask("create_daily_statement", s.CreateDailyStatement)
exec.RegTask("image_audit_batch", s.ImageAuditBatch)
exec.RegTask("image_audit_batch_his", s.ImageAuditBatchHis)
exec.RegTask("text_audit_batch", s.TextAuditBatch)
exec.RegTask("text_audit_batch_his", s.TextAuditBatchHis)
exec.RegTask("create_veri_code", s.CreateVeriCode)
//exec.RegTask("clear_content_audit_batch_execution_logs", s.ClearContentAuditBatchExecutionLogs)
exec.RegTask("send_contact_customer_services_of_last_minute", s.SendContactCustomerServicesOfLastMinute)
exec.RegTask("cancel_account_at_due_time", s.CancelAccountsAtDueTime)
exec.RegTask("reload_moment_recomm_list", s.ReloadMomentRecommList)
exec.RegTask("clear_zone_moment_create_times", s.ClearZoneMomentCreateTimes)
exec.RegTask("clear_wrong_pswd_times", s.ClearWrongPswdTimes)
exec.RegTask("video_moderation_batch", s.VideoModerationBatch)
exec.RegTask("video_moderation_batch_his", s.VideoModerationBatchHis)
exec.RegTask("clear_expired_btcb", s.ClearExpiredBtcb)
exec.LogHandler(customLogHandle)
//注册任务handler
go exec.Run()
return
}
// 自定义日志处理器
func customLogHandle(req *xxl.LogReq) *xxl.LogRes {
return &xxl.LogRes{Code: xxl.SuccessCode, Msg: "", Content: xxl.LogResContent{
FromLineNum: req.FromLineNum,
ToLineNum: 2,
LogContent: "这个是自定义日志handler",
IsEnd: true,
}}
}
// xxl.Logger接口实现
type XxlLogger struct{}
func (l *XxlLogger) Info(format string, a ...interface{}) {
logger.Info(format, a)
}
func (l *XxlLogger) Error(format string, a ...interface{}) {
logger.Error(format, a)
}
func customMiddleware(tf xxl.TaskFunc) xxl.TaskFunc {
return func(cxt context.Context, param *xxl.RunReq) string {
logger.Info("task:[job_id:%v] start", param.JobID)
res := tf(cxt, param)
logger.Info("task:[job_id:%v] end", param.JobID)
return res
}
}