service.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "go-common/app/job/main/block/conf"
  7. "go-common/app/job/main/block/dao"
  8. "go-common/library/cache"
  9. "go-common/library/log"
  10. "go-common/library/queue/databus"
  11. "github.com/pkg/errors"
  12. )
  13. // Service struct
  14. type Service struct {
  15. dao *dao.Dao
  16. cache *cache.Cache
  17. missch chan func()
  18. creditSub *databus.Databus
  19. accountNotifyPub *databus.Databus
  20. }
  21. // New init
  22. func New() (s *Service) {
  23. s = &Service{
  24. dao: dao.New(),
  25. cache: cache.New(1, 10240),
  26. missch: make(chan func(), 10240),
  27. creditSub: databus.New(conf.Conf.Databus.Credit),
  28. accountNotifyPub: databus.New(conf.Conf.AccountNotify),
  29. }
  30. // 自动解禁检查
  31. if conf.Conf.Property.Flag.ExpireCheck {
  32. go s.limitcheckproc()
  33. go s.creditcheckproc()
  34. }
  35. // 小黑屋答题状态订阅
  36. if conf.Conf.Property.Flag.CreditSub {
  37. go s.creditsubproc()
  38. }
  39. go s.missproc()
  40. return s
  41. }
  42. // Ping Service
  43. func (s *Service) Ping(c context.Context) (err error) {
  44. return s.dao.Ping(c)
  45. }
  46. // Close Service
  47. func (s *Service) Close() {
  48. s.dao.Close()
  49. }
  50. func (s *Service) limitcheckproc() {
  51. defer func() {
  52. if x := recover(); x != nil {
  53. log.Error("service.limitcheckproc panic(%v)", x)
  54. go s.limitcheckproc()
  55. log.Info("service.limitcheckproc recover")
  56. }
  57. }()
  58. for {
  59. log.Info("limit check start")
  60. s.limitExpireHandler(context.TODO())
  61. log.Info("limit check end")
  62. time.Sleep(time.Duration(conf.Conf.Property.LimitExpireCheckTick))
  63. }
  64. }
  65. func (s *Service) creditcheckproc() {
  66. defer func() {
  67. if x := recover(); x != nil {
  68. log.Error("%+v", errors.WithStack(fmt.Errorf("service.creditcheckproc panic(%v)", x)))
  69. go s.creditcheckproc()
  70. log.Info("service.creditcheckproc recover")
  71. }
  72. }()
  73. for {
  74. log.Info("black house check start")
  75. s.creditExpireHandler(context.TODO())
  76. log.Info("black house check end")
  77. time.Sleep(time.Duration(conf.Conf.Property.CreditExpireCheckTick))
  78. }
  79. }
  80. func (s *Service) missproc() {
  81. defer func() {
  82. if x := recover(); x != nil {
  83. log.Error("service.missproc panic(%v)", x)
  84. go s.missproc()
  85. log.Info("service.missproc recover")
  86. }
  87. }()
  88. for {
  89. f := <-s.missch
  90. f()
  91. }
  92. }
  93. func (s *Service) mission(f func()) {
  94. select {
  95. case s.missch <- f:
  96. default:
  97. log.Error("s.missch full")
  98. }
  99. }