main.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  1. package main
  2. import (
  3. "encoding/json"
  4. "esindex/config"
  5. "esindex/oss"
  6. "fmt"
  7. "github.com/robfig/cron"
  8. "go.uber.org/zap"
  9. "io/ioutil"
  10. util "jygit.jydev.jianyu360.cn/data_processing/common_utils"
  11. "jygit.jydev.jianyu360.cn/data_processing/common_utils/elastic"
  12. "jygit.jydev.jianyu360.cn/data_processing/common_utils/log"
  13. "jygit.jydev.jianyu360.cn/data_processing/common_utils/mongodb"
  14. "jygit.jydev.jianyu360.cn/data_processing/common_utils/mysqldb"
  15. "jygit.jydev.jianyu360.cn/data_processing/common_utils/udp"
  16. "net"
  17. "net/http"
  18. "os"
  19. "os/signal"
  20. "sync"
  21. "syscall"
  22. "time"
  23. )
  24. var (
  25. MgoB *mongodb.MongodbSim
  26. MgoBOld *mongodb.MongodbSim
  27. MgoP *mongodb.MongodbSim
  28. MgoQ *mongodb.MongodbSim
  29. MgoS *mongodb.MongodbSim
  30. Mysql *mysqldb.Mysql
  31. Es, Es2, Es3 *elastic.Elastic //Es3 迁移华为云新集群地址
  32. //PreEs = make(map[string]*elastic.Elastic, 0) //预处理 索引客户端
  33. UdpClient udp.UdpClient
  34. UdpTaskMap = &sync.Map{}
  35. JyUdpAddr *net.UDPAddr
  36. NeUdpAddr *net.UDPAddr
  37. EsBulkSize = 50 // es批量保存大小
  38. updateBiddingPool = make(chan map[string]interface{}, 5000) //更新bingding数据
  39. updateBiddingSp = make(chan bool, 5)
  40. saveEsPool = make(chan map[string]interface{}, 5000) //保存binding数据到es
  41. saveEsSp = make(chan bool, 5)
  42. saveDetailEsSp = make(chan bool, 5)
  43. saveDetailEsPool = make(chan map[string]interface{}, 5000) //保存binding detail,contenthtml 二个字段数据到es
  44. saveProjectEsPool = make(chan map[string]interface{}, 5000) //保存project数据到es
  45. saveProjectSp = make(chan bool, 5)
  46. saveProjectDetailEsPool = make(chan map[string]interface{}, 5000) //保存project detail 数据到es
  47. saveProjectDetailSp = make(chan bool, 5)
  48. saveEsAllPool = make(chan map[string]interface{}, 5000) //存储单机版es,爬虫采集判重使用
  49. saveEsAllSp = make(chan bool, 5)
  50. saveBiddingAllPool = make(chan map[string]interface{}, 5000) //保存binding数据到es,stype=bidding_all_data
  51. saveBiddingAllBEsSp = make(chan bool, 5)
  52. saveErrBidPool = make(chan map[string]interface{}, 5000)
  53. saveBidSp = make(chan bool, 5)
  54. //detailLength = 50000 // es保存detail长度
  55. fileLength = 200000 // es保存附件文本长度,大于20万 时做一个日志记录
  56. //pscopeLength = 32766 // projectscope长度
  57. specialNames = make(map[string]bool, 0) //存储181 凭安提供的特企,爬虫采购单位
  58. responselock sync.Mutex
  59. BiddingLastNodeResponse int64 //上次节点接受数据时间
  60. ProjectLastNodeResponse int64 //上次节点接受数据时间
  61. )
  62. func init() {
  63. config.InitConf("./common.toml")
  64. InitLog()
  65. InitMysql()
  66. InitMgo()
  67. InitEs()
  68. InitField() //初始化项目数据升索引字段
  69. InitBitmap() //初始化项目名称副标题 bitmap
  70. InitRule() //初始化中国移动定制标签规则
  71. InitEsBiddingField() //初始bidding数据升索引字段
  72. oss.InitOss() // 初始化oss 存储
  73. verifyESFields() //检测es 字段类型
  74. JyUdpAddr = &net.UDPAddr{
  75. IP: net.ParseIP(config.Conf.Udp.JyAddr),
  76. Port: util.IntAll(config.Conf.Udp.JyPort),
  77. }
  78. if config.Conf.Udp.NeAddr != "" {
  79. NeUdpAddr = &net.UDPAddr{
  80. IP: net.ParseIP(config.Conf.Udp.NeAddr),
  81. Port: util.IntAll(config.Conf.Udp.NePort),
  82. }
  83. }
  84. BiddingLastNodeResponse = time.Now().Unix()
  85. ProjectLastNodeResponse = time.Now().Unix()
  86. log.Info("init success")
  87. }
  88. func main() {
  89. //正式环境才执行定时任务
  90. if config.Conf.Env.Stype == 0 {
  91. go LastUdpJob() //监听半小时内有无数据
  92. go checkMapJob() //udp 发送邮件
  93. go task_index() //定时同步更新winner_enterprise、buyer_enterprise ES索引;这个功能很少变动,几乎不需要维护
  94. }
  95. //go SaveDetailEsMethod() //保存 bidding_detail 索引
  96. go BatchSaveBiddingDetailEsMethod() //保存 bidding_detail 索引
  97. go UpdateBidding() //更新bidding表数据
  98. go SaveEsMethod() //保存es bidding数据
  99. //go SaveBiddingEsMethod() //保存es bidding数据
  100. go SaveAllEsMethod() // 保存爬虫采集临时数据
  101. go SaveProjectEs() //保存项目索引数据
  102. go SaveProjectDetailEs() //保存项目索引,添加了详情字段的新索引
  103. go SaveBiddingAllDataEs() //保存stype=bidding_all_data 数据
  104. go SaveBidErr()
  105. //添加预处理函数
  106. //if config.Conf.Env.OpenPre {
  107. // go SavePreEsMethod()
  108. // go dealPreProcess()
  109. //}
  110. UdpClient = udp.UdpClient{Local: config.Conf.Udp.LocPort, BufSize: 1024}
  111. UdpClient.Listen(processUdpMsg)
  112. log.Info("Udp服务监听", zap.String("port:", config.Conf.Udp.LocPort))
  113. //监听异常退出信号;及时保存项目名称副标题数据
  114. signalChan := make(chan os.Signal, 1)
  115. signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
  116. <-signalChan
  117. saveDb()
  118. //ch := make(chan bool, 1)
  119. //<-ch
  120. }
  121. var pool = make(chan bool, 20)
  122. func processUdpMsg(act byte, data []byte, ra *net.UDPAddr) {
  123. switch act {
  124. case udp.OP_TYPE_DATA:
  125. var mapInfo map[string]interface{}
  126. err := json.Unmarshal(data, &mapInfo)
  127. log.Info("processUdpMsg", zap.Any("mapInfo:", mapInfo))
  128. if err != nil {
  129. UdpClient.WriteUdp([]byte("err:"+err.Error()), udp.OP_NOOP, ra)
  130. } else if mapInfo != nil {
  131. key, _ := mapInfo["key"].(string)
  132. if key == "" {
  133. key = "udpok"
  134. }
  135. go UdpClient.WriteUdp([]byte(key), udp.OP_NOOP, ra)
  136. //有udp 转发时
  137. if config.Conf.Udp.NeAddr != "" {
  138. go SendUdpMsg(mapInfo, NeUdpAddr)
  139. }
  140. tasktype, _ := mapInfo["stype"].(string)
  141. switch tasktype {
  142. case "index-by-id": //单个索引,更新pici
  143. pool <- true
  144. go func() {
  145. defer func() {
  146. <-pool
  147. }()
  148. biddingTaskById(mapInfo)
  149. }()
  150. case "index_by_id": //单个索引,不更新pici
  151. pool <- true
  152. go func() {
  153. defer func() {
  154. <-pool
  155. }()
  156. biddingTaskById(mapInfo)
  157. }()
  158. case "bidding":
  159. BiddingLastNodeResponse = time.Now().Unix()
  160. pool <- true
  161. go func() {
  162. defer func() {
  163. <-pool
  164. }()
  165. biddingTask(mapInfo)
  166. }()
  167. case "bidding_detail": //bidding_detail 标讯详情索引;历史数据需要读取oss
  168. pool <- true
  169. go func() {
  170. defer func() {
  171. <-pool
  172. }()
  173. biddingDetailTask(mapInfo)
  174. }()
  175. case "biddingall": //补充存量数据
  176. pool <- true
  177. go func() {
  178. defer func() {
  179. <-pool
  180. }()
  181. biddingAllTask(mapInfo)
  182. }()
  183. case "bidding_all_data": //根据biddingall配置文件,存量迁移数据
  184. pool <- true
  185. go func() {
  186. defer func() {
  187. <-pool
  188. }()
  189. biddingAllDataTask()
  190. }()
  191. case "bidding_history":
  192. pool <- true
  193. go func() {
  194. defer func() {
  195. <-pool
  196. }()
  197. biddingTask(mapInfo)
  198. }()
  199. case "project":
  200. ProjectLastNodeResponse = time.Now().Unix()
  201. pool <- true
  202. go func() {
  203. defer func() {
  204. <-pool
  205. }()
  206. projectTask(data, mapInfo)
  207. // 配置项目详情索引
  208. if config.Conf.DB.Es.IndexPD != "" {
  209. projectDetailTask(data, mapInfo)
  210. }
  211. }()
  212. case "project_detail": //添加了详情字段的项目索引
  213. if config.Conf.DB.Es.IndexPD != "" {
  214. ProjectLastNodeResponse = time.Now().Unix()
  215. pool <- true
  216. go func() {
  217. defer func() {
  218. <-pool
  219. }()
  220. projectDetailTask(data, mapInfo)
  221. }()
  222. } else {
  223. log.Info("升级项目索引", zap.String("项目详情索引 ", "缺少项目详情索引配置,请检查配置文件"))
  224. }
  225. case "project_all_data": //存量 projectset 数据
  226. pool <- true
  227. go func() {
  228. defer func() {
  229. <-pool
  230. }()
  231. projectAllData()
  232. }()
  233. case "biddingdata": //es 单机版,采集判重,对应索引 biddingalll; 172.17.4.184:19905
  234. pool <- true
  235. go func() {
  236. defer func() {
  237. <-pool
  238. }()
  239. biddingDataTask(data, mapInfo)
  240. }()
  241. case "biddingdelbyextracttype": //根据bidding表extracttype=-1,删除es中重复数据
  242. pool <- true
  243. go func() {
  244. defer func() {
  245. <-pool
  246. }()
  247. biddingDelByExtracttype(data, mapInfo)
  248. }()
  249. case "buyer_once": // 采购单位昨天增量数据
  250. pool <- true
  251. go func() {
  252. defer func() {
  253. <-pool
  254. }()
  255. buyerOnce()
  256. }()
  257. case "buyer_all": // 采购单位全量数据
  258. pool <- true
  259. go func() {
  260. defer func() {
  261. <-pool
  262. }()
  263. buyerAll()
  264. }()
  265. case "winner_once": // 中标单位昨天增量数据
  266. pool <- true
  267. go func() {
  268. defer func() {
  269. <-pool
  270. }()
  271. winnerEsTaskOnce()
  272. }()
  273. case "winner_all": // 中标单位存量数据
  274. pool <- true
  275. go func() {
  276. defer func() {
  277. <-pool
  278. }()
  279. winnerEsAll()
  280. }()
  281. case "attachment": // 补充附件采集,对应bidding为bidding_downloadfile_log
  282. pool <- true
  283. go func() {
  284. defer func() {
  285. <-pool
  286. }()
  287. //有单独配置其他操作
  288. if len(config.Conf.Others) > 0 {
  289. if v, ok := config.Conf.Others[tasktype]; ok {
  290. attachmentBiddingTask(mapInfo, v)
  291. }
  292. }
  293. }()
  294. default:
  295. pool <- true
  296. go func() {
  297. defer func() {
  298. <-pool
  299. }()
  300. log.Info("err", zap.Any("mapInfo", mapInfo))
  301. }()
  302. }
  303. }
  304. case udp.OP_NOOP: //下个节点回应
  305. ok := string(data)
  306. if ok != "" {
  307. log.Info("udp re", zap.String("data:", ok))
  308. UdpTaskMap.Delete(ok)
  309. }
  310. }
  311. }
  312. func task_index() {
  313. c := cron.New()
  314. _ = c.AddFunc("0 00 00 * * *", func() { task_winneres() }) //每天凌晨执行一次winner生索引
  315. _ = c.AddFunc("0 00 01 * * *", func() { task_buyeres() }) //每天1点执行一次buyer生索引
  316. c.Start()
  317. }
  318. func task_winneres() {
  319. log.Info("定时任务,winneres")
  320. winnerEsTaskOnce()
  321. }
  322. func task_buyeres() {
  323. log.Info("定时任务,buyeres")
  324. buyerOnce()
  325. }
  326. type UdpNode struct {
  327. data []byte
  328. addr *net.UDPAddr
  329. timestamp int64
  330. retry int
  331. }
  332. // UpdateBidding 更新bidding表数据
  333. func UpdateBidding() {
  334. //arru := make([][]map[string]interface{}, 10)
  335. //indexu := 0
  336. for {
  337. select {
  338. case v := <-updateBiddingPool:
  339. MgoB.UpdateById(config.Conf.DB.MongoB.Coll, v["_id"], map[string]interface{}{"$set": v["set"]})
  340. //arru[indexu] = v
  341. //indexu++
  342. //if indexu == 10 {
  343. // updateBiddingSp <- true
  344. //go func(arru [][]map[string]interface{}) {
  345. // defer func() {
  346. // <-updateBiddingSp
  347. //}()
  348. //MgoB.UpdateBulk(config.Conf.DB.MongoB.Coll, arru...)
  349. //}(arru)
  350. //arru = make([][]map[string]interface{}, 10)
  351. //indexu = 0
  352. //}
  353. //case <-time.After(1000 * time.Millisecond):
  354. // if indexu > 0 {
  355. // updateBiddingSp <- true
  356. // go func(arru [][]map[string]interface{}) {
  357. // defer func() {
  358. // <-updateBiddingSp
  359. // }()
  360. // MgoB.UpdateBulk(config.Conf.DB.MongoB.Coll, arru...)
  361. // }(arru[:indexu])
  362. // arru = make([][]map[string]interface{}, 200)
  363. // indexu = 0
  364. // }
  365. }
  366. }
  367. }
  368. // SaveBidErr 记录错误信息,暂时记录 附件过长的
  369. func SaveBidErr() {
  370. arru := make([]map[string]interface{}, 200)
  371. indexu := 0
  372. for {
  373. select {
  374. case v := <-saveErrBidPool:
  375. arru[indexu] = v
  376. indexu++
  377. if indexu == 200 {
  378. saveBidSp <- true
  379. go func(arru []map[string]interface{}) {
  380. defer func() {
  381. <-saveBidSp
  382. }()
  383. MgoB.SaveBulk("bidding_es_err_record", arru...)
  384. }(arru)
  385. arru = make([]map[string]interface{}, 200)
  386. indexu = 0
  387. }
  388. case <-time.After(1000 * time.Millisecond):
  389. if indexu > 0 {
  390. saveBidSp <- true
  391. go func(arru []map[string]interface{}) {
  392. defer func() {
  393. <-saveBidSp
  394. }()
  395. MgoB.SaveBulk("bidding_es_err_record", arru...)
  396. }(arru[:indexu])
  397. arru = make([]map[string]interface{}, 200)
  398. indexu = 0
  399. }
  400. }
  401. }
  402. }
  403. // SaveEsMethod 保存bidding数据到es;单携程保存
  404. func SaveEsMethod() {
  405. for {
  406. select {
  407. case v := <-saveEsPool:
  408. id := v["id"]
  409. ids := v["_id"]
  410. //如果优选,存在上次的优选ID,需要删除上次的ID索引
  411. oid := util.ObjToString(v["old_preferid"])
  412. delete(v, "old_preferid")
  413. if oid != "" {
  414. err := Es.DeleteByID(config.Conf.DB.Es.IndexB, oid)
  415. if err != nil {
  416. log.Info("SaveEsMethod", zap.String("上次优选ID删除失败", oid))
  417. }
  418. }
  419. Es.Save(config.Conf.DB.Es.IndexB, v)
  420. // 华为云集群1
  421. if config.Conf.DB.Es.Addr2 != "" {
  422. v["id"] = id
  423. v["_id"] = ids
  424. delete(v, "old_preferid")
  425. if oid != "" {
  426. err := Es2.DeleteByID(config.Conf.DB.Es.IndexB, oid)
  427. if err != nil {
  428. log.Info("SaveEsMethod", zap.String("上次优选ID删除失败", oid))
  429. }
  430. }
  431. Es2.Save(config.Conf.DB.Es.Indexb2, v)
  432. }
  433. // 华为云新集群2,迁移原来阿里云数据
  434. if config.Conf.DB.Es.Addr3 != "" {
  435. v["id"] = id
  436. v["_id"] = ids
  437. delete(v, "old_preferid")
  438. if oid != "" {
  439. err := Es3.DeleteByID(config.Conf.DB.Es.IndexB, oid)
  440. if err != nil {
  441. log.Info("SaveEsMethod", zap.String("上次优选ID删除失败", oid))
  442. }
  443. }
  444. Es3.Save(config.Conf.DB.Es.Indexb3, v)
  445. }
  446. }
  447. }
  448. }
  449. // SaveDetailEsMethod 保存bidding detail、contenghtml 字段的详情索引
  450. func SaveDetailEsMethod() {
  451. if config.Conf.DB.Es.BiddingDetail == "" {
  452. config.Conf.DB.Es.BiddingDetail = "bidding_detail"
  453. log.Info("SaveDetailEsMethod", zap.String("config.Conf.DB.Es.BiddingDetail", "配置文件中,biddingdetail 没有配置,默认初始化为 bidding_detail"))
  454. }
  455. for {
  456. select {
  457. case v := <-saveDetailEsPool:
  458. Es.Save(config.Conf.DB.Es.BiddingDetail, v)
  459. }
  460. }
  461. }
  462. // BatchSaveBiddingDetailEsMethod 批量保存bidding_detail
  463. func BatchSaveBiddingDetailEsMethod() {
  464. arru := make([]map[string]interface{}, EsBulkSize)
  465. indexu := 0
  466. for {
  467. select {
  468. case v := <-saveDetailEsPool:
  469. arru[indexu] = v
  470. indexu++
  471. if indexu == EsBulkSize {
  472. saveDetailEsSp <- true
  473. go func(arru []map[string]interface{}) {
  474. defer func() {
  475. <-saveDetailEsSp
  476. }()
  477. if config.Conf.DB.Es.Addr != "" {
  478. Es.BulkSave(config.Conf.DB.Es.BiddingDetail, arru)
  479. }
  480. }(arru)
  481. arru = make([]map[string]interface{}, EsBulkSize)
  482. indexu = 0
  483. }
  484. case <-time.After(1000 * time.Millisecond):
  485. if indexu > 0 {
  486. saveDetailEsSp <- true
  487. go func(arru []map[string]interface{}) {
  488. defer func() {
  489. <-saveDetailEsSp
  490. }()
  491. if config.Conf.DB.Es.Addr != "" {
  492. Es.BulkSave(config.Conf.DB.Es.BiddingDetail, arru)
  493. }
  494. }(arru[:indexu])
  495. arru = make([]map[string]interface{}, EsBulkSize)
  496. indexu = 0
  497. }
  498. }
  499. }
  500. }
  501. // SaveBiddingEsMethod 批量保存bidding数据
  502. func SaveBiddingEsMethod() {
  503. arru := make([]map[string]interface{}, EsBulkSize)
  504. indexu := 0
  505. for {
  506. select {
  507. case v := <-saveEsPool:
  508. arru[indexu] = v
  509. indexu++
  510. if indexu == EsBulkSize {
  511. saveEsSp <- true
  512. go func(arru []map[string]interface{}) {
  513. defer func() {
  514. <-saveEsSp
  515. }()
  516. if config.Conf.DB.Es.Addr != "" {
  517. Es.BulkSave(config.Conf.DB.Es.IndexB, arru)
  518. }
  519. // 集群地址2
  520. if config.Conf.DB.Es.Addr2 != "" {
  521. Es2.BulkSave(config.Conf.DB.Es.Indexb2, arru)
  522. }
  523. // 集群地址3
  524. if config.Conf.DB.Es.Addr3 != "" {
  525. Es3.BulkSave(config.Conf.DB.Es.Indexb3, arru)
  526. }
  527. }(arru)
  528. arru = make([]map[string]interface{}, EsBulkSize)
  529. indexu = 0
  530. }
  531. case <-time.After(1000 * time.Millisecond):
  532. if indexu > 0 {
  533. saveEsSp <- true
  534. go func(arru []map[string]interface{}) {
  535. defer func() {
  536. <-saveEsSp
  537. }()
  538. if config.Conf.DB.Es.Addr != "" {
  539. Es.BulkSave(config.Conf.DB.Es.IndexB, arru)
  540. }
  541. // 集群地址2
  542. if config.Conf.DB.Es.Addr2 != "" {
  543. Es2.BulkSave(config.Conf.DB.Es.Indexb2, arru)
  544. }
  545. // 集群地址3
  546. if config.Conf.DB.Es.Addr3 != "" {
  547. Es3.BulkSave(config.Conf.DB.Es.Indexb3, arru)
  548. }
  549. }(arru[:indexu])
  550. arru = make([]map[string]interface{}, EsBulkSize)
  551. indexu = 0
  552. }
  553. }
  554. }
  555. }
  556. // SaveAllEsMethod 保存爬虫采集临时数据,保存在华为云上
  557. func SaveAllEsMethod() {
  558. arru := make([]map[string]interface{}, EsBulkSize)
  559. indexu := 0
  560. for {
  561. select {
  562. case v := <-saveEsAllPool:
  563. arru[indexu] = v
  564. indexu++
  565. if indexu == EsBulkSize {
  566. saveEsAllSp <- true
  567. go func(arru []map[string]interface{}) {
  568. defer func() {
  569. <-saveEsAllSp
  570. }()
  571. if config.Conf.DB.Es.Addr2 != "" {
  572. Es2.BulkSave("biddingall", arru)
  573. }
  574. }(arru)
  575. arru = make([]map[string]interface{}, EsBulkSize)
  576. indexu = 0
  577. }
  578. case <-time.After(1000 * time.Millisecond):
  579. if indexu > 0 {
  580. saveEsAllSp <- true
  581. go func(arru []map[string]interface{}) {
  582. defer func() {
  583. <-saveEsAllSp
  584. }()
  585. if config.Conf.DB.Es.Addr2 != "" {
  586. Es2.BulkSave("biddingall", arru)
  587. }
  588. }(arru[:indexu])
  589. arru = make([]map[string]interface{}, EsBulkSize)
  590. indexu = 0
  591. }
  592. }
  593. }
  594. }
  595. // SaveBiddingAllDataEs 保存bidding数据到Es,stype=bidding_all_data 数据
  596. func SaveBiddingAllDataEs() {
  597. arru := make([]map[string]interface{}, EsBulkSize)
  598. indexu := 0
  599. for {
  600. select {
  601. case v := <-saveBiddingAllPool:
  602. arru[indexu] = v
  603. indexu++
  604. if indexu == EsBulkSize {
  605. saveBiddingAllBEsSp <- true
  606. go func(arru []map[string]interface{}) {
  607. defer func() {
  608. <-saveBiddingAllBEsSp
  609. }()
  610. //1.阿里云集群
  611. Es.BulkSave(config.Conf.DB.Es.IndexB, arru)
  612. }(arru)
  613. arru = make([]map[string]interface{}, EsBulkSize)
  614. indexu = 0
  615. }
  616. case <-time.After(1000 * time.Millisecond):
  617. if indexu > 0 {
  618. saveBiddingAllBEsSp <- true
  619. go func(arru []map[string]interface{}) {
  620. defer func() {
  621. <-saveBiddingAllBEsSp
  622. }()
  623. //1.阿里云集群
  624. Es.BulkSave(config.Conf.DB.Es.IndexB, arru)
  625. }(arru[:indexu])
  626. arru = make([]map[string]interface{}, EsBulkSize)
  627. indexu = 0
  628. }
  629. }
  630. }
  631. }
  632. // SaveProjectEsMethod 保存项目索引数据
  633. func SaveProjectEsMethod() {
  634. arru := make([]map[string]interface{}, EsBulkSize)
  635. indexu := 0
  636. for {
  637. select {
  638. case v := <-saveProjectEsPool:
  639. arru[indexu] = v
  640. indexu++
  641. if indexu == EsBulkSize {
  642. saveProjectSp <- true
  643. go func(arru []map[string]interface{}) {
  644. defer func() {
  645. <-saveProjectSp
  646. }()
  647. Es.BulkSave(config.Conf.DB.Es.IndexP, arru)
  648. // 华为云新集群,存储标讯、项目、凭安数据
  649. if config.Conf.DB.Es.Addr3 != "" {
  650. Es3.BulkSave(config.Conf.DB.Es.IndexP, arru)
  651. }
  652. }(arru)
  653. arru = make([]map[string]interface{}, EsBulkSize)
  654. indexu = 0
  655. }
  656. case <-time.After(1000 * time.Millisecond):
  657. if indexu > 0 {
  658. saveProjectSp <- true
  659. go func(arru []map[string]interface{}) {
  660. defer func() {
  661. <-saveProjectSp
  662. }()
  663. Es.BulkSave(config.Conf.DB.Es.IndexP, arru)
  664. // 华为云新集群,存储标讯、项目、凭安数据
  665. if config.Conf.DB.Es.Addr3 != "" {
  666. Es3.BulkSave(config.Conf.DB.Es.IndexP, arru)
  667. }
  668. }(arru[:indexu])
  669. arru = make([]map[string]interface{}, EsBulkSize)
  670. indexu = 0
  671. }
  672. }
  673. }
  674. }
  675. // SaveProjectEs 保存项目索引数据,但携程保存
  676. func SaveProjectEs() {
  677. for {
  678. select {
  679. case v := <-saveProjectEsPool:
  680. id := v["id"]
  681. ids := v["_id"]
  682. Es.Save(config.Conf.DB.Es.IndexP, v)
  683. // 华为云新集群,存储标讯、项目、凭安数据
  684. if config.Conf.DB.Es.Addr3 != "" {
  685. v["id"] = id
  686. v["_id"] = ids
  687. Es3.Save(config.Conf.DB.Es.IndexP, v)
  688. }
  689. }
  690. }
  691. }
  692. // SaveProjectDetailEs 保存项目索引,支持详情字段
  693. func SaveProjectDetailEs() {
  694. for {
  695. select {
  696. case v := <-saveProjectDetailEsPool:
  697. id := v["id"]
  698. ids := v["_id"]
  699. Es.Save(config.Conf.DB.Es.IndexPD, v)
  700. // 华为云新集群,存储标讯、项目、凭安数据
  701. if config.Conf.DB.Es.Addr3 != "" {
  702. v["id"] = id
  703. v["_id"] = ids
  704. Es3.Save(config.Conf.DB.Es.IndexPD, v)
  705. }
  706. }
  707. }
  708. }
  709. func checkMapJob() {
  710. if config.Conf.Mail.Send {
  711. log.Info("checkMapJob", zap.String("to:", config.Conf.Mail.To))
  712. for {
  713. UdpTaskMap.Range(func(k, v interface{}) bool {
  714. now := time.Now().Unix()
  715. node, _ := v.(*UdpNode)
  716. if now-node.timestamp > 120 {
  717. node.retry++
  718. if node.retry > 5 {
  719. UdpTaskMap.Delete(k)
  720. 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)))
  721. if err == nil {
  722. defer res.Body.Close()
  723. read, err := ioutil.ReadAll(res.Body)
  724. log.Info("send mail ...", zap.String("r:", string(read)), zap.Any("err:", err))
  725. }
  726. } else {
  727. log.Info("udp重发", zap.Any("k:", k))
  728. UdpClient.WriteUdp(node.data, udp.OP_TYPE_DATA, node.addr)
  729. }
  730. } else if now-node.timestamp > 10 {
  731. log.Info("udp任务超时中..", zap.Any("k:", k))
  732. }
  733. return true
  734. })
  735. time.Sleep(60 * time.Second)
  736. }
  737. }
  738. }
  739. func task() {
  740. sess := MgoB.GetMgoConn()
  741. defer MgoB.DestoryMongoConn(sess)
  742. ch := make(chan bool, 10)
  743. wg := &sync.WaitGroup{}
  744. query := sess.DB("qfw").C("result_replace_repair_log").Find(nil).Iter()
  745. count := 0
  746. for tmp := make(map[string]interface{}); query.Next(tmp); count++ {
  747. if count%1000 == 0 {
  748. util.Debug("current ---", count)
  749. }
  750. ch <- true
  751. wg.Add(1)
  752. go func(tmp map[string]interface{}) {
  753. defer func() {
  754. <-ch
  755. wg.Done()
  756. }()
  757. if id := util.ObjToString(tmp["replace_id"]); mongodb.IsObjectIdHex(id) {
  758. biddingTaskById(map[string]interface{}{"infoid": id, "stype": "bidding"})
  759. }
  760. }(tmp)
  761. tmp = make(map[string]interface{})
  762. }
  763. wg.Wait()
  764. util.Debug("over ---", count)
  765. }
  766. // LastUdpJob 处理UDP 没有接受数据
  767. func LastUdpJob() {
  768. for {
  769. responselock.Lock()
  770. if time.Now().Unix()-BiddingLastNodeResponse >= 1800 {
  771. BiddingLastNodeResponse = time.Now().Unix() //重置时间
  772. sendErrMailApi("索引程序异常", fmt.Sprintf("半小时左右 无bidding据进入 ...相关人员检查..."))
  773. }
  774. if time.Now().Unix()-ProjectLastNodeResponse >= 1800 {
  775. ProjectLastNodeResponse = time.Now().Unix() //重置时间
  776. sendErrMailApi("索引程序异常", fmt.Sprintf("半小时左右 无project数据进入 ...相关人员检查..."))
  777. }
  778. responselock.Unlock()
  779. time.Sleep(300 * time.Second)
  780. }
  781. }
  782. // sendErrMailApi 发送邮件
  783. func sendErrMailApi(title, body string) {
  784. var tomail, api string
  785. if config.Conf.Mail.Send {
  786. tomail = config.Conf.Mail.To
  787. api = config.Conf.Mail.Api
  788. }
  789. log.Info("sendErrMailApi", zap.Any(tomail, api))
  790. res, err := http.Get(fmt.Sprintf("%s?to=%s&title=%s&body=%s", api, tomail, title, body))
  791. if err == nil {
  792. defer res.Body.Close()
  793. read, err := ioutil.ReadAll(res.Body)
  794. if err != nil {
  795. log.Info("邮件发送成功", zap.String("read", string(read)))
  796. }
  797. } else {
  798. log.Info("sendErrMailApi", zap.String("邮件发送失败", err.Error()))
  799. }
  800. }