setting.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package service
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/interface/main/push-archive/model"
  6. pb "go-common/app/service/main/push/api/grpc/v1"
  7. "go-common/library/log"
  8. )
  9. // Setting gets user's archive-result setting.
  10. func (s *Service) Setting(c context.Context, mid int64) (st *model.Setting, err error) {
  11. st, err = s.dao.Setting(c, mid)
  12. if err != nil {
  13. return
  14. }
  15. if st == nil {
  16. st = &model.Setting{Type: model.PushTypeSpecial} // 如果用户还未上传配置,则默认为特殊关注
  17. }
  18. return
  19. }
  20. // SetSetting saves user's archive-result setting.
  21. func (s *Service) SetSetting(c context.Context, mid int64, st *model.Setting) (err error) {
  22. err = s.dao.SetSetting(c, mid, st)
  23. if err == nil {
  24. set := &pb.SetSettingRequest{
  25. Mid: mid,
  26. Type: 1,
  27. Value: int32(st.Type),
  28. }
  29. s.settingCh <- set
  30. }
  31. return
  32. }
  33. func (s *Service) setSettingProc() (err error) {
  34. defer func() {
  35. if msg := recover(); msg != nil {
  36. log.Error("setSettingProc got panic(%+v)", msg)
  37. }
  38. }()
  39. for {
  40. time.Sleep(time.Millisecond * 200)
  41. set, open := <-s.settingCh
  42. if !open {
  43. log.Error("setSettingProc settingCh is closed")
  44. return
  45. }
  46. // before send rpc, check db value with new value, if diff, then rpc is later than another update
  47. dbSet, err := s.dao.Setting(context.TODO(), set.Mid)
  48. if err != nil {
  49. log.Error("setSettingProc s.dao.Setting error(%v) set(%+v)", err, set)
  50. s.settingCh <- set
  51. continue
  52. }
  53. if dbSet == nil || dbSet.Type != int(set.Value) {
  54. log.Info("setSettingProc push setting value diff, db(%+v) rpc(%+v)", dbSet, set)
  55. continue
  56. }
  57. // rpc中0-关闭,1-开启
  58. var tp int32
  59. if set.Value == model.PushTypeSpecial || set.Value == model.PushTypeAttention {
  60. tp = 1
  61. }
  62. set.Value = tp
  63. if _, err := s.pushRPC.SetSetting(context.TODO(), set); err != nil {
  64. log.Error("s.pushRPC.SetSetting error(%v) set(%+v)", err, set)
  65. s.settingCh <- set
  66. }
  67. }
  68. }