main.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. package main
  2. import (
  3. "encoding/json"
  4. "field_sync/config"
  5. "field_sync/oss"
  6. "fmt"
  7. "io/ioutil"
  8. "net"
  9. "net/http"
  10. "strings"
  11. "sync"
  12. "time"
  13. "log"
  14. elastic "app.yhyue.com/moapp/jybase/es"
  15. "go.uber.org/zap"
  16. "gopkg.in/mgo.v2/bson"
  17. util "jygit.jydev.jianyu360.cn/data_processing/common_utils"
  18. "jygit.jydev.jianyu360.cn/data_processing/common_utils/mongodb"
  19. gonsq "jygit.jydev.jianyu360.cn/data_processing/common_utils/nsq"
  20. "jygit.jydev.jianyu360.cn/data_processing/common_utils/redis"
  21. "jygit.jydev.jianyu360.cn/data_processing/common_utils/udp"
  22. )
  23. var (
  24. MgoB, MgoBP *mongodb.MongodbSim
  25. MgoE *mongodb.MongodbSim
  26. MgoQ *mongodb.MongodbSim // 企业
  27. MgoP *mongodb.MongodbSim // 凭安企业
  28. Es elastic.Es
  29. UdpClient udp.UdpClient
  30. UdpTaskMap = &sync.Map{}
  31. Mcmer *gonsq.Consumer
  32. MgoBulkSize = 200 // mgo批量保存大小
  33. updateBidPool = make(chan []map[string]interface{}, 5000)
  34. updateBidSp = make(chan bool, 5)
  35. updateExtPool = make(chan []map[string]interface{}, 5000)
  36. updateExtSp = make(chan bool, 5)
  37. )
  38. func init() {
  39. config.Init("./common.toml")
  40. oss.InitOss()
  41. InitFileInfo()
  42. // InitLog()
  43. InitMgo()
  44. // InitEs()
  45. inits()
  46. redis.InitRedis1(config.Conf.DB.Redis.Addr, config.Conf.DB.Redis.DbIndex)
  47. log.Println("init success")
  48. }
  49. func main() {
  50. // go checkMapJob()
  51. // go nsqMethod()
  52. // go UpdateBidding()
  53. // go UpdateExtract()
  54. // UdpClient = udp.UdpClient{Local: ":11876", BufSize: 1024}
  55. // UdpClient.Listen(processUdpMsg)
  56. // log.Println("Udp服务监听 port:", ":11876")
  57. // info, _ := MgoBP.Find("bidding_processing_ids", `{"dataprocess_ai": 5}`, bson.M{"_id": 1}, nil, false, -1, -1)
  58. // log.Println(len(*info))
  59. // log.Println("size", len(*info))
  60. // if len(*info) > 0 {
  61. // for _, m := range *info {
  62. // mapInfo := make(map[string]interface{})
  63. // mapInfo["gtid"] = util.ObjToString(m["gtid"])
  64. // mapInfo["lteid"] = util.ObjToString(m["lteid"])
  65. // mapInfo["stype"] = "bidding"
  66. // mapInfo["key"] = fmt.Sprintf("%s-%s-bidding", util.ObjToString(m["gtid"]), util.ObjToString(m["lteid"]))
  67. // log.Println("--", mapInfo)
  68. biddingAllTask()
  69. // }
  70. // }
  71. // ch := make(chan bool, 1)
  72. // <-ch
  73. }
  74. var pool = make(chan bool, 20)
  75. type UdpNode struct {
  76. data []byte
  77. addr *net.UDPAddr
  78. timestamp int64
  79. retry int
  80. }
  81. func NextNode(mapInfo map[string]interface{}, stype string) {
  82. var next = &net.UDPAddr{
  83. IP: net.ParseIP(config.Conf.Udp.Next.Addr),
  84. Port: util.IntAll(config.Conf.Udp.Next.Port),
  85. }
  86. mapInfo["stype"] = stype
  87. key := fmt.Sprintf("%s-%s-%s", util.ObjToString(mapInfo["gtid"]), util.ObjToString(mapInfo["lteid"]), stype)
  88. mapInfo["key"] = key
  89. log.Println("udp es node mapinfo:", mapInfo)
  90. datas, _ := json.Marshal(mapInfo)
  91. node := &UdpNode{datas, next, time.Now().Unix(), 0}
  92. UdpTaskMap.Store(key, node)
  93. _ = UdpClient.WriteUdp(datas, udp.OP_TYPE_DATA, next)
  94. }
  95. func NextNodePro(mapInfo map[string]interface{}, stype string) {
  96. var next = &net.UDPAddr{
  97. IP: net.ParseIP(config.Conf.Udp.Project.Addr),
  98. Port: util.IntAll(config.Conf.Udp.Project.Port),
  99. }
  100. if stype == "bidding_history" {
  101. mapInfo["stype"] = "project_history"
  102. } else {
  103. mapInfo["stype"] = "project"
  104. }
  105. key := fmt.Sprintf("%s-%s-%s", util.ObjToString(mapInfo["gtid"]), util.ObjToString(mapInfo["lteid"]), util.ObjToString(mapInfo["stype"]))
  106. mapInfo["key"] = key
  107. log.Println("udp project node mapinfo:", mapInfo)
  108. datas, _ := json.Marshal(mapInfo)
  109. node := &UdpNode{datas, next, time.Now().Unix(), 0}
  110. UdpTaskMap.Store(key, node)
  111. _ = UdpClient.WriteUdp(datas, udp.OP_TYPE_DATA, next)
  112. }
  113. func NextNodeBidData(mapInfo map[string]interface{}) {
  114. next := &net.UDPAddr{
  115. IP: net.ParseIP(config.Conf.Udp.Next.Addr),
  116. Port: util.IntAll(config.Conf.Udp.Next.Port),
  117. }
  118. mapInfo["stype"] = "biddingdata"
  119. mapInfo["key"] = fmt.Sprintf("%s-%s-%s", util.ObjToString(mapInfo["gtid"]), util.ObjToString(mapInfo["lteid"]), util.ObjToString(mapInfo["stype"]))
  120. log.Println("udp es node mapinfo:", mapInfo)
  121. datas, _ := json.Marshal(mapInfo)
  122. _ = UdpClient.WriteUdp(datas, udp.OP_TYPE_DATA, next)
  123. }
  124. func NextNodeTidbQyxy(mapInfo map[string]interface{}) {
  125. next := &net.UDPAddr{
  126. IP: net.ParseIP(config.Conf.Udp.Tidb.Addr),
  127. Port: util.IntAll(config.Conf.Udp.Tidb.Port),
  128. }
  129. mapInfo["stype"] = config.Conf.Udp.Tidb.Stype
  130. mapInfo["key"] = fmt.Sprintf("%s-%s-%s", util.ObjToString(mapInfo["gtid"]), util.ObjToString(mapInfo["lteid"]), util.ObjToString(mapInfo["stype"]))
  131. log.Println("udp tidb-qyxy node mapinfo:", mapInfo)
  132. datas, _ := json.Marshal(mapInfo)
  133. node := &UdpNode{datas, next, time.Now().Unix(), 0}
  134. UdpTaskMap.Store(mapInfo["key"], node)
  135. _ = UdpClient.WriteUdp(datas, udp.OP_TYPE_DATA, next)
  136. }
  137. func NextNodeTidb(mapInfo map[string]interface{}, stype string) {
  138. next := &net.UDPAddr{
  139. IP: net.ParseIP(config.Conf.Udp.Tidb1.Addr),
  140. Port: util.IntAll(config.Conf.Udp.Tidb1.Port),
  141. }
  142. mapInfo["stype"] = stype
  143. mapInfo["key"] = fmt.Sprintf("%s-%s-%s", util.ObjToString(mapInfo["gtid"]), util.ObjToString(mapInfo["lteid"]), util.ObjToString(mapInfo["stype"]))
  144. log.Println("udp tidb-bidding node mapinfo:", mapInfo)
  145. datas, _ := json.Marshal(mapInfo)
  146. node := &UdpNode{datas, next, time.Now().Unix(), 0}
  147. UdpTaskMap.Store(mapInfo["key"], node)
  148. _ = UdpClient.WriteUdp(datas, udp.OP_TYPE_DATA, next)
  149. }
  150. // NextNodeHn @Description 郑坤 海南数据处理
  151. // @Author J 2022/10/28 09:26
  152. func NextNodeHn(mapInfo map[string]interface{}) {
  153. next := &net.UDPAddr{
  154. IP: net.ParseIP(config.Conf.Udp.Tidb2.Addr),
  155. Port: util.IntAll(config.Conf.Udp.Tidb2.Port),
  156. }
  157. mapInfo["stype"] = "hainan"
  158. mapInfo["key"] = fmt.Sprintf("%s-%s-%s", util.ObjToString(mapInfo["gtid"]), util.ObjToString(mapInfo["lteid"]), util.ObjToString(mapInfo["stype"]))
  159. log.Println("NextNodeTidb mapinfo:", mapInfo)
  160. datas, _ := json.Marshal(mapInfo)
  161. node := &UdpNode{datas, next, time.Now().Unix(), 0}
  162. UdpTaskMap.Store(mapInfo["key"], node)
  163. _ = UdpClient.WriteUdp(datas, udp.OP_TYPE_DATA, next)
  164. }
  165. func UpdateBidding() {
  166. arru := make([][]map[string]interface{}, MgoBulkSize)
  167. indexu := 0
  168. for {
  169. select {
  170. case v := <-updateBidPool:
  171. arru[indexu] = v
  172. indexu++
  173. if indexu == MgoBulkSize {
  174. // updateBidSp <- true
  175. // go func(arru [][]map[string]interface{}) {
  176. // defer func() {
  177. // }()
  178. MgoB.UpdateBulk(config.Conf.DB.MongoB.Coll, arru...)
  179. // <-updateBidSp
  180. // }(arru)
  181. arru = make([][]map[string]interface{}, MgoBulkSize)
  182. indexu = 0
  183. }
  184. case <-time.After(1000 * time.Millisecond):
  185. if indexu > 0 {
  186. // updateBidSp <- true
  187. // go func(arru [][]map[string]interface{}) {
  188. // defer func() {
  189. // <-updateBidSp
  190. // }()
  191. MgoB.UpdateBulk(config.Conf.DB.MongoB.Coll, arru[:indexu]...)
  192. // }(arru[:indexu])
  193. arru = make([][]map[string]interface{}, MgoBulkSize)
  194. indexu = 0
  195. }
  196. }
  197. }
  198. }
  199. func UpdateExtract() {
  200. arru := make([][]map[string]interface{}, MgoBulkSize)
  201. indexu := 0
  202. for {
  203. select {
  204. case v := <-updateExtPool:
  205. arru[indexu] = v
  206. indexu++
  207. if indexu == MgoBulkSize {
  208. updateExtSp <- true
  209. go func(arru [][]map[string]interface{}) {
  210. defer func() {
  211. <-updateExtSp
  212. }()
  213. MgoE.UpdateBulk(config.Conf.DB.MongoE.Coll, arru...)
  214. }(arru)
  215. arru = make([][]map[string]interface{}, MgoBulkSize)
  216. indexu = 0
  217. }
  218. case <-time.After(1000 * time.Millisecond):
  219. if indexu > 0 {
  220. updateExtSp <- true
  221. go func(arru [][]map[string]interface{}) {
  222. defer func() {
  223. <-updateExtSp
  224. }()
  225. MgoE.UpdateBulk(config.Conf.DB.MongoE.Coll, arru...)
  226. }(arru[:indexu])
  227. arru = make([][]map[string]interface{}, MgoBulkSize)
  228. indexu = 0
  229. }
  230. }
  231. }
  232. }
  233. func checkMapJob() {
  234. if config.Conf.Mail.Send {
  235. log.Println("checkMapJob to:", config.Conf.Mail.To)
  236. for {
  237. UdpTaskMap.Range(func(k, v interface{}) bool {
  238. now := time.Now().Unix()
  239. node, _ := v.(*UdpNode)
  240. if now-node.timestamp > 120 {
  241. node.retry++
  242. if node.retry > 5 {
  243. UdpTaskMap.Delete(k)
  244. res, err := http.Get(fmt.Sprintf("%s?to=%s&title=%s&body=%s", config.Conf.Mail.Api, config.Conf.Mail.To, "field-sync-send-fail", k.(string)))
  245. if err == nil {
  246. defer res.Body.Close()
  247. read, err := ioutil.ReadAll(res.Body)
  248. log.Println("send mail ... r:", string(read), "err:", err)
  249. }
  250. } else {
  251. log.Println("udp重发", zap.Any("k:", k))
  252. //UdpClient.WriteUdp(node.data, udp.OP_TYPE_DATA, node.addr)
  253. }
  254. } else if now-node.timestamp > 10 {
  255. log.Println("udp任务超时中.. k:", k)
  256. }
  257. return true
  258. })
  259. time.Sleep(60 * time.Second)
  260. }
  261. }
  262. }
  263. // @Description nsq处理id不变,内容替换的竞品数据
  264. // @Author J 2022/8/10 11:40
  265. func nsqMethod() {
  266. var err error
  267. Mcmer, err = gonsq.NewConsumer(&gonsq.Cconfig{
  268. IsJsonEncode: true, //与生产者配置对应,设为true会取第1个字节进行类型判断
  269. Addr: config.Conf.Nsq.Addr,
  270. ConnectType: 0, //默认连接nsqd
  271. Topic: config.Conf.Nsq.Topic,
  272. Channel: config.Conf.Nsq.Channel,
  273. Concurrent: config.Conf.Nsq.Concurrent, //并发数
  274. })
  275. if err != nil {
  276. log.Println("nsqMethod err", err)
  277. }
  278. for {
  279. select {
  280. case obj := <-Mcmer.Ch: //从通道读取即可
  281. objstr := util.ObjToString(obj)
  282. log.Println("obj ", obj, objstr)
  283. id := strings.Split(objstr, "=")
  284. if len(id) > 1 {
  285. if bson.IsObjectIdHex(id[1]) {
  286. taskinfo(id[1])
  287. } else {
  288. log.Println("jy nsq id err id", objstr)
  289. }
  290. } else {
  291. log.Println("jy nsq id err id", objstr)
  292. }
  293. }
  294. }
  295. }