热度值清除
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.

156 lines
3.6 KiB

9 months ago
package task
import (
"context"
6 months ago
onlinev1 "git.gz.internal.jumaiyx.cn/jm/jmproto/online/v1"
9 months ago
v2 "git.gz.internal.jumaiyx.cn/jm/jmproto/room/v2"
"git.gz.internal.jumaiyx.cn/pkg/client"
9 months ago
configv2 "git.gz.internal.jumaiyx.cn/pkg/config/v2"
"git.gz.internal.jumaiyx.cn/pkg/config/v2/cproto"
k8sclient "git.gz.internal.jumaiyx.cn/pkg/k8s-client/v2"
"git.gz.internal.jumaiyx.cn/pkg/webhook/wechat"
"github.com/redis/go-redis/v9"
"strconv"
"sync"
"time"
)
const (
hotKey = "room:hot:ranking"
)
var (
redisInstance *redis.Client
once sync.Once
)
func NewRedis() *redis.Client {
once.Do(func() {
redisConfig := &cproto.Redis{}
_ = configv2.Get(configv2.Redis, redisConfig)
redisInstance = redis.NewClient(&redis.Options{
Addr: redisConfig.Addr,
Password: redisConfig.Password,
PoolSize: 10,
ReadTimeout: 100 * time.Second,
WriteTimeout: 100 * time.Second,
})
})
return redisInstance
}
func NewTask() {
start := time.Now().Unix()
9 months ago
err := task(0, 10000)
9 months ago
times := time.Now().Unix() - start
if times == 0 {
times = 1
}
wechatHook(times, err)
}
9 months ago
func task(start, end int64) error {
6 months ago
ctx := context.Background()
9 months ago
redisClient := NewRedis()
9 months ago
//defer func() { redisClient.Close() }()
6 months ago
result, err := redisClient.ZRangeWithScores(ctx, hotKey, start, end).Result()
9 months ago
if err != nil {
return err
}
9 months ago
if len(result) == 0 {
return nil
}
var (
members []redis.Z
ids []int64
)
9 months ago
for _, val := range result {
if val.Score > 0 {
id, _ := strconv.Atoi(val.Member.(string))
if id != 0 {
ids = append(ids, int64(id))
members = append(members, redis.Z{
Member: id,
Score: 0,
})
}
}
9 months ago
}
9 months ago
if len(ids) != 0 {
6 months ago
err = redisClient.ZAdd(ctx, hotKey, members...).Err()
9 months ago
if err != nil {
return err
}
8 months ago
6 months ago
//liveClient, liveClientClose, err := client.GetLiveClient(ctx)
//if err != nil {
// return err
//}
//defer func() { _ = liveClientClose() }()
//
//onlineTotalResp, err := liveClient.GetLiveOnlineTotal(ctx, &livev1.GetLiveOnlineTotalReq{
// RoomIdList: ids,
//})
//if err != nil {
// return err
//}
onlineClient, onlineClientClose, err := client.GetOnlineClient(ctx)
8 months ago
if err != nil {
return err
}
6 months ago
defer func() { _ = onlineClientClose() }()
onlineTotalResp, err := onlineClient.GetLiveOnlineTotal(ctx, &onlinev1.GetLiveOnlineTotalRequest{
8 months ago
RoomIdList: ids,
})
if err != nil {
return err
}
6 months ago
roomClient, roomClientClose, err := client.GetRoomClientV2(ctx)
9 months ago
if err != nil {
return err
}
defer func() { _ = roomClientClose() }()
8 months ago
onlineMap := onlineTotalResp.Items
var items []*v2.IncrRoomHotspotReq_Item
for _, id := range ids {
online := onlineMap[id]
items = append(items, &v2.IncrRoomHotspotReq_Item{
RoomId: id,
HotValue: int32(online) * 10,
})
}
6 months ago
_, err = roomClient.IncrRoomHotspot(ctx, &v2.IncrRoomHotspotReq{
8 months ago
Items: items,
9 months ago
})
if err != nil {
return err
}
9 months ago
}
// 递增查询
start = end + 1
end = start + 10000
return task(start, end)
9 months ago
}
func wechatHook(times int64, err error) {
if err != nil {
9 months ago
envi := "测试"
if k8sclient.Environment() == k8sclient.MasterNamespace {
envi = "正式"
}
hook := wechat.NewMarkdown("https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=00dd9216-1f50-42be-8fe1-e66640d7bb27").
Title(1, "直播间热度清理").Br().
Text("环境:" + envi).Br().
Text("耗时:" + strconv.Itoa(int(times)) + "s").Br().Text("状态:")
if err != nil {
hook = hook.FontColor("失败", wechat.Warning).Br().Text("异常:").FontColor(err.Error(), wechat.Warning)
} else {
hook = hook.FontColor("完成", wechat.Info)
}
_ = hook.Send()
9 months ago
}
9 months ago
9 months ago
}