main.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  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. stype := util.ObjToString(v["stype"])
  420. delete(v, "stype")
  421. mgoUpdate := make(map[string]interface{})
  422. if stype == "bidding" || stype == "bidding_history" || stype == "index-by-id" {
  423. //之前存在pici,就不在添加
  424. if pici, ok := v["pici"]; ok {
  425. v["pici"] = pici
  426. } else {
  427. picc := time.Now().Unix()
  428. v["pici"] = picc
  429. mgoUpdate["pici"] = picc
  430. }
  431. }
  432. if len(mgoUpdate) > 0 {
  433. updateBiddingPool <- map[string]interface{}{
  434. "_id": mongodb.StringTOBsonId(util.ObjToString(v["_id"])),
  435. "set": mgoUpdate,
  436. }
  437. if util.ObjToString(v["spidercode"]) == "a_jyxxfbpt_gg" {
  438. // 剑鱼信息发布数据 通过udp通知信息发布程序
  439. go UdpMethod(mongodb.BsonIdToSId(v["_id"]))
  440. }
  441. }
  442. Es.Save(config.Conf.DB.Es.IndexB, v)
  443. // 华为云集群1
  444. if config.Conf.DB.Es.Addr2 != "" {
  445. v["id"] = id
  446. v["_id"] = ids
  447. delete(v, "old_preferid")
  448. if oid != "" {
  449. err := Es2.DeleteByID(config.Conf.DB.Es.IndexB, oid)
  450. if err != nil {
  451. log.Info("SaveEsMethod", zap.String("上次优选ID删除失败", oid))
  452. }
  453. }
  454. Es2.Save(config.Conf.DB.Es.Indexb2, v)
  455. }
  456. // 华为云新集群2,迁移原来阿里云数据
  457. if config.Conf.DB.Es.Addr3 != "" {
  458. v["id"] = id
  459. v["_id"] = ids
  460. delete(v, "old_preferid")
  461. if oid != "" {
  462. err := Es3.DeleteByID(config.Conf.DB.Es.IndexB, oid)
  463. if err != nil {
  464. log.Info("SaveEsMethod", zap.String("上次优选ID删除失败", oid))
  465. }
  466. }
  467. Es3.Save(config.Conf.DB.Es.Indexb3, v)
  468. }
  469. }
  470. }
  471. }
  472. // SaveDetailEsMethod 保存bidding detail、contenghtml 字段的详情索引
  473. func SaveDetailEsMethod() {
  474. if config.Conf.DB.Es.BiddingDetail == "" {
  475. config.Conf.DB.Es.BiddingDetail = "bidding_detail"
  476. log.Info("SaveDetailEsMethod", zap.String("config.Conf.DB.Es.BiddingDetail", "配置文件中,biddingdetail 没有配置,默认初始化为 bidding_detail"))
  477. }
  478. for {
  479. select {
  480. case v := <-saveDetailEsPool:
  481. Es.Save(config.Conf.DB.Es.BiddingDetail, v)
  482. }
  483. }
  484. }
  485. // BatchSaveBiddingDetailEsMethod 批量保存bidding_detail
  486. func BatchSaveBiddingDetailEsMethod() {
  487. arru := make([]map[string]interface{}, EsBulkSize)
  488. indexu := 0
  489. for {
  490. select {
  491. case v := <-saveDetailEsPool:
  492. arru[indexu] = v
  493. indexu++
  494. if indexu == EsBulkSize {
  495. saveDetailEsSp <- true
  496. go func(arru []map[string]interface{}) {
  497. defer func() {
  498. <-saveDetailEsSp
  499. }()
  500. if config.Conf.DB.Es.Addr != "" {
  501. Es.BulkSave(config.Conf.DB.Es.BiddingDetail, arru)
  502. }
  503. }(arru)
  504. arru = make([]map[string]interface{}, EsBulkSize)
  505. indexu = 0
  506. }
  507. case <-time.After(1000 * time.Millisecond):
  508. if indexu > 0 {
  509. saveDetailEsSp <- true
  510. go func(arru []map[string]interface{}) {
  511. defer func() {
  512. <-saveDetailEsSp
  513. }()
  514. if config.Conf.DB.Es.Addr != "" {
  515. Es.BulkSave(config.Conf.DB.Es.BiddingDetail, arru)
  516. }
  517. }(arru[:indexu])
  518. arru = make([]map[string]interface{}, EsBulkSize)
  519. indexu = 0
  520. }
  521. }
  522. }
  523. }
  524. // SaveBiddingEsMethod 批量保存bidding数据
  525. func SaveBiddingEsMethod() {
  526. arru := make([]map[string]interface{}, EsBulkSize)
  527. indexu := 0
  528. for {
  529. select {
  530. case v := <-saveEsPool:
  531. arru[indexu] = v
  532. indexu++
  533. if indexu == EsBulkSize {
  534. saveEsSp <- true
  535. go func(arru []map[string]interface{}) {
  536. defer func() {
  537. <-saveEsSp
  538. }()
  539. if config.Conf.DB.Es.Addr != "" {
  540. Es.BulkSave(config.Conf.DB.Es.IndexB, arru)
  541. }
  542. // 集群地址2
  543. if config.Conf.DB.Es.Addr2 != "" {
  544. Es2.BulkSave(config.Conf.DB.Es.Indexb2, arru)
  545. }
  546. // 集群地址3
  547. if config.Conf.DB.Es.Addr3 != "" {
  548. Es3.BulkSave(config.Conf.DB.Es.Indexb3, arru)
  549. }
  550. }(arru)
  551. arru = make([]map[string]interface{}, EsBulkSize)
  552. indexu = 0
  553. }
  554. case <-time.After(1000 * time.Millisecond):
  555. if indexu > 0 {
  556. saveEsSp <- true
  557. go func(arru []map[string]interface{}) {
  558. defer func() {
  559. <-saveEsSp
  560. }()
  561. if config.Conf.DB.Es.Addr != "" {
  562. Es.BulkSave(config.Conf.DB.Es.IndexB, arru)
  563. }
  564. // 集群地址2
  565. if config.Conf.DB.Es.Addr2 != "" {
  566. Es2.BulkSave(config.Conf.DB.Es.Indexb2, arru)
  567. }
  568. // 集群地址3
  569. if config.Conf.DB.Es.Addr3 != "" {
  570. Es3.BulkSave(config.Conf.DB.Es.Indexb3, arru)
  571. }
  572. }(arru[:indexu])
  573. arru = make([]map[string]interface{}, EsBulkSize)
  574. indexu = 0
  575. }
  576. }
  577. }
  578. }
  579. // SaveAllEsMethod 保存爬虫采集临时数据,保存在华为云上
  580. func SaveAllEsMethod() {
  581. arru := make([]map[string]interface{}, EsBulkSize)
  582. indexu := 0
  583. for {
  584. select {
  585. case v := <-saveEsAllPool:
  586. arru[indexu] = v
  587. indexu++
  588. if indexu == EsBulkSize {
  589. saveEsAllSp <- true
  590. go func(arru []map[string]interface{}) {
  591. defer func() {
  592. <-saveEsAllSp
  593. }()
  594. if config.Conf.DB.Es.Addr2 != "" {
  595. Es2.BulkSave("biddingall", arru)
  596. }
  597. }(arru)
  598. arru = make([]map[string]interface{}, EsBulkSize)
  599. indexu = 0
  600. }
  601. case <-time.After(1000 * time.Millisecond):
  602. if indexu > 0 {
  603. saveEsAllSp <- true
  604. go func(arru []map[string]interface{}) {
  605. defer func() {
  606. <-saveEsAllSp
  607. }()
  608. if config.Conf.DB.Es.Addr2 != "" {
  609. Es2.BulkSave("biddingall", arru)
  610. }
  611. }(arru[:indexu])
  612. arru = make([]map[string]interface{}, EsBulkSize)
  613. indexu = 0
  614. }
  615. }
  616. }
  617. }
  618. // SaveBiddingAllDataEs 保存bidding数据到Es,stype=bidding_all_data 数据
  619. func SaveBiddingAllDataEs() {
  620. arru := make([]map[string]interface{}, EsBulkSize)
  621. indexu := 0
  622. for {
  623. select {
  624. case v := <-saveBiddingAllPool:
  625. arru[indexu] = v
  626. indexu++
  627. if indexu == EsBulkSize {
  628. saveBiddingAllBEsSp <- true
  629. go func(arru []map[string]interface{}) {
  630. defer func() {
  631. <-saveBiddingAllBEsSp
  632. }()
  633. //1.阿里云集群
  634. Es.BulkSave(config.Conf.DB.Es.IndexB, arru)
  635. }(arru)
  636. arru = make([]map[string]interface{}, EsBulkSize)
  637. indexu = 0
  638. }
  639. case <-time.After(1000 * time.Millisecond):
  640. if indexu > 0 {
  641. saveBiddingAllBEsSp <- true
  642. go func(arru []map[string]interface{}) {
  643. defer func() {
  644. <-saveBiddingAllBEsSp
  645. }()
  646. //1.阿里云集群
  647. Es.BulkSave(config.Conf.DB.Es.IndexB, arru)
  648. }(arru[:indexu])
  649. arru = make([]map[string]interface{}, EsBulkSize)
  650. indexu = 0
  651. }
  652. }
  653. }
  654. }
  655. // SaveProjectEsMethod 保存项目索引数据
  656. func SaveProjectEsMethod() {
  657. arru := make([]map[string]interface{}, EsBulkSize)
  658. indexu := 0
  659. for {
  660. select {
  661. case v := <-saveProjectEsPool:
  662. arru[indexu] = v
  663. indexu++
  664. if indexu == EsBulkSize {
  665. saveProjectSp <- true
  666. go func(arru []map[string]interface{}) {
  667. defer func() {
  668. <-saveProjectSp
  669. }()
  670. Es.BulkSave(config.Conf.DB.Es.IndexP, arru)
  671. // 华为云新集群,存储标讯、项目、凭安数据
  672. if config.Conf.DB.Es.Addr3 != "" {
  673. Es3.BulkSave(config.Conf.DB.Es.IndexP, arru)
  674. }
  675. }(arru)
  676. arru = make([]map[string]interface{}, EsBulkSize)
  677. indexu = 0
  678. }
  679. case <-time.After(1000 * time.Millisecond):
  680. if indexu > 0 {
  681. saveProjectSp <- true
  682. go func(arru []map[string]interface{}) {
  683. defer func() {
  684. <-saveProjectSp
  685. }()
  686. Es.BulkSave(config.Conf.DB.Es.IndexP, arru)
  687. // 华为云新集群,存储标讯、项目、凭安数据
  688. if config.Conf.DB.Es.Addr3 != "" {
  689. Es3.BulkSave(config.Conf.DB.Es.IndexP, arru)
  690. }
  691. }(arru[:indexu])
  692. arru = make([]map[string]interface{}, EsBulkSize)
  693. indexu = 0
  694. }
  695. }
  696. }
  697. }
  698. // SaveProjectEs 保存项目索引数据,但携程保存
  699. func SaveProjectEs() {
  700. for {
  701. select {
  702. case v := <-saveProjectEsPool:
  703. id := v["id"]
  704. ids := v["_id"]
  705. Es.Save(config.Conf.DB.Es.IndexP, v)
  706. // 华为云新集群,存储标讯、项目、凭安数据
  707. if config.Conf.DB.Es.Addr3 != "" {
  708. v["id"] = id
  709. v["_id"] = ids
  710. Es3.Save(config.Conf.DB.Es.IndexP, v)
  711. }
  712. }
  713. }
  714. }
  715. // SaveProjectDetailEs 保存项目索引,支持详情字段
  716. func SaveProjectDetailEs() {
  717. for {
  718. select {
  719. case v := <-saveProjectDetailEsPool:
  720. id := v["id"]
  721. ids := v["_id"]
  722. Es.Save(config.Conf.DB.Es.IndexPD, v)
  723. // 华为云新集群,存储标讯、项目、凭安数据
  724. if config.Conf.DB.Es.Addr3 != "" {
  725. v["id"] = id
  726. v["_id"] = ids
  727. Es3.Save(config.Conf.DB.Es.IndexPD, v)
  728. }
  729. }
  730. }
  731. }
  732. func checkMapJob() {
  733. if config.Conf.Mail.Send {
  734. log.Info("checkMapJob", zap.String("to:", config.Conf.Mail.To))
  735. for {
  736. UdpTaskMap.Range(func(k, v interface{}) bool {
  737. now := time.Now().Unix()
  738. node, _ := v.(*UdpNode)
  739. if now-node.timestamp > 120 {
  740. node.retry++
  741. if node.retry > 5 {
  742. UdpTaskMap.Delete(k)
  743. 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)))
  744. if err == nil {
  745. defer res.Body.Close()
  746. read, err := ioutil.ReadAll(res.Body)
  747. log.Info("send mail ...", zap.String("r:", string(read)), zap.Any("err:", err))
  748. }
  749. } else {
  750. log.Info("udp重发", zap.Any("k:", k))
  751. UdpClient.WriteUdp(node.data, udp.OP_TYPE_DATA, node.addr)
  752. }
  753. } else if now-node.timestamp > 10 {
  754. log.Info("udp任务超时中..", zap.Any("k:", k))
  755. }
  756. return true
  757. })
  758. time.Sleep(60 * time.Second)
  759. }
  760. }
  761. }
  762. func task() {
  763. sess := MgoB.GetMgoConn()
  764. defer MgoB.DestoryMongoConn(sess)
  765. ch := make(chan bool, 10)
  766. wg := &sync.WaitGroup{}
  767. query := sess.DB("qfw").C("result_replace_repair_log").Find(nil).Iter()
  768. count := 0
  769. for tmp := make(map[string]interface{}); query.Next(tmp); count++ {
  770. if count%1000 == 0 {
  771. util.Debug("current ---", count)
  772. }
  773. ch <- true
  774. wg.Add(1)
  775. go func(tmp map[string]interface{}) {
  776. defer func() {
  777. <-ch
  778. wg.Done()
  779. }()
  780. if id := util.ObjToString(tmp["replace_id"]); mongodb.IsObjectIdHex(id) {
  781. biddingTaskById(map[string]interface{}{"infoid": id, "stype": "bidding"})
  782. }
  783. }(tmp)
  784. tmp = make(map[string]interface{})
  785. }
  786. wg.Wait()
  787. util.Debug("over ---", count)
  788. }
  789. // LastUdpJob 处理UDP 没有接受数据
  790. func LastUdpJob() {
  791. for {
  792. responselock.Lock()
  793. if time.Now().Unix()-BiddingLastNodeResponse >= 1800 {
  794. BiddingLastNodeResponse = time.Now().Unix() //重置时间
  795. sendErrMailApi("索引程序异常", fmt.Sprintf("半小时左右 无bidding据进入 ...相关人员检查..."))
  796. }
  797. if time.Now().Unix()-ProjectLastNodeResponse >= 1800 {
  798. ProjectLastNodeResponse = time.Now().Unix() //重置时间
  799. sendErrMailApi("索引程序异常", fmt.Sprintf("半小时左右 无project数据进入 ...相关人员检查..."))
  800. }
  801. responselock.Unlock()
  802. time.Sleep(300 * time.Second)
  803. }
  804. }
  805. // sendErrMailApi 发送邮件
  806. func sendErrMailApi(title, body string) {
  807. var tomail, api string
  808. if config.Conf.Mail.Send {
  809. tomail = config.Conf.Mail.To
  810. api = config.Conf.Mail.Api
  811. }
  812. log.Info("sendErrMailApi", zap.Any(tomail, api))
  813. res, err := http.Get(fmt.Sprintf("%s?to=%s&title=%s&body=%s", api, tomail, title, body))
  814. if err == nil {
  815. defer res.Body.Close()
  816. read, err := ioutil.ReadAll(res.Body)
  817. if err != nil {
  818. log.Info("邮件发送成功", zap.String("read", string(read)))
  819. }
  820. } else {
  821. log.Info("sendErrMailApi", zap.String("邮件发送失败", err.Error()))
  822. }
  823. }