124 lines
3.9 KiB
Go
124 lines
3.9 KiB
Go
package service
|
||
|
||
import (
|
||
"context"
|
||
"errors"
|
||
"service/app/mix/conf"
|
||
"service/library/logger"
|
||
"strings"
|
||
|
||
xxl "github.com/xxl-job/xxl-job-executor-go"
|
||
)
|
||
|
||
// 20240117: 接入xxl-job定时任务作业系统
|
||
|
||
var (
|
||
DefaultCronService *CronService
|
||
)
|
||
|
||
type ServerConnInfo struct {
|
||
Ip string
|
||
Port string
|
||
User string
|
||
Password string
|
||
}
|
||
|
||
type CronService struct {
|
||
fileAbsPath string
|
||
serverConnInfos []*ServerConnInfo
|
||
}
|
||
|
||
func NewCronService() *CronService {
|
||
return new(CronService)
|
||
}
|
||
|
||
func (s *CronService) Init(c any) (exec xxl.Executor, 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
|
||
s.serverConnInfos = make([]*ServerConnInfo, 0)
|
||
ips := strings.Split(cfg.ServerInfo.MainServerIps, " ")
|
||
ports := strings.Split(cfg.ServerInfo.MainServerPorts, " ")
|
||
users := strings.Split(cfg.ServerInfo.MainServerUsers, " ")
|
||
passwords := strings.Split(cfg.ServerInfo.MainServerPasswords, " ")
|
||
for i := range ips {
|
||
s.serverConnInfos = append(s.serverConnInfos, &ServerConnInfo{
|
||
Ip: ips[i],
|
||
Port: ports[i],
|
||
User: users[i],
|
||
Password: passwords[i],
|
||
})
|
||
}
|
||
|
||
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.RegTask("reload_blocked_from_being_searched_list", s.ReloadBlockedFromBeingSearchedList)
|
||
exec.RegTask("clear_auto_response_create_times", s.ClearAutoResponseCreateTimes)
|
||
exec.LogHandler(customLogHandle)
|
||
//注册任务handler
|
||
|
||
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
|
||
}
|
||
}
|