main.go 18 KB

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