main.go 8.3 KB

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