init: 初始化 web_hgdjb_server 项目
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
package ipstat
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
"videoweb/pub/common/enum/redisconst"
|
||||
"videoweb/pub/common/log"
|
||||
"videoweb/pub/common/utils"
|
||||
"videoweb/pub/models/ipdailystatmod"
|
||||
"videoweb/pub/pubdef"
|
||||
"videoweb/pub/redis"
|
||||
)
|
||||
|
||||
const syncInterval = 5 * time.Minute
|
||||
|
||||
func StartSync() {
|
||||
go func() {
|
||||
syncOnce()
|
||||
ticker := time.NewTicker(syncInterval)
|
||||
defer ticker.Stop()
|
||||
for range ticker.C {
|
||||
syncOnce()
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func syncOnce() {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
||||
defer cancel()
|
||||
|
||||
now := time.Now().In(utils.LocBJ)
|
||||
appId := int64(pubdef.AppId)
|
||||
|
||||
for _, offset := range []int{0, -1} {
|
||||
date := now.AddDate(0, 0, offset).Format("20060102")
|
||||
key := redisconst.IpStatKey(appId, date)
|
||||
|
||||
count, err := redis.SCard(key)
|
||||
if err != nil {
|
||||
log.Warn("ipstat sync: SCard failed", log.Any("date", date), log.E(err))
|
||||
continue
|
||||
}
|
||||
|
||||
if err := ipdailystatmod.UpsertCount(ctx, appId, date, count); err != nil {
|
||||
log.Warn("ipstat sync: UpsertCount failed",
|
||||
log.Any("appId", appId), log.Any("date", date), log.E(err))
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user