service.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package job
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/interface/main/web-show/conf"
  6. "go-common/app/interface/main/web-show/dao/job"
  7. jobmdl "go-common/app/interface/main/web-show/model/job"
  8. "go-common/library/log"
  9. )
  10. var (
  11. _emptyJobs = make([]*jobmdl.Job, 0)
  12. )
  13. // Service struct
  14. type Service struct {
  15. dao *job.Dao
  16. cache []*jobmdl.Job
  17. }
  18. // New init
  19. func New(c *conf.Config) (s *Service) {
  20. s = &Service{}
  21. s.dao = job.New(c)
  22. s.cache = _emptyJobs
  23. s.reload()
  24. go s.loadproc()
  25. return
  26. }
  27. // jobproc load job infos to cache
  28. func (s *Service) loadproc() {
  29. for {
  30. s.reload()
  31. time.Sleep(time.Duration(conf.Conf.Reload.Jobs))
  32. }
  33. }
  34. // reload
  35. func (s *Service) reload() {
  36. js, err := s.dao.Jobs(context.Background())
  37. if err != nil {
  38. log.Error("s.job.Jobs error(%v)", err)
  39. return
  40. } else if len(js) == 0 {
  41. s.cache = _emptyJobs
  42. }
  43. cates, err := s.dao.Categories(context.Background())
  44. if err != nil {
  45. log.Error("job.Categories error(%v)", err)
  46. return
  47. }
  48. cs := make(map[int]string, len(cates))
  49. for _, cate := range cates {
  50. cs[cate.ID] = cate.Name
  51. }
  52. for _, j := range js {
  53. j.JobsCla = cs[j.CateID]
  54. j.Location = cs[j.AddrID]
  55. }
  56. s.cache = js
  57. }
  58. // Ping Service
  59. func (s *Service) Ping(c context.Context) (err error) {
  60. err = s.dao.Ping(c)
  61. return
  62. }
  63. // Close Service
  64. func (s *Service) Close() {
  65. s.dao.Close()
  66. }