补齐相关日志日志

This commit is contained in:
2026-09-16 00:23:49 +08:00
parent 8c2ea84939
commit b2e5eef3c8
39 changed files with 1576 additions and 176 deletions
+17 -2
View File
@@ -3,6 +3,7 @@ package service
import (
"context"
"encoding/json"
"log/slog"
"time"
"git.lzcwy.cn/zhengchiliu/zt-closet-backend/global"
@@ -36,11 +37,14 @@ func (s *HoroscopeService) GetToday(ctx context.Context, userID int64) (*dto.Hor
user, err := s.userDao.UserGetByID(ctx, userID)
if err != nil {
if isNotFound(err) {
slog.WarnContext(ctx, "horoscope: user not found", "user_id", userID)
return nil, errcode.UserNotFound
}
slog.ErrorContext(ctx, "horoscope: query user failed", "user_id", userID, "error", err)
return nil, errcode.DatabaseError
}
if user.BirthDay == 0 {
slog.InfoContext(ctx, "horoscope: birth day missing", "user_id", userID)
return nil, errcode.InvalidParams.WithDetails("请先完善生日信息")
}
@@ -50,9 +54,14 @@ func (s *HoroscopeService) GetToday(ctx context.Context, userID int64) (*dto.Hor
// 1. Redis 优先。
if data, cerr := redisdao.HoroscopeGet(ctx, zodiac, statDate); cerr == nil && len(data) > 0 {
var response dto.HoroscopeResponse
if json.Unmarshal(data, &response) == nil {
if uerr := json.Unmarshal(data, &response); uerr == nil {
return &response, nil
} else {
slog.DebugContext(ctx, "horoscope: unmarshal cached data failed, fallback to db",
"zodiac", zodiac, "error", uerr)
}
} else if cerr != nil {
slog.DebugContext(ctx, "horoscope: cache read failed, fallback to db", "zodiac", zodiac, "error", cerr)
}
// 2. 回落 MySQL。
@@ -60,9 +69,11 @@ func (s *HoroscopeService) GetToday(ctx context.Context, userID int64) (*dto.Hor
if derr != nil {
if isNotFound(derr) {
// 今日运势尚未生成,异步触发兜底生成,本次返回未生成。
slog.InfoContext(ctx, "horoscope: today data missing, trigger fallback generation", "zodiac", zodiac)
tokenhub.EnsureTodayFortunes()
return nil, errcode.HoroscopeNotFound
}
slog.ErrorContext(ctx, "horoscope: query db failed", "zodiac", zodiac, "error", derr)
return nil, errcode.DatabaseError
}
@@ -70,7 +81,11 @@ func (s *HoroscopeService) GetToday(ctx context.Context, userID int64) (*dto.Hor
// 回填缓存,TTL 至次日凌晨。
if data, merr := json.Marshal(response); merr == nil {
_ = redisdao.HoroscopeSet(ctx, zodiac, statDate, data, ttlUntilNextMidnight())
if serr := redisdao.HoroscopeSet(ctx, zodiac, statDate, data, ttlUntilNextMidnight()); serr != nil {
slog.DebugContext(ctx, "horoscope: cache backfill failed", "zodiac", zodiac, "error", serr)
}
} else {
slog.DebugContext(ctx, "horoscope: marshal response failed", "zodiac", zodiac, "error", merr)
}
return &response, nil