limit.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package service
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/job/main/block/conf"
  6. "go-common/app/job/main/block/model"
  7. xsql "go-common/library/database/sql"
  8. "go-common/library/log"
  9. "github.com/pkg/errors"
  10. )
  11. func (s *Service) limitExpireHandler(c context.Context) {
  12. if conf.Conf.Property.LimitExpireCheckLimit <= 0 {
  13. log.Error("conf.Conf.Property.LimitExpireCheckLimit [%d] <= 0", conf.Conf.Property.LimitExpireCheckLimit)
  14. return
  15. }
  16. var (
  17. mids = make([]int64, conf.Conf.Property.LimitExpireCheckLimit)
  18. startID int64
  19. err error
  20. )
  21. for len(mids) >= conf.Conf.Property.LimitExpireCheckLimit {
  22. log.Info("limit expire handle startID (%d)", startID)
  23. if startID, mids, err = s.dao.UserStatusList(c, model.BlockStatusLimit, startID, conf.Conf.Property.LimitExpireCheckLimit); err != nil {
  24. log.Error("%+v", err)
  25. return
  26. }
  27. for _, mid := range mids {
  28. var ok bool
  29. if ok, err = s.limitExpireCheck(c, mid); err != nil {
  30. log.Error("%+v", err)
  31. continue
  32. }
  33. if ok {
  34. s.limitExpireRemove(c, mid)
  35. }
  36. time.Sleep(time.Millisecond * 100)
  37. }
  38. }
  39. }
  40. func (s *Service) limitExpireCheck(c context.Context, mid int64) (ok bool, err error) {
  41. var (
  42. his *model.DBHistory
  43. )
  44. if his, err = s.dao.UserLastHistory(c, mid); err != nil {
  45. return
  46. }
  47. if his == nil {
  48. return
  49. }
  50. if his.Action != model.BlockActionLimit {
  51. return
  52. }
  53. if his.StartTime.Add(time.Duration(his.Duration) * time.Second).After(time.Now()) {
  54. return
  55. }
  56. ok = true
  57. return
  58. }
  59. func (s *Service) limitExpireRemove(c context.Context, mid int64) (err error) {
  60. var (
  61. _reason, _comment = "系统自动解封", "系统自动解封"
  62. stime = time.Now()
  63. db = &model.DBHistory{
  64. MID: mid,
  65. AdminID: model.BlockJOBManagerID,
  66. AdminName: model.BlockJOBManagerName,
  67. Source: model.BlockSourceRemove,
  68. Area: model.BlockAreaNone,
  69. Reason: _reason,
  70. Comment: _comment,
  71. Action: model.BlockActionSelfRemove,
  72. StartTime: stime,
  73. Duration: 0,
  74. Notify: false,
  75. }
  76. tx *xsql.Tx
  77. )
  78. if tx, err = s.dao.BeginTX(c); err != nil {
  79. return
  80. }
  81. if err = s.dao.TxInsertHistory(c, tx, db); err != nil {
  82. tx.Rollback()
  83. return
  84. }
  85. if err = s.dao.TxUpsertUser(c, tx, mid, model.BlockStatusFalse); err != nil {
  86. tx.Rollback()
  87. return
  88. }
  89. if err = tx.Commit(); err != nil {
  90. err = errors.WithStack(err)
  91. }
  92. s.mission(func() {
  93. if err := s.notifyRemoveMSG(context.TODO(), []int64{mid}); err != nil {
  94. log.Error("%+v", err)
  95. }
  96. })
  97. s.mission(func() {
  98. s.AddAuditLog(context.TODO(), model.BlockActionSelfRemove, model.BlockJOBManagerID, model.BlockJOBManagerName, []int64{mid}, 0, model.BlockSourceRemove, model.BlockAreaNone, _reason, _comment, false, stime)
  99. })
  100. s.cache.Save(func() {
  101. if err := s.dao.DeleteUserCache(context.TODO(), mid); err != nil {
  102. log.Error("%+v", err)
  103. }
  104. if databusErr := s.accountNotify(context.TODO(), mid); databusErr != nil {
  105. log.Error("%+v", databusErr)
  106. }
  107. })
  108. return
  109. }