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

110 lines
2.7 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"
"fmt"
2024-02-02 10:11:13 +08:00
"github.com/OpenIMSDK/tools/errs"
2023-11-13 15:21:54 +08:00
"os"
"os/signal"
"syscall"
"time"
2023-06-30 11:24:37 +08:00
2023-11-13 15:21:54 +08:00
"github.com/redis/go-redis/v9"
2023-07-03 16:29:22 +08:00
"github.com/robfig/cron/v3"
"github.com/openimsdk/open-im-server/v3/pkg/common/config"
2023-11-13 15:21:54 +08:00
"github.com/openimsdk/open-im-server/v3/pkg/common/db/cache"
2023-06-30 11:24:37 +08:00
)
2023-08-09 10:37:56 +08:00
func StartTask() error {
2023-06-30 11:24:37 +08:00
fmt.Println("cron task start, config", config.Config.ChatRecordsClearTime)
2024-02-02 10:11:13 +08:00
2023-06-30 11:24:37 +08:00
msgTool, err := InitMsgTool()
if err != nil {
return err
}
2023-11-13 15:21:54 +08:00
msgTool.convertTools()
rdb, err := cache.NewRedis()
if err != nil {
return err
}
// register cron tasks
var crontab = cron.New()
2024-02-02 10:11:13 +08:00
fmt.Println("start chatRecordsClearTime cron task", "cron config", config.Config.ChatRecordsClearTime)
2023-11-13 15:21:54 +08:00
_, err = crontab.AddFunc(config.Config.ChatRecordsClearTime, cronWrapFunc(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-02-02 10:11:13 +08:00
fmt.Println("start msgDestruct cron task", "cron config", config.Config.MsgDestructTime)
2023-11-13 15:21:54 +08:00
_, err = crontab.AddFunc(config.Config.MsgDestructTime, cronWrapFunc(rdb, "cron_conversations_destruct_msgs", msgTool.ConversationsDestructMsgs))
if err != nil {
2024-02-02 10:11:13 +08:00
return errs.Wrap(err)
}
2023-11-13 15:21:54 +08:00
// start crontab
crontab.Start()
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
<-sigs
// stop crontab, Wait for the running task to exit.
ctx := crontab.Stop()
select {
case <-ctx.Done():
// 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
}
func cronWrapFunc(rdb redis.UniversalClient, key string, fn func()) func() {
enableCronLocker := config.Config.EnableCronLocker
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()
}
}
}