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/crontask/cron_task.go

44 lines
1.1 KiB

2 years ago
package cronTask
import (
2 years ago
"Open_IM/pkg/common/config"
2 years ago
"Open_IM/pkg/common/log"
"Open_IM/pkg/utils"
2 years ago
"fmt"
2 years ago
"time"
2 years ago
"github.com/robfig/cron/v3"
2 years ago
)
const cronTaskOperationID = "cronTaskOperationID-"
2 years ago
const moduleName = "cron"
2 years ago
2 years ago
func StartCronTask(userID, workingGroupID string) {
2 years ago
log.NewPrivateLog(moduleName)
2 years ago
log.NewInfo(utils.OperationIDGenerator(), "start cron task", "cron config", config.Config.Mongo.ChatRecordsClearTime)
2 years ago
fmt.Println("cron task start, config", config.Config.Mongo.ChatRecordsClearTime)
2 years ago
if userID != "" {
2 years ago
operationID := getCronTaskOperationID()
2 years ago
ClearUsersMsg(operationID, []string{userID})
2 years ago
}
if workingGroupID != "" {
operationID := getCronTaskOperationID()
2 years ago
ClearSuperGroupMsg(operationID, []string{workingGroupID})
2 years ago
}
if userID != "" || workingGroupID != "" {
fmt.Println("clear msg finished")
return
}
c := cron.New()
_, err := c.AddFunc(config.Config.Mongo.ChatRecordsClearTime, ClearAll)
2 years ago
if err != nil {
2 years ago
fmt.Println("start cron failed", err.Error(), config.Config.Mongo.ChatRecordsClearTime)
2 years ago
panic(err)
}
c.Start()
2 years ago
fmt.Println("start cron task success")
2 years ago
for {
2 years ago
time.Sleep(10 * time.Second)
2 years ago
}
2 years ago
}