main.go 11 KB

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