You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Open-IM-Server/internal/cron_task/cron_task.go

78 lines
2.6 KiB

3 years ago
package cronTask
import (
3 years ago
"Open_IM/pkg/common/config"
3 years ago
"Open_IM/pkg/common/constant"
"Open_IM/pkg/common/db/mysql_model/im_mysql_model"
3 years ago
rocksCache "Open_IM/pkg/common/db/rocks_cache"
3 years ago
"Open_IM/pkg/common/log"
"Open_IM/pkg/utils"
3 years ago
"fmt"
3 years ago
"github.com/robfig/cron/v3"
3 years ago
"time"
3 years ago
)
const cronTaskOperationID = "cronTaskOperationID-"
func StartCronTask() {
2 years ago
log.NewPrivateLog("cron")
3 years ago
log.NewInfo(utils.OperationIDGenerator(), "start cron task")
c := cron.New()
3 years ago
fmt.Println("config", config.Config.Mongo.ChatRecordsClearTime)
3 years ago
_, err := c.AddFunc(config.Config.Mongo.ChatRecordsClearTime, func() {
3 years ago
operationID := getCronTaskOperationID()
2 years ago
log.NewInfo(operationID, "====================== start del cron task ======================")
3 years ago
userIDList, err := im_mysql_model.SelectAllUserID()
if err == nil {
log.NewDebug(operationID, utils.GetSelfFuncName(), "userIDList: ", userIDList)
for _, userID := range userIDList {
3 years ago
if err := DeleteMongoMsgAndResetRedisSeq(operationID, userID); err != nil {
log.NewError(operationID, utils.GetSelfFuncName(), err.Error(), userID)
3 years ago
}
3 years ago
if err := checkMaxSeqWithMongo(operationID, userID, constant.WriteDiffusion); err != nil {
log.NewError(operationID, utils.GetSelfFuncName(), userID, err)
}
3 years ago
}
} else {
log.NewError(operationID, utils.GetSelfFuncName(), err.Error())
}
workingGroupIDList, err := im_mysql_model.GetGroupIDListByGroupType(constant.WorkingGroup)
if err == nil {
for _, groupID := range workingGroupIDList {
3 years ago
userIDList, err = rocksCache.GetGroupMemberIDListFromCache(groupID)
if err != nil {
log.NewError(operationID, utils.GetSelfFuncName(), err.Error(), groupID)
continue
}
log.NewDebug(operationID, utils.GetSelfFuncName(), "groupID:", groupID, "userIDList:", userIDList)
3 years ago
if err := ResetUserGroupMinSeq(operationID, groupID, userIDList); err != nil {
log.NewError(operationID, utils.GetSelfFuncName(), err.Error(), groupID, userIDList)
3 years ago
}
3 years ago
if err := checkMaxSeqWithMongo(operationID, groupID, constant.ReadDiffusion); err != nil {
log.NewError(operationID, utils.GetSelfFuncName(), groupID, err)
}
3 years ago
}
} else {
log.NewError(operationID, utils.GetSelfFuncName(), err.Error())
return
}
2 years ago
log.NewInfo(operationID, "====================== start del cron finished ======================")
3 years ago
})
if err != nil {
3 years ago
fmt.Println("start cron failed", err.Error())
3 years ago
panic(err)
}
3 years ago
3 years ago
c.Start()
3 years ago
fmt.Println("start cron task success")
3 years ago
for {
time.Sleep(time.Second)
}
3 years ago
}
func getCronTaskOperationID() string {
return cronTaskOperationID + utils.OperationIDGenerator()
}