winnertask.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package main
  2. import (
  3. "log"
  4. qu "qfw/util"
  5. elastic "qfw/util/elastic"
  6. "sync"
  7. "time"
  8. "gopkg.in/mgo.v2/bson"
  9. )
  10. func winnerEsTaskOnce() {
  11. defer qu.Catch()
  12. arrEs := []map[string]interface{}{}
  13. winerEsLock := &sync.Mutex{}
  14. pool := make(chan bool, 3)
  15. wg := &sync.WaitGroup{}
  16. now := time.Now()
  17. preTime := time.Date(now.Year(), now.Month(), now.Day()-1, now.Hour(), 0, 0, 0, time.Local)
  18. curTime := time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), 0, 0, 0, time.Local)
  19. task_sid := qu.BsonIdToSId(bson.NewObjectIdWithTime(preTime))
  20. task_eid := qu.BsonIdToSId(bson.NewObjectIdWithTime(curTime))
  21. // task_sid = "5e6598f82c27dc56292158da"
  22. // task_eid = "5f80c8f89a0c261af872294c"
  23. log.Println("winner 区间id:", task_sid, task_eid)
  24. //区间id
  25. q := map[string]interface{}{
  26. "_id": map[string]interface{}{
  27. "$gte": qu.StringTOBsonId(task_sid),
  28. "$lt": qu.StringTOBsonId(task_eid),
  29. },
  30. }
  31. //参数
  32. winnerent, _ := standard["winnerent"].(map[string]interface{})
  33. win_ent := qu.ObjToString(winnerent["collect1"])
  34. //win_enterr := qu.ObjToString(winnerent["collect2"])
  35. index, _ := winnerent["index"].(string)
  36. itype, _ := winnerent["type"].(string)
  37. //mongo
  38. sess := mgostandard.GetMgoConn()
  39. defer mgostandard.DestoryMongoConn(sess)
  40. log.Println("q:", q, "db:", mgostandard.DbName, "coll:", win_ent)
  41. it_1 := sess.DB(mgostandard.DbName).C(win_ent).Find(&q).Sort("_id").Iter()
  42. num_1 := 0
  43. for tmp := make(map[string]interface{}); it_1.Next(&tmp); num_1++ {
  44. if num_1%100 == 0 && num_1 > 0 {
  45. log.Println("当前表:", win_ent, "数量:", num_1)
  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. savetmp := map[string]interface{}{}
  55. tmp_id := qu.BsonIdToSId(tmp["_id"])
  56. savetmp["_id"] = tmp_id
  57. savetmp["name"] = tmp["company_name"]
  58. savetmp["winner_name"] = tmp["company_name"]
  59. savetmp["pici"] = tmp["updatetime"]
  60. if province := qu.ObjToString(tmp["province"]); province != "" {
  61. savetmp["province"] = province
  62. }
  63. if city := qu.ObjToString(tmp["city"]); city != "" {
  64. savetmp["city"] = city
  65. }
  66. winerEsLock.Lock()
  67. arrEs = append(arrEs, savetmp)
  68. if len(arrEs) >= BulkSize {
  69. tmps := arrEs
  70. elastic.BulkSave(index, itype, &tmps, true)
  71. arrEs = []map[string]interface{}{}
  72. }
  73. winerEsLock.Unlock()
  74. }(tmp)
  75. tmp = make(map[string]interface{})
  76. }
  77. // log.Println("q:", q, "db:", mgostandard.DbName, "coll:", win_enterr)
  78. // it_2 := sess.DB(mgostandard.DbName).C(win_enterr).Find(&q).Sort("_id").Iter()
  79. // num_2 := 0
  80. // for tmp := make(map[string]interface{}); it_2.Next(&tmp); num_2++ {
  81. // if num_2%100 == 0 && num_2 > 0 {
  82. // log.Println("当前表:", win_enterr, "数量:", num_2)
  83. // }
  84. // pool <- true
  85. // wg.Add(1)
  86. // go func(tmp map[string]interface{}) {
  87. // defer func() {
  88. // <-pool
  89. // wg.Done()
  90. // }()
  91. // savetmp := map[string]interface{}{}
  92. // tmp_id := qu.BsonIdToSId(tmp["_id"])
  93. // savetmp["_id"] = tmp_id
  94. // savetmp["name"] = tmp["name"]
  95. // savetmp["winner_name"] = tmp["name"]
  96. // savetmp["pici"] = tmp["updatetime"]
  97. // winerEsLock.Lock()
  98. // arrEs = append(arrEs, savetmp)
  99. // if len(arrEs) >= BulkSize {
  100. // tmps := arrEs
  101. // elastic.BulkSave(index, itype, &tmps, true)
  102. // arrEs = []map[string]interface{}{}
  103. // }
  104. // winerEsLock.Unlock()
  105. // }(tmp)
  106. // tmp = make(map[string]interface{})
  107. // }
  108. wg.Wait()
  109. winerEsLock.Lock()
  110. if len(arrEs) > 0 {
  111. tmps := arrEs
  112. elastic.BulkSave(index, itype, &tmps, true)
  113. arrEs = []map[string]interface{}{}
  114. }
  115. winerEsLock.Unlock()
  116. log.Println("winnerextract 索引完毕! 总计:", num_1)
  117. }