Skip to content

Commit

Permalink
feat: update watcher
Browse files Browse the repository at this point in the history
  • Loading branch information
BrooksYang committed Mar 23, 2024
1 parent 3ff7485 commit b19a6b7
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
2 changes: 1 addition & 1 deletion internal/watcher/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
7 changes: 1 addition & 6 deletions internal/watcher/watcher/all/all.go
Original file line number Diff line number Diff line change
@@ -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{})
}
15 changes: 10 additions & 5 deletions internal/watcher/watcher/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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.
Expand Down

0 comments on commit b19a6b7

Please sign in to comment.