main.go 24 KB

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