main.go 25 KB

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