sea_con.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package pgc
  2. import (
  3. "encoding/json"
  4. "os"
  5. "time"
  6. model "go-common/app/job/main/tv/model/pgc"
  7. "go-common/library/log"
  8. )
  9. const (
  10. //ContLimit is used for getting ugc value 50 records every time
  11. _ContLimit = 50
  12. )
  13. func (s *Service) seaPgcContproc() {
  14. for {
  15. if s.daoClosed {
  16. log.Info("seaPgcContproc DB closed!")
  17. return
  18. }
  19. s.seaPgcCont()
  20. time.Sleep(time.Duration(s.c.Search.Cfg.UploadFre))
  21. }
  22. }
  23. // seaPgcCont is used for generate search content content
  24. func (s *Service) seaPgcCont() {
  25. var (
  26. err error
  27. seasons []*model.SearPgcCon
  28. str []byte // the json string to write in file
  29. cnt int
  30. cycle int //cycle count and every cycle is 50 records
  31. id int
  32. )
  33. if cnt, err = s.dao.PgcContCount(ctx); err != nil {
  34. log.Error(errFormat, "searchCont", "OnlineSeasonsC", err)
  35. return
  36. }
  37. cycle = cnt / _ContLimit
  38. if cnt%_ContLimit != 0 {
  39. cycle = cnt/_ContLimit + 1
  40. }
  41. // write into the file
  42. file, error := os.OpenFile(s.c.Search.PgcContPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0766)
  43. if error != nil {
  44. log.Error(errFormat, "searchSug", "OpenFile", err)
  45. return
  46. }
  47. //cycle get sql value
  48. for i := 0; i < cycle; i++ {
  49. if i == 0 {
  50. id = 0
  51. } else {
  52. id = seasons[len(seasons)-1].ID
  53. }
  54. if seasons, err = s.dao.PgcCont(ctx, id, _ContLimit); err != nil {
  55. log.Error(errFormat, "PgcCont", "PgcCont", err)
  56. return
  57. }
  58. for _, v := range seasons {
  59. if str, err = json.Marshal(v); err != nil {
  60. log.Error(errFormat, "searchSug", "JsonMarshal", err)
  61. return
  62. }
  63. file.WriteString(string(str) + "\n")
  64. }
  65. }
  66. file.Close()
  67. //calculate file's md5
  68. if err = s.ftpDao.FileMd5(s.c.Search.PgcContPath, s.c.Search.PgcContMd5Path); err != nil {
  69. log.Error(errFormat, "searPgcCont", "fileMd5", err)
  70. return
  71. }
  72. // upload original file
  73. if err = s.ftpDao.UploadFile(s.c.Search.PgcContPath, s.c.Search.FTP.RemotePgcCont, s.c.Search.FTP.RemotePgcURL); err != nil {
  74. log.Error(errFormat, "searPgcCont-File", "uploadFile", err)
  75. return
  76. }
  77. // upload md5 file
  78. if err = s.ftpDao.UploadFile(s.c.Search.PgcContMd5Path, s.c.Search.FTP.RemotePgcContMd5, s.c.Search.FTP.RemotePgcURL); err != nil {
  79. log.Error(errFormat, "searPgcCont-Md5", "uploadFile", err)
  80. return
  81. }
  82. log.Info("FTP Upload Success")
  83. }