main.go 24 KB

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