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

121 lines
3.0 KiB
Go
Raw Normal View History

2023-07-04 11:15:20 +08:00
// Copyright © 2023 OpenIM. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2023-06-30 11:24:37 +08:00
package tools
import (
"context"
2024-04-19 22:23:08 +08:00
"github.com/openimsdk/open-im-server/v3/pkg/common/config"
"github.com/openimsdk/tools/db/redisutil"
2023-11-13 15:21:54 +08:00
"os"
"os/signal"
"syscall"
"time"
2023-06-30 11:24:37 +08:00
2024-04-19 22:23:08 +08:00
"github.com/openimsdk/tools/errs"
"github.com/openimsdk/tools/log"
2024-03-06 15:58:05 +08:00
"github.com/redis/go-redis/v9"
"github.com/robfig/cron/v3"
2023-06-30 11:24:37 +08:00
)
2024-04-19 22:23:08 +08:00
type CronTaskConfig struct {
CronTask config.CronTask
RedisConfig config.Redis
MongodbConfig config.Mongo
ZookeeperConfig config.ZooKeeper
Share config.Share
KafkaConfig config.Kafka
}
func Start(ctx context.Context, config *CronTaskConfig) error {
log.CInfo(ctx, "CRON-TASK server is initializing", "chatRecordsClearTime",
config.CronTask.ChatRecordsClearTime, "msgDestructTime", config.CronTask.MsgDestructTime)
2024-02-02 10:11:13 +08:00
2024-04-19 22:23:08 +08:00
msgTool, err := InitMsgTool(ctx, config)
2023-06-30 11:24:37 +08:00
if err != nil {
return err
}
2023-11-13 15:21:54 +08:00
msgTool.convertTools()
2024-04-19 22:23:08 +08:00
rdb, err := redisutil.NewRedisClient(ctx, config.RedisConfig.Build())
2023-11-13 15:21:54 +08:00
if err != nil {
return err
}
// register cron tasks
var crontab = cron.New()
2024-04-19 22:23:08 +08:00
_, err = crontab.AddFunc(config.CronTask.ChatRecordsClearTime,
cronWrapFunc(config, rdb, "cron_clear_msg_and_fix_seq", msgTool.AllConversationClearMsgAndFixSeq))
2023-06-30 11:24:37 +08:00
if err != nil {
2024-02-02 10:11:13 +08:00
return errs.Wrap(err)
2023-07-12 15:31:24 +08:00
}
2023-11-13 15:21:54 +08:00
2024-04-19 22:23:08 +08:00
_, err = crontab.AddFunc(config.CronTask.MsgDestructTime,
cronWrapFunc(config, rdb, "cron_conversations_destruct_msgs", msgTool.ConversationsDestructMsgs))
if err != nil {
2024-04-19 22:23:08 +08:00
return errs.WrapMsg(err, "cron_conversations_destruct_msgs")
}
2023-11-13 15:21:54 +08:00
// start crontab
crontab.Start()
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGTERM)
2023-11-13 15:21:54 +08:00
<-sigs
// stop crontab, Wait for the running task to exit.
2024-04-19 22:23:08 +08:00
cronCtx := crontab.Stop()
2023-11-13 15:21:54 +08:00
select {
2024-04-19 22:23:08 +08:00
case <-cronCtx.Done():
2023-11-13 15:21:54 +08:00
// graceful exit
case <-time.After(15 * time.Second):
// forced exit on timeout
}
2023-06-30 11:24:37 +08:00
return nil
}
2023-11-13 15:21:54 +08:00
// netlock redis lock.
func netlock(rdb redis.UniversalClient, key string, ttl time.Duration) bool {
value := "used"
ok, err := rdb.SetNX(context.Background(), key, value, ttl).Result() // nolint
if err != nil {
// when err is about redis server, return true.
return false
}
return ok
}
2024-04-19 22:23:08 +08:00
func cronWrapFunc(config *CronTaskConfig, rdb redis.UniversalClient, key string, fn func()) func() {
enableCronLocker := config.CronTask.EnableCronLocker
2023-11-13 15:21:54 +08:00
return func() {
// if don't enable cron-locker, call fn directly.
if !enableCronLocker {
fn()
return
}
// when acquire redis lock, call fn().
if netlock(rdb, key, 5*time.Second) {
fn()
}
}
}