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.

58 lines
1.1 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package timer
import (
"fmt"
"math/rand"
"time"
"github.com/didi/nightingale/v5/cache"
"github.com/didi/nightingale/v5/models"
"github.com/toolkits/pkg/logger"
)
// userid->user 将数据库中的用户信息缓存在内存里,
// 在生成告警事件的时候根据用户ID快速找到用户的详情
func SyncUsers() {
err := syncUsers()
if err != nil {
fmt.Println("timer: sync users fail:", err)
exit(1)
}
go loopSyncUsers()
}
func loopSyncUsers() {
randtime := rand.Intn(9000)
fmt.Printf("timer: sync users: random sleep %dms\n", randtime)
time.Sleep(time.Duration(randtime) * time.Millisecond)
for {
time.Sleep(time.Second * time.Duration(9))
err := syncUsers()
if err != nil {
logger.Warning("timer: sync users fail:", err)
}
}
}
func syncUsers() error {
start := time.Now()
users, err := models.UserGetAll()
if err != nil {
return err
}
usersMap := make(map[int64]*models.User)
for i := range users {
usersMap[users[i].Id] = &users[i]
}
cache.UserCache.SetAll(usersMap)
logger.Debugf("timer: sync users done, cost: %dms", time.Since(start).Milliseconds())
return nil
}