main.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. package main
  2. import (
  3. util "app.yhyue.com/data_processing/common_utils"
  4. "app.yhyue.com/data_processing/common_utils/elastic"
  5. "app.yhyue.com/data_processing/common_utils/log"
  6. "app.yhyue.com/data_processing/common_utils/mongodb"
  7. "app.yhyue.com/data_processing/common_utils/udp"
  8. "encoding/json"
  9. "esindex/config"
  10. "esindex/oss"
  11. "fmt"
  12. "github.com/robfig/cron"
  13. "go.uber.org/zap"
  14. "io/ioutil"
  15. "net"
  16. "net/http"
  17. "sync"
  18. "time"
  19. )
  20. var (
  21. MgoB *mongodb.MongodbSim
  22. MgoP *mongodb.MongodbSim
  23. MgoQ *mongodb.MongodbSim
  24. Es, Es1 *elastic.Elastic
  25. UdpClient udp.UdpClient
  26. UdpTaskMap = &sync.Map{}
  27. JyUdpAddr *net.UDPAddr
  28. EsBulkSize = 100 // es批量保存大小
  29. updateBiddingPool = make(chan []map[string]interface{}, 5000) //更新bingding数据
  30. updateBiddingSp = make(chan bool, 5)
  31. saveEsPool = make(chan map[string]interface{}, 5000) //保存binding数据到es
  32. saveEsSp = make(chan bool, 5)
  33. saveProjectEsPool = make(chan map[string]interface{}, 5000) //保存project数据到es
  34. saveProjectSp = make(chan bool, 5)
  35. saveEsAllPool = make(chan map[string]interface{}, 5000)
  36. saveEsAllSp = make(chan bool, 5)
  37. saveErrBidPool = make(chan map[string]interface{}, 5000)
  38. saveBidSp = make(chan bool, 5)
  39. //detailLength = 50000 // es保存detail长度
  40. fileLength = 50000 // es保存附件文本长度
  41. //pscopeLength = 32766 // projectscope长度
  42. )
  43. func init() {
  44. config.Init("./common.toml")
  45. InitLog()
  46. InitMgo()
  47. InitEs()
  48. InitField()
  49. oss.InitOss()
  50. JyUdpAddr = &net.UDPAddr{
  51. IP: net.ParseIP(config.Conf.Udp.JyAddr),
  52. Port: util.IntAll(config.Conf.Udp.JyPort),
  53. }
  54. log.Info("init success")
  55. }
  56. func main() {
  57. <<<<<<< HEAD
  58. //go checkMapJob()
  59. //go task_index()
  60. //go UpdateBidding()
  61. =======
  62. go checkMapJob() //udp 发送邮件
  63. go task_index() //定时同步更新winner_enterprise、buyer_enterprise ES索引;这个功能很少变动,几乎不需要维护
  64. go UpdateBidding() //更新bidding表数据
  65. >>>>>>> 2c01aa33777c70182cf9990d22a7ec6da6d17457
  66. go SaveEsMethod()
  67. go SaveAllEsMethod()
  68. go SaveProjectEs()
  69. //go SaveBidErr()
  70. UdpClient = udp.UdpClient{Local: config.Conf.Udp.LocPort, BufSize: 1024}
  71. UdpClient.Listen(processUdpMsg)
  72. log.Info("Udp服务监听", zap.String("port:", config.Conf.Udp.LocPort))
  73. ch := make(chan bool, 1)
  74. <-ch
  75. }
  76. var pool = make(chan bool, 20)
  77. func processUdpMsg(act byte, data []byte, ra *net.UDPAddr) {
  78. switch act {
  79. case udp.OP_TYPE_DATA:
  80. var mapInfo map[string]interface{}
  81. err := json.Unmarshal(data, &mapInfo)
  82. log.Info("processUdpMsg", zap.Any("mapInfo:", mapInfo))
  83. if err != nil {
  84. UdpClient.WriteUdp([]byte("err:"+err.Error()), udp.OP_NOOP, ra)
  85. } else if mapInfo != nil {
  86. key, _ := mapInfo["key"].(string)
  87. if key == "" {
  88. key = "udpok"
  89. }
  90. go UdpClient.WriteUdp([]byte(key), udp.OP_NOOP, ra)
  91. tasktype, _ := mapInfo["stype"].(string)
  92. switch tasktype {
  93. case "index-by-id": //单个索引
  94. pool <- true
  95. go func() {
  96. defer func() {
  97. <-pool
  98. }()
  99. biddingTaskById(mapInfo)
  100. }()
  101. case "bidding":
  102. pool <- true
  103. go func() {
  104. defer func() {
  105. <-pool
  106. }()
  107. biddingTask(mapInfo)
  108. }()
  109. case "biddingall":
  110. pool <- true
  111. go func() {
  112. defer func() {
  113. <-pool
  114. }()
  115. biddingAllTask(mapInfo)
  116. }()
  117. case "bidding_all_data": //根据biddingall配置文件,存量迁移数据
  118. pool <- true
  119. go func() {
  120. defer func() {
  121. <-pool
  122. }()
  123. biddingAllDataTask()
  124. }()
  125. case "bidding_history":
  126. pool <- true
  127. go func() {
  128. defer func() {
  129. <-pool
  130. }()
  131. biddingTask(mapInfo)
  132. }()
  133. case "project":
  134. pool <- true
  135. go func() {
  136. defer func() {
  137. <-pool
  138. }()
  139. projectTask(data, mapInfo)
  140. }()
  141. case "biddingdata": //bidding全量数据
  142. pool <- true
  143. go func() {
  144. defer func() {
  145. <-pool
  146. }()
  147. biddingDataTask(data, mapInfo)
  148. }()
  149. case "biddingdelbyextracttype": //根据bidding表extracttype=-1,删除es中重复数据
  150. pool <- true
  151. go func() {
  152. defer func() {
  153. <-pool
  154. }()
  155. biddingDelByExtracttype(data, mapInfo)
  156. }()
  157. default:
  158. pool <- true
  159. go func() {
  160. defer func() {
  161. <-pool
  162. }()
  163. log.Info("err", zap.Any("mapInfo", mapInfo))
  164. }()
  165. }
  166. }
  167. case udp.OP_NOOP: //下个节点回应
  168. ok := string(data)
  169. if ok != "" {
  170. log.Info("udp re", zap.String("data:", ok))
  171. UdpTaskMap.Delete(ok)
  172. }
  173. }
  174. }
  175. func task_index() {
  176. c := cron.New()
  177. _ = c.AddFunc("0 0 0 * * ?", func() { task_winneres() }) //每天凌晨执行一次winner生索引
  178. _ = c.AddFunc("0 0 1 * * ?", func() { task_buyeres() }) //每天1点执行一次buyer生索引
  179. c.Start()
  180. }
  181. func task_winneres() {
  182. log.Info("定时任务,winneres")
  183. winnerEsTaskOnce()
  184. }
  185. func task_buyeres() {
  186. log.Info("定时任务,buyeres")
  187. buyerEsTaskOnce()
  188. }
  189. type UdpNode struct {
  190. data []byte
  191. addr *net.UDPAddr
  192. timestamp int64
  193. retry int
  194. }
  195. //UpdateBidding 更新bidding表数据
  196. func UpdateBidding() {
  197. arru := make([][]map[string]interface{}, 200)
  198. indexu := 0
  199. for {
  200. select {
  201. case v := <-updateBiddingPool:
  202. arru[indexu] = v
  203. indexu++
  204. if indexu == 200 {
  205. updateBiddingSp <- true
  206. go func(arru [][]map[string]interface{}) {
  207. defer func() {
  208. <-updateBiddingSp
  209. }()
  210. MgoB.UpdateBulk(config.Conf.DB.MongoB.Coll, arru...)
  211. }(arru)
  212. arru = make([][]map[string]interface{}, 200)
  213. indexu = 0
  214. }
  215. case <-time.After(1000 * time.Millisecond):
  216. if indexu > 0 {
  217. updateBiddingSp <- true
  218. go func(arru [][]map[string]interface{}) {
  219. defer func() {
  220. <-updateBiddingSp
  221. }()
  222. MgoB.UpdateBulk(config.Conf.DB.MongoB.Coll, arru...)
  223. }(arru[:indexu])
  224. arru = make([][]map[string]interface{}, 200)
  225. indexu = 0
  226. }
  227. }
  228. }
  229. }
  230. func SaveBidErr() {
  231. arru := make([]map[string]interface{}, 200)
  232. indexu := 0
  233. for {
  234. select {
  235. case v := <-saveErrBidPool:
  236. arru[indexu] = v
  237. indexu++
  238. if indexu == 200 {
  239. saveBidSp <- true
  240. go func(arru []map[string]interface{}) {
  241. defer func() {
  242. <-saveBidSp
  243. }()
  244. MgoB.SaveBulk("bidding_es_err_record", arru...)
  245. }(arru)
  246. arru = make([]map[string]interface{}, 200)
  247. indexu = 0
  248. }
  249. case <-time.After(1000 * time.Millisecond):
  250. if indexu > 0 {
  251. saveBidSp <- true
  252. go func(arru []map[string]interface{}) {
  253. defer func() {
  254. <-saveBidSp
  255. }()
  256. MgoB.SaveBulk("bidding_es_err_record", arru...)
  257. }(arru[:indexu])
  258. arru = make([]map[string]interface{}, 200)
  259. indexu = 0
  260. }
  261. }
  262. }
  263. }
  264. //SaveEsMethod 保存到es
  265. func SaveEsMethod() {
  266. arru := make([]map[string]interface{}, EsBulkSize)
  267. indexu := 0
  268. for {
  269. select {
  270. case v := <-saveEsPool:
  271. arru[indexu] = v
  272. indexu++
  273. if indexu == EsBulkSize {
  274. saveEsSp <- true
  275. go func(arru []map[string]interface{}) {
  276. defer func() {
  277. <-saveEsSp
  278. }()
  279. Es.BulkSave(config.Conf.DB.Es.IndexB, arru)
  280. }(arru)
  281. arru = make([]map[string]interface{}, EsBulkSize)
  282. indexu = 0
  283. }
  284. case <-time.After(1000 * time.Millisecond):
  285. if indexu > 0 {
  286. saveEsSp <- true
  287. go func(arru []map[string]interface{}) {
  288. defer func() {
  289. <-saveEsSp
  290. }()
  291. Es.BulkSave(config.Conf.DB.Es.IndexB, arru)
  292. }(arru[:indexu])
  293. arru = make([]map[string]interface{}, EsBulkSize)
  294. indexu = 0
  295. }
  296. }
  297. }
  298. }
  299. func SaveAllEsMethod() {
  300. arru := make([]map[string]interface{}, EsBulkSize)
  301. indexu := 0
  302. for {
  303. select {
  304. case v := <-saveEsAllPool:
  305. arru[indexu] = v
  306. indexu++
  307. if indexu == EsBulkSize {
  308. saveEsAllSp <- true
  309. go func(arru []map[string]interface{}) {
  310. defer func() {
  311. <-saveEsAllSp
  312. }()
  313. Es1.BulkSave("biddingall", arru)
  314. }(arru)
  315. arru = make([]map[string]interface{}, EsBulkSize)
  316. indexu = 0
  317. }
  318. case <-time.After(1000 * time.Millisecond):
  319. if indexu > 0 {
  320. saveEsAllSp <- true
  321. go func(arru []map[string]interface{}) {
  322. defer func() {
  323. <-saveEsAllSp
  324. }()
  325. Es1.BulkSave("biddingall", arru)
  326. }(arru[:indexu])
  327. arru = make([]map[string]interface{}, EsBulkSize)
  328. indexu = 0
  329. }
  330. }
  331. }
  332. }
  333. func SaveProjectEs() {
  334. arru := make([]map[string]interface{}, EsBulkSize)
  335. indexu := 0
  336. for {
  337. select {
  338. case v := <-saveProjectEsPool:
  339. arru[indexu] = v
  340. indexu++
  341. if indexu == EsBulkSize {
  342. saveProjectSp <- true
  343. go func(arru []map[string]interface{}) {
  344. defer func() {
  345. <-saveProjectSp
  346. }()
  347. Es.BulkSave(config.Conf.DB.Es.IndexP, arru)
  348. }(arru)
  349. arru = make([]map[string]interface{}, EsBulkSize)
  350. indexu = 0
  351. }
  352. case <-time.After(1000 * time.Millisecond):
  353. if indexu > 0 {
  354. saveProjectSp <- true
  355. go func(arru []map[string]interface{}) {
  356. defer func() {
  357. <-saveProjectSp
  358. }()
  359. Es.BulkSave(config.Conf.DB.Es.IndexP, arru)
  360. }(arru[:indexu])
  361. arru = make([]map[string]interface{}, EsBulkSize)
  362. indexu = 0
  363. }
  364. }
  365. }
  366. }
  367. func checkMapJob() {
  368. if config.Conf.Mail.Send {
  369. log.Info("checkMapJob", zap.String("to:", config.Conf.Mail.To))
  370. for {
  371. UdpTaskMap.Range(func(k, v interface{}) bool {
  372. now := time.Now().Unix()
  373. node, _ := v.(*UdpNode)
  374. if now-node.timestamp > 120 {
  375. node.retry++
  376. if node.retry > 5 {
  377. UdpTaskMap.Delete(k)
  378. 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)))
  379. if err == nil {
  380. defer res.Body.Close()
  381. read, err := ioutil.ReadAll(res.Body)
  382. log.Info("send mail ...", zap.String("r:", string(read)), zap.Any("err:", err))
  383. }
  384. } else {
  385. log.Info("udp重发", zap.Any("k:", k))
  386. UdpClient.WriteUdp(node.data, udp.OP_TYPE_DATA, node.addr)
  387. }
  388. } else if now-node.timestamp > 10 {
  389. log.Info("udp任务超时中..", zap.Any("k:", k))
  390. }
  391. return true
  392. })
  393. time.Sleep(60 * time.Second)
  394. }
  395. }
  396. }
  397. func task() {
  398. sess := MgoB.GetMgoConn()
  399. defer MgoB.DestoryMongoConn(sess)
  400. ch := make(chan bool, 10)
  401. wg := &sync.WaitGroup{}
  402. query := sess.DB("qfw").C("result_replace_repair_log").Find(nil).Iter()
  403. count := 0
  404. for tmp := make(map[string]interface{}); query.Next(tmp); count++ {
  405. if count%1000 == 0 {
  406. util.Debug("current ---", count)
  407. }
  408. ch <- true
  409. wg.Add(1)
  410. go func(tmp map[string]interface{}) {
  411. defer func() {
  412. <-ch
  413. wg.Done()
  414. }()
  415. if id := util.ObjToString(tmp["replace_id"]); mongodb.IsObjectIdHex(id) {
  416. biddingTaskById(map[string]interface{}{"infoid": id, "stype": "bidding"})
  417. }
  418. }(tmp)
  419. tmp = make(map[string]interface{})
  420. }
  421. wg.Wait()
  422. util.Debug("over ---", count)
  423. }