main.go 20 KB

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