main.go 18 KB

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