Files
open-im-server/internal/cron_task/cron_task.go
T

78 lines
2.6 KiB
Go
Raw Normal View History

2022-08-09 16:38:33 +08:00
package cronTask
import (
2022-08-10 15:14:51 +08:00
"Open_IM/pkg/common/config"
2022-08-09 16:38:33 +08:00
"Open_IM/pkg/common/constant"
"Open_IM/pkg/common/db/mysql_model/im_mysql_model"
2022-08-09 18:48:11 +08:00
rocksCache "Open_IM/pkg/common/db/rocks_cache"
2022-08-09 16:38:33 +08:00
"Open_IM/pkg/common/log"
"Open_IM/pkg/utils"
2022-08-10 12:02:50 +08:00
"fmt"
2022-08-09 16:38:33 +08:00
"github.com/robfig/cron/v3"
2022-08-10 12:09:28 +08:00
"time"
2022-08-09 16:38:33 +08:00
)
const cronTaskOperationID = "cronTaskOperationID-"
func StartCronTask() {
2022-08-12 18:37:51 +08:00
log.NewPrivateLog("cron")
2022-08-09 16:38:33 +08:00
log.NewInfo(utils.OperationIDGenerator(), "start cron task")
c := cron.New()
2022-08-10 15:19:24 +08:00
fmt.Println("config", config.Config.Mongo.ChatRecordsClearTime)
2022-08-10 15:14:51 +08:00
_, err := c.AddFunc(config.Config.Mongo.ChatRecordsClearTime, func() {
2022-08-09 16:38:33 +08:00
operationID := getCronTaskOperationID()
2022-08-11 12:09:44 +08:00
log.NewInfo(operationID, "====================== start del cron task ======================")
2022-08-09 16:38:33 +08:00
userIDList, err := im_mysql_model.SelectAllUserID()
if err == nil {
log.NewDebug(operationID, utils.GetSelfFuncName(), "userIDList: ", userIDList)
for _, userID := range userIDList {
2022-08-09 18:48:11 +08:00
if err := DeleteMongoMsgAndResetRedisSeq(operationID, userID); err != nil {
log.NewError(operationID, utils.GetSelfFuncName(), err.Error(), userID)
2022-08-09 16:38:33 +08:00
}
2022-08-10 19:31:57 +08:00
if err := checkMaxSeqWithMongo(operationID, userID, constant.WriteDiffusion); err != nil {
log.NewError(operationID, utils.GetSelfFuncName(), userID, err)
}
2022-08-09 16:38:33 +08:00
}
} else {
log.NewError(operationID, utils.GetSelfFuncName(), err.Error())
}
workingGroupIDList, err := im_mysql_model.GetGroupIDListByGroupType(constant.WorkingGroup)
if err == nil {
for _, groupID := range workingGroupIDList {
2022-08-09 18:48:11 +08:00
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)
2022-08-10 12:02:50 +08:00
if err := ResetUserGroupMinSeq(operationID, groupID, userIDList); err != nil {
log.NewError(operationID, utils.GetSelfFuncName(), err.Error(), groupID, userIDList)
2022-08-09 16:38:33 +08:00
}
2022-08-10 19:31:57 +08:00
if err := checkMaxSeqWithMongo(operationID, groupID, constant.ReadDiffusion); err != nil {
log.NewError(operationID, utils.GetSelfFuncName(), groupID, err)
}
2022-08-09 16:38:33 +08:00
}
} else {
log.NewError(operationID, utils.GetSelfFuncName(), err.Error())
return
}
2022-08-11 12:09:44 +08:00
log.NewInfo(operationID, "====================== start del cron finished ======================")
2022-08-09 16:38:33 +08:00
})
if err != nil {
2022-08-10 12:02:50 +08:00
fmt.Println("start cron failed", err.Error())
2022-08-09 16:38:33 +08:00
panic(err)
}
2022-08-10 19:31:57 +08:00
2022-08-09 16:38:33 +08:00
c.Start()
2022-08-10 12:08:28 +08:00
fmt.Println("start cron task success")
2022-08-10 12:09:28 +08:00
for {
time.Sleep(time.Second)
}
2022-08-09 16:38:33 +08:00
}
func getCronTaskOperationID() string {
return cronTaskOperationID + utils.OperationIDGenerator()
}