task.go 4.3 KB

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