main.go 10 KB

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