sea_con.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package ugc
  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. errFormat = "Func:[%s] - Step:[%s] - Error:[%v]"
  11. //ContLimit is used for getting pgc season value 50 records every time
  12. _ContLimit = 50
  13. )
  14. func (s *Service) seaUgcContproc() {
  15. for {
  16. if s.daoClosed {
  17. log.Info("seaUgcContproc DB closed!")
  18. return
  19. }
  20. s.seaUgcCont()
  21. time.Sleep(time.Duration(s.c.Search.Cfg.UploadFre))
  22. }
  23. }
  24. // seaUgcCont is used for generate search content content
  25. func (s *Service) seaUgcCont() {
  26. var (
  27. archives []*model.SearUgcCon
  28. str []byte // the json string to write in file
  29. cycle int //cycle count and every cycle is 50 records
  30. maxID = 0
  31. cnt int
  32. file *os.File
  33. err error
  34. )
  35. if cnt, err = s.dao.UgcCnt(ctx); err != nil {
  36. log.Error(errFormat, "searUgcCont", "UgcCnt", err)
  37. return
  38. }
  39. cycle = cnt / _ContLimit
  40. if cnt%_ContLimit != 0 {
  41. cycle = cnt/_ContLimit + 1
  42. }
  43. // write into the file
  44. if file, err = os.OpenFile(s.c.Search.UgcContPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0766); err != nil {
  45. log.Error(errFormat, "searUgcCont", "OpenFile", err)
  46. return
  47. }
  48. //cycle get sql value
  49. for i := 0; i < cycle; i++ {
  50. if archives, maxID, err = s.dao.UgcCont(ctx, maxID, _ContLimit); err != nil {
  51. log.Error(errFormat, "searUgcCont", "UgcCont", err)
  52. return
  53. }
  54. for _, v := range archives {
  55. v.Typeid = s.getPType(v.Typeid)
  56. if str, err = json.Marshal(v); err != nil {
  57. log.Error(errFormat, "searUgcCont", "JsonMarshal", err)
  58. return
  59. }
  60. file.WriteString(string(str) + "\n")
  61. }
  62. }
  63. file.Close()
  64. //calculate file's md5
  65. if err = s.ftpDao.FileMd5(s.c.Search.UgcContPath, s.c.Search.UgcContMd5Path); err != nil {
  66. log.Error(errFormat, "searUgcCont", "fileMd5", err)
  67. return
  68. }
  69. // upload original file
  70. if err = s.ftpDao.UploadFile(s.c.Search.UgcContPath, s.c.Search.FTP.RemoteUgcCont, s.c.Search.FTP.RemoteUgcURL); err != nil {
  71. log.Error(errFormat, "searUgcCont-File", "uploadFile", err)
  72. return
  73. }
  74. // upload md5 file
  75. if err = s.ftpDao.UploadFile(s.c.Search.UgcContMd5Path, s.c.Search.FTP.RemoteUgcContMd5, s.c.Search.FTP.RemoteUgcURL); err != nil {
  76. log.Error(errFormat, "searUgcCont-Md5", "uploadFile", err)
  77. return
  78. }
  79. log.Info("FTP Upload Success")
  80. }