From b19a6b73eb32187e45300355e20dd83b18fd3dcd Mon Sep 17 00:00:00 2001 From: Brooks Date: Sat, 23 Mar 2024 21:17:17 +0800 Subject: [PATCH] feat: update watcher --- internal/watcher/watcher.go | 2 +- internal/watcher/watcher/all/all.go | 7 +------ internal/watcher/watcher/user/user.go | 15 ++++++++++----- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/internal/watcher/watcher.go b/internal/watcher/watcher.go index 3931cb6..64111b2 100644 --- a/internal/watcher/watcher.go +++ b/internal/watcher/watcher.go @@ -48,7 +48,7 @@ func (w *watchJob) addWatchers() *watchJob { for name, watch := range watcher.ListWatchers() { // log with `{"watcher": "counter"}` key-value to distinguish which watcher the log comes from. // nolint: golint,staticcheck - ctx := context.WithValue(context.Background(), "watcher", name) + ctx := context.WithValue(context.Background(), log.KeyWatcher, name) if err := watch.Init(ctx, w.rs.NewMutex(name, redsync.WithExpiry(2*time.Hour)), nil); err != nil { log.Fatalw("construct watcher %s failed: %s", name, err.Error()) diff --git a/internal/watcher/watcher/all/all.go b/internal/watcher/watcher/all/all.go index 42a8558..798df71 100644 --- a/internal/watcher/watcher/all/all.go +++ b/internal/watcher/watcher/all/all.go @@ -1,10 +1,5 @@ package all -import ( - "bingo/internal/watcher/watcher" - "bingo/internal/watcher/watcher/user" -) - func init() { - watcher.Register("user", &user.UserWatcher{}) + // watcher.Register("user", &user.UserWatcher{}) } diff --git a/internal/watcher/watcher/user/user.go b/internal/watcher/watcher/user/user.go index c8abe0c..6e33ca6 100644 --- a/internal/watcher/watcher/user/user.go +++ b/internal/watcher/watcher/user/user.go @@ -2,10 +2,11 @@ package user import ( "context" - - "github.com/go-redsync/redsync/v4" + "errors" "github.com/bingo-project/component-base/log" + "github.com/go-redsync/redsync/v4" + "gorm.io/gorm" "bingo/internal/apiserver/store" ) @@ -31,13 +32,17 @@ func (w *UserWatcher) Run() { }() user, err := store.S.Users().Get(w.ctx, "test") - if err != nil { - log.Errorw(err.Error()) + if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { + log.C(w.ctx).Errorw(err.Error()) return } - log.Infow(user.Email) + if errors.Is(err, gorm.ErrRecordNotFound) { + log.C(w.ctx).Debug("not found") + } + + log.C(w.ctx).Infow(user.Email) } // Spec is parsed using the time zone of clean Cron instance as the default.