task.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. package main
  2. import (
  3. "github.com/cron"
  4. "go.mongodb.org/mongo-driver/bson"
  5. "log"
  6. "qfw/util"
  7. "reflect"
  8. "sync"
  9. "time"
  10. )
  11. //定时任务
  12. func TimeTask() {
  13. c := cron.New()
  14. cronstr := "0 0 15 ? * Tue" //每周二15点执行
  15. //cronstr := "0 */" + fmt.Sprint(TaskTime) + " * * * ?" //每TaskTime小时执行一次
  16. err := c.AddFunc(cronstr, func() { SaveAdd() })
  17. if err != nil {
  18. util.Debug(err)
  19. return
  20. }
  21. c.Start()
  22. }
  23. // SaveAdd 增量数据
  24. func SaveAdd() {
  25. defer util.Catch()
  26. sess := Mgo.GetMgoConn()
  27. defer Mgo.DestoryMongoConn(sess)
  28. pool := make(chan bool, 5)
  29. wg := &sync.WaitGroup{}
  30. //q := bson.M{"_id": "affe29f8d061f3faa4170cafba41f316"}
  31. q := bson.M{"updatetime": bson.M{"$gt": Updatetime}}
  32. util.Debug(q)
  33. it := sess.DB(Dbname).C(Dbcoll).Find(q).Iter()
  34. count := 0
  35. for tmp := make(map[string]interface{}); it.Next(&tmp); count++ {
  36. if count%5000 == 0 {
  37. log.Println("current:", count)
  38. }
  39. pool <- true
  40. wg.Add(1)
  41. go func(tmp map[string]interface{}) {
  42. defer func() {
  43. <-pool
  44. wg.Done()
  45. }()
  46. esMap := map[string]interface{}{}
  47. //生索引字段处理
  48. for _, field := range EsFields {
  49. if tmp[field] == nil {
  50. continue
  51. }
  52. }
  53. EsSaveCache <- esMap // 保存es
  54. }(tmp)
  55. tmp = make(map[string]interface{})
  56. }
  57. wg.Wait()
  58. log.Println("Run Over...Count:", count)
  59. }
  60. // SaveAll 存量数据生es
  61. func SaveAll() {
  62. defer util.Catch()
  63. sess := Mgo.GetMgoConn()
  64. defer Mgo.DestoryMongoConn(sess)
  65. pool := make(chan bool, 10)
  66. wg := &sync.WaitGroup{}
  67. //q := bson.M{"_id": mongodb.StringTOBsonId("6194a3c105180be8dae19cbb")}
  68. it := sess.DB(Dbname).C(Dbcoll).Find(nil).Iter()
  69. count := 0
  70. for tmp := make(map[string]interface{}); it.Next(&tmp); count++ {
  71. if count%20000 == 0 {
  72. log.Println("current:", count, tmp["_id"])
  73. }
  74. pool <- true
  75. wg.Add(1)
  76. go func(tmp map[string]interface{}) {
  77. defer func() {
  78. <-pool
  79. wg.Done()
  80. }()
  81. esMap := map[string]interface{}{}
  82. //生索引字段处理
  83. for _, field := range EsFields {
  84. if tmp[field] == nil {
  85. continue
  86. }
  87. if field == "buyerclass" {
  88. if reflect.TypeOf(tmp["buyerclass"]).String() == "[]interface {}" {
  89. esMap["buyerclass"] = util.ObjArrToStringArr(tmp["buyerclass"].([]interface{}))[0]
  90. }else {
  91. esMap["buyerclass"] = tmp["buyerclass"]
  92. }
  93. }else {
  94. esMap[field] = tmp[field]
  95. }
  96. }
  97. // 处理result
  98. if mp, ok := tmp["results"].([]interface{}); ok {
  99. var mpArr []map[string]interface{}
  100. for _, v := range mp{
  101. v1 := v.(map[string]interface{})
  102. if v1["purchasing"] != nil {
  103. mpArr = append(mpArr, map[string]interface{}{"purchasing": v1["purchasing"]})
  104. }
  105. }
  106. if len(mpArr) > 0 {
  107. esMap["results"] = mpArr
  108. }
  109. }
  110. EsSaveCache <- esMap
  111. }(tmp)
  112. tmp = make(map[string]interface{})
  113. }
  114. wg.Wait()
  115. log.Println("Run Over...Count:", count)
  116. }
  117. // SaveEs 过滤后数据存库
  118. func SaveEs() {
  119. log.Println("Es Save...")
  120. arru := make([]map[string]interface{}, 100)
  121. indexu := 0
  122. for {
  123. select {
  124. case v := <-EsSaveCache:
  125. arru[indexu] = v
  126. indexu++
  127. if indexu == 100 {
  128. SP <- true
  129. go func(arru []map[string]interface{}) {
  130. defer func() {
  131. <-SP
  132. }()
  133. Es.BulkSave(Index, Itype, &arru, true)
  134. }(arru)
  135. arru = make([]map[string]interface{}, 100)
  136. indexu = 0
  137. }
  138. case <-time.After(1000 * time.Millisecond):
  139. if indexu > 0 {
  140. SP <- true
  141. go func(arru []map[string]interface{}) {
  142. defer func() {
  143. <-SP
  144. }()
  145. Es.BulkSave(Index, Itype, &arru, true)
  146. }(arru[:indexu])
  147. arru = make([]map[string]interface{}, 100)
  148. indexu = 0
  149. }
  150. }
  151. }
  152. }